PropertyGrid, how is behavior different in the desiger vs at runtime? - c#

I've been having some issues with the propertygrid. Namely:
The behavior is not the same when i'm using a the propertygrid in the visual studio designer, versus at runtime, also while debugging.
A few to note (this is using a form in the designer, and editing a custom textbox, versus embedding a propertygrid into a form, and attaching it to the same custom textbox)
When I have multiple object selected in the designer, and I set a property (custom type, with converter) I'm getting a null reference exception. When I used the 'SelectedObjects' field at runtime, I don't get the exception. If I attach the debugger to the designer, the exception will not happen, and my property setting will work correctly.
I implemented my own expandableobject converter that does property ordering. My properties show up in the correct order when I'm running my application, but they still show up in the default order when using the visual studio designer.
Is there any resources out there that can help explain why these behaviors are happening?
Thanks!

Are you referencing any classes outside of the current assembly (or outside the solution even more-so)?
The designer is not nearly as good as loading those external dll's. There are situations where the designer will know that you're deriving from ExpandableObjectConverter, but cannot load your specific base class, and in the end, it just loads the generic converter. Try loading all of your bases classes into the current assembly and see if that makes a difference.

Related

Binding using Source and x:Reference leads to design-time error

for our WPF application I have designed a CustomControl to mimic some flyout-like behavior. The flyout basically just consists of a FrameworkElement which holds the content that is placed inside the flyout.
Controls that are placed in the FrameworkElement of the flyout are, of course, bound to other controls. However, I am confused as this results in several problems:
I can not use ElementName=sourceName to set the source of the binding. Instead I have to use Source={x:Reference sourceName} to denote the source.
The XAML editor of VisualStudio marks all lines where a binding of this type occurs and the ErrorList shows "Object reference not set to an instance of an object" for each occurrence of the binding (no exception is thrown within the designer view).
The project builds successfully and during runtime all bindings work flawlessly! Besides that, it does not matter if the flyout is implemented as a UserControl or a CustomControl.
If a control that is not placed in the flyout is bound to the same source I can use ElementName=sourceName without any problems and the correct values are displayed in the designer.
As proposed in some other replies concerning this error I tried to attach the VS instance to the debugger of a second instance of VS but no errors/exceptions are displayed there.
As already mentioned, everything works at runtime. However, as multiple developers are working on this project it is not acceptable to have any errors at all; warnings would be acceptable to a certain extend.
We know that using a binding proxy (like it is proposed in e.g. https://stackoverflow.com/a/22531679/119177) should/could solve the problem. However, because the current binding is actually working, we hope to solve the design-time problem some other way (maybe even with just suppressing the error message).
Thanks for your help!

Form Designer unable to find type

In a windows form I am populating a combo box with the following code.
this.JackStudTypeComboBox.Items.AddRange(ViewController.GetJackStudMaterialType().ToArray());
When I attempt to open the Form Designer I get the message that the ViewController type is not found.
The ViewController class is located in the same project as the form. Compiling the project has no issues, and running the project results in the expected behaviour.
So, how do I convince the Form Designer to locate this particular class, or to ignore the fact that it is unable to find it or do anything useful with it when in static design mode?
Don't do that.
You should never put any custom logic in InitializeComponent().
Move that to the constructor and everything will work fine.

How to make dynamic user controls play nice with the designer

I have a dynamic user control that instantiates various bars and labels dynamically based on the number of members in a object group. This functionality works out very well, but the issue is that I am not the only developer on this project. I am new to the team and the "senior" members want all of the components to work in the designer. Since the components of the user control are generated at run time I am not sure how to go about making some/any of them show up in design view. Is this even an option?
I don't think that's possible, because there is no markup for the designer to show. I find it hard to believe that they would expect dynamically created controls to show in the designer.
EDIT
Thinking about it some more, why don't you just add a few hard-coded instances of the control in the markup, with the ability to add/remove. That way, the control is displayed in the designer, but you can still add/remove instances. That would probably be the best compromise in this case.
Why not give both the possibility of defining the number of dynamic controls at design time, and at run time?
In your custom control class, you can define a property that specifies the number of controls. Implement adding/removing controls in the set{} method of this property.
Make your property a "designer property". See http://msdn.microsoft.com/en-us/library/a19191fh.aspx
Keep in mind that the designer actually creates an instance of your class. Also, when a user changes your "property" in the designer, the set{} method is invoked.
I hope this helps

Forms Designer Error Display HTML Source

I i am unable to see the design view of a winform app
Errors are:
One or more errors encountered while
loading the designer. The errors are
listed below. Some errors can be fixed
by rebuilding your project, while
others may require code changes.
The event Click is read-only and
cannot be changed.
at
System.ComponentModel.Design.EventBindingService.EventPropertyDescriptor.SetValue(Object
component, Object value)
at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializeAttachEventStatement(IDesignerSerializationManager
manager, CodeAttachEventStatement
statement)
at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializeStatement(IDesignerSerializationManager
manager, CodeStatement statement)
Try the following helpful links:
http://connect.microsoft.com/VisualStudio/feedback/details/329327/designer-error-when-implementing-a-baseform-event-for-a-control-on-an-inherited-form
http://www.google.co.uk/search?hl=en&source=hp&biw=1600&bih=1075&q=the+event+click+is+read-only+and+cannot+be+changed&aq=f&aqi=g1&aql=&oq=&gs_rfai=
Of course, I'm only guessing.
The reason is that the default in the form designer is to make these Friendly or Private. You need these to be protected or internal. You can just change this and the designer will not overwrite this change.

Visual Studio isn't generating code to instantiate instance of custom control

I've created a custom control using C#, .Net3.5, and Visual Studio 2008.
I'm then adding that control to another control by dragging it from the toolbox.
After doing this when I try to compile I get an error as follows:
Error 1 Warning as Error: Field 'MyNamespace.MyControl._myCustomControl' is never assigned to, and will always have its default value null
Looking at the Designer.cs file this is due to the fact that VS never generates the following code:
this._rgReportGallery = new MyNameSpace.MyCustomControl();
The field itself is there, and the code to add it to the control I'm putting it in, just the instantiation code is missing.
I can add that code manually and everything seems to work, but as soon as the designer.cs file is regenerated it goes missing again.
I've even successfully added a different custom control and had that code get generated.
Does anybody know what could be causing this?
I was having this issue and a reply here helped me find the issue, I was using the "internal" keyword for both my component accessibility and the constructor accessibility. As Brent mentions in a couple posts above mine, VS designer apparently has issues generating an instantiation line with anything except a public constructor, so never make a custom user control where the class accessibility and constructor(s) are anything but "public."
By the way, this occurred for me on Visual Studio 2005, and it doesn't surprise me that it also would happen on Visual Studio 2008. It likely happens on earlier versions of VS as well.
Have you adjusted the constructor of the custom control in any way? The default constructor for controls is a parameterless one, and I know I've had trouble in the past when creating a constructor for my controls that requires parameters. In those cases I always provide two constructors. That way the parameterless one remains for the design mode in VS.
Also make sure your UserControl's constructors are public. I've had issues with internal UserControls and the VS2008 SP1 WinForms designer code generation. It failed to generate the allocation line like you mentioned.
Even though the .Designer.cs files look like simple code, playing around in that code is very risky because it can easily break the Windows Forms Designer. I've heard a lot of people just abandoning the WinForms designer because they say it does not work. It's indeed easy to break, but if you don't try to play in the .Designer.cs file, it works like a charm.
If you have edited the Designer.cs file manually, it might explain why it keeps rewriting itself wrong. It's hard to know how to fix it though. If your UI is not too hard to redo, I would just redo it (whatever is in your main .cs file will be untouched). Otherwise you can look at a similar form to see a model of how exactly it should be formatted (the order of the statements is important).
Do you have the latest service pack for Visual Studio 2008? This usually happens when you don't have the latest service pack.

Categories