Visual Studio Development Bookmark and Share   
 index > Visual Studio WPF Designer > Why does VS 2010's designer clear my property value editor's properties when it is dismissed?
 

Why does VS 2010's designer clear my property value editor's properties when it is dismissed?

(This started as a Connect post but I decided it seems like I should find out if I'm doing something daft before blaming .NET. It requires a lot of code, and Connect lets you attach projects. I see no option to do this on these forums, so I will link to a .zip file containing the project on my hosting.)

I'm experimenting with various ideas for the design-time experience for some WPF controls. One feature I'm trying to implement is the display of a graphical preview for enumeration values. My first attempt at this failed due to some behaviors in the design environment that I'm not certain are expected. Here's a description of the attached project and the behaviors I don't understand.

PreviewItem is a class that represents an enumeration value; it has properties for the value and the image that represents it.PreviewItemCollection is a collection class that contains a PreviewItem for each valid enumeration value; it relies on an implementation of PreviewItemFactory. PreviewItemFactory is responsible for generating a collection based on an enumeration that contains one PreviewItem per enumeration value. EnumPreview is a UserControl that maintains a PreviewItemCollection and displays each PreviewItem in a ComboBox. It contains logic for representing the selected value as the enumeration value (not a PreviewItem). To facilitate this, when the SelectedItem property is changed the correct PreviewItem is selected, and when the ComboBox's selection changes the SelectedItem property is updated.

The above classes were created to be a framework for creation of extended property value editors for enumeration values with previews. To create a preview, the user derives a PreviewItem and implements logic to generate the image for each value. Then, a PreviewItemFactory is derived and the user implements logic to create PreviewItems for each value. Finally, a PreviewItemCollection is derived and configured to use the appropriate PreviewItemFactory. The extended property value editor for the type uses an EnumPreview that is given the derived PreviewItemFactory. An example of this implementation is in the attached project. ControlLibrary contains the Character enumeration, and CustomButton has a Character property. TestApp consists of a window with nothing but a CustomButton on it. ControlLibrary.Design implements the framework described above and uses it to implement an editor for the CustomButton.Character property.

This initially works, but strange things happen during execution. I can select the CustomButton and view my property editor in the property grid, but when I make a selection a NullReferenceException is thrown. I've debugged the exception and it looks like what happens is when the control is dismissed, something decides to clear PreviewItems to null. This triggers a change handler that sets the combo box's ItemsSource to the new collection, which triggers code that updates SelectedItem, which triggers code that tries to update the combo box's selected item, which tries to look through the preview items and throws the exception because PreviewItems is now null.

I'll agree that this is a bug in my code: null is a valid value and EnumPreview.SetComboSelectedItem() is not prepared to handle it. However, SelectedItem is bound to the ModelProperty; this means that under the current behavior my property editor cannot work. The user makes a selection and this sets SelectedItem to what I want. When the control is dismissed, the preview items are removed and SelectedItem is set to null; this means the designer doesn't use the user's selected value. I could create a secondary property that is only set by direct user input and bind to that, but it seems cumbersome.

I pursued solutions to this problem for a while but found none. I've since moved to using a different approach that is not as general or simple to implement. I'd like to find a way to make this one work.

Why is my property being reset to null? Is there something wrong with my approach? Is it a bug?

Project File (FailedEnumPreview.zip)
opellegr  Friday, September 18, 2009 6:37 PM
Hi opellegr

Please can you open a Connect bug on this and let us have the Connect ID of the bug so we can grab it? We need to track this issue better than a forum post would permit, and a Connect bug will be a better way of doing that.

Thanks

Mark Wilson-Thomas



Mark Wilson-Thomas - Program Team - WPF & SL Designer for Visual Studio - posts are provided 'as-is'
Mark Wilson-Thomas  Thursday, September 24, 2009 4:28 PM
Mark,

I've submitted a connect bug for this issue. The ID is 491933 and here's a handy link .
opellegr  Thursday, September 24, 2009 8:16 PM
Perfect! Thanks for doing that - and sorry to have to ask (I would prefer to have been able to convert this thread to a Connect bug directly, and am pressing for that feature in Connect).

We'll respond on the Connect Bug.

Mark
Mark Wilson-Thomas - Program Team - WPF & SL Designer for Visual Studio - posts are provided 'as-is'
Mark Wilson-Thomas  Thursday, September 24, 2009 8:44 PM

You can use google to search for other answers

Custom Search

More Threads

• Application for Blackberry
• Property Windows and XAML editor do not cooperate correctly
• WPF ListView Skins/Styles
• WPF how to add a control to a custom control during design time
• Making a Datgrid into a User Control loses the data
• Windows Presentation Foundation Samples - Data Binding Demo
• How can I change properties for elements in custom control?
• Unable to add controls from tool box to wpf designer
• Sending list as ConverterParameter
• The Split View Bar is missing in the XAML view