This thread looks to be a little on the old side and therefore may no longer be relevant. Please see if there is a newer thread on the subject and ensure you're using the most recent build of any software if your question regards a particular product.
This thread has been locked and is no longer accepting new posts, if you have a question regarding this topic please email us at support@mindscape.co.nz
|
Hi,
I am experiencing a strange behaviour when using WPF property grid in a small test application. Maybe you can help me with this: I am using a simple ListBox, populating some instances of a test class "Person". /// <summary> /// Simple test class /// </summary> public class Person { public String Vorname { get; set; } public String Name { get; set; } public Int16 Age { get; set; } } I also added a property grid to my window, where SelectedObject property is bound to SelectedItem of ListBox using WPF data binding. <ListBox Margin="3" Grid.Row="2" x:Name="lstPersonen"> <ListBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <TextBlock Margin="3" Text="{Binding Path=Firstname}"/> <TextBlock Margin="3" Text="{Binding Path=Name}"/> <TextBlock Margin="3" Text="("/> <TextBlock Margin="3" Text="{Binding Path=Age}"/> <TextBlock Margin="3" Text=")"/> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </Grid> <ms:PropertyGrid Grid.Column="1" SelectedObject="{Binding ElementName=lstPersonen, Path=SelectedItem}" Margin="3" Name="propGrid" /> Everything seems to work fine, until I set null to SelectedObject. After that, property grid is not updating when a new item becomes selected in ListBox (it remains empty). However, if I assign a new instance of "Person" to SelectedObject in source code, it is working, so it seems, that WPF data binding is broken in this situation somehow... When I try to set SelectedObject in source code (without using any WPF data binding), everything works fine. Is there an error in my approach? Thanks in advance, Patrick |
|
|
(There is a little mistake in my post: Property "Vorname" in Person is called "Vorname" in XAML-Code too, but I wanted to change it to "Firstname" before posting here - sorry about that ) |
|
|
Let me just restate what I think you are doing in case I have misunderstood: * You declare the property grid in XAML and data-bind the SelectedObject property as shown in your code snippet (and this works). * Then from code you set propGrid.SelectedObject = null; and after this the property grid no longer updates. If so, this is expected behaviour for (one-way) WPF data binding. To observe this, try adding, say, a ContentControl to your form, with its Content bound to lstPersonen.SelectedItem (same as the PropertyGrid.SelectedObject). You will see that the ContentControl tracks the selection just as the PropertyGrid does. Then set contentControl.Content = null from code. You will see that the ContentControl stops tracking the selection. Effectively the imposition of a specific value has overridden the binding. Note that this is specific to one-way data binding. In a two-way binding, setting the value to null doesn't actually set a local value: it pushes null back to the binding source. If you modify your SelectedObject binding to specify Mode=TwoWay, you will find that setting propGrid.SelectedObject = null now clears the property grid and clears the selection in lstPersonen -- and that now, when a new item becomes selected in lstPersonen, the grid does update to reflect it! If I've not understood your scenario, could you clarify where you are setting the null please, and we will investigate -- thanks! |
|
|
Thank you for the quick response. |
|
|
Ah, sorry for misunderstanding and thanks for the detailed repro instructions. I have reproduced and fixed the problem, and the fix will be included in nightly builds dated 24 June 2009 and above, available from about 1430 GMT. Thanks for letting us know about this! |
|
|
Would you mind explaining what you had to do to fix this? We are seeing the exact same problem with our databinding when setting the Path property to access an object relative to the control itself. After I have changed the source to what would evaluate as null in the Path, none of the controls update any longer. Thanks |
|
|
It was an internal issue with cascading changes between the SelectedObject and SelectedObjects properties and the way the grid internally represents multiple selection. You shouldn't need to do anything in your code to fix the problem other than update to the current nightly build (available from the Downloads page). |
|