This project has moved and is read-only. For the latest updates, please go here.

PropertyGrid and UpdateSourceTrigger=PropertyChanged not working

Jun 3, 2015 at 2:13 PM
Edited Jun 3, 2015 at 2:14 PM
Hi,

I got a grid with the following binding:
<tk:PropertyGrid  SelectedObject="{Binding Blubber, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}" >
</tk:PropertyGrid>
in the "Blubber" class I have a public property that throws an exception:

private string validationTest = "hello";
    public string ValidationTest
    {
        get
        {
            return validationTest; 

        }
        set
        {

            if (value != "hello") throw new MyValidiationException("Must be hello");
            else validationTest = value; 
            RaiseEvent("ValidationTest");
        }
    }
Now when I enter some text in the PropertyGrid, the UpdateSourceTrigger=PropertyChanged should update the binding immediately while typeing. So the exception should be thrown immediately. Yet it doesn't!

Is this a bug or does anyone have an explanation for it?
Developer
Jun 3, 2015 at 7:32 PM
Hi,

The Binding is on the SelectedObject, so UpdateSourceTrigger will be activated when the SelectedObject changes. But here, its a property from the SelectedObject that is changed. Here's what you could do :
 <xctk:PropertyGrid x:Name="_propertyGrid"
                            SelectedObject="{Binding Blubber}">
            <xctk:PropertyGrid.EditorDefinitions>
               <xctk:EditorTemplateDefinition TargetProperties="ValidationTest">
                  <xctk:EditorTemplateDefinition.EditingTemplate>
                     <DataTemplate>
                          <!-- one option -->
                          <!--<TextBox Text="{Binding Value, UpdateSourceTrigger=PropertyChanged}" />-->
                          <!-- another option -->
                          <xctk:PropertyGridEditorTextBox Text="{Binding Value, UpdateSourceTrigger=PropertyChanged}" />
                     </DataTemplate>
                  </xctk:EditorTemplateDefinition.EditingTemplate>
               </xctk:EditorTemplateDefinition>
            </xctk:PropertyGrid.EditorDefinitions>
 </xctk:PropertyGrid>
Jun 8, 2015 at 1:04 PM
Hi,

Thanks for your reply. Using the proposed template definition it's working fine. For those who might want to apply it to not only named properties, but based on the type of the property:
    <tk:PropertyGrid.EditorDefinitions>
                <tk:EditorTemplateDefinition >
                    <tk:EditorTemplateDefinition.TargetProperties>
                        <tk:TargetPropertyType Type="{x:Type s:Int32}" />
                        <tk:TargetPropertyType Type="{x:Type s:String}" />
                    </tk:EditorTemplateDefinition.TargetProperties>
                    <tk:EditorTemplateDefinition.EditingTemplate>
                        <DataTemplate>
                            <tk:PropertyGridEditorTextBox Text="{Binding Value, UpdateSourceTrigger=PropertyChanged}" />
                        </DataTemplate>
                    </tk:EditorTemplateDefinition.EditingTemplate>
                </tk:EditorTemplateDefinition>
            </tk:PropertyGrid.EditorDefinitions>
        </tk:PropertyGrid>
Thx again