Updating Values for Certain Property Types in PropertyGrid

Grids for WPF Forum

Posted 6 months ago by BenjaminLopVic - France
Version: 24.1.3
Platform: .NET 4.8
Environment: Windows 11 (64-bit)
Avatar

Hello,

I'm facing an issue with updating values for certain property types in a PropertyGrid. I use a PropertyGrid to display information about a selection, and for that, I've set up a customDataFactory and a customPropertyModel that updates the values when my selection changes. This works except for  certain property types :

  • bool : updates as excpected
  • int and double : don't update unless I define a custom PropertyEditor in the "Editor" attribute.
  • string : never updates

I've reproduced this issue with a simplified model, which you can find on GitHub at the following link: PropertyEditorNotUpdating

In this project, only the bool and the int property with a custom PropertyEditor update when clicking "Change!", but all properties should update.

Thanks.

Benjamin.

Comments (4)

Posted 6 months ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hello,

Thank you for the sample.  I noticed that in your TestVM class, you are updating the field values for your properties in all their getters, which is not good for MVVM.  You also don't update the property values in the setters.  If you update the field value of the property, you have to raise the INotifyPropertyChanged.PropertyChanged event or PropertyGrid will not know when to update its UI.

Instead of this:

public int IntProperty {
    get => intValue++;
    set => OnPropertyChanged(nameof(IntProperty));
}

Your properties should look like this:

public int IntProperty {
    get => intValue;
    set {
        intValue = value;
        OnPropertyChanged(nameof(IntProperty));
    }
}

If you do that for each property and then use this kind of code for your button, everything works great:

private void Button_Click(object sender, RoutedEventArgs e) {
    TestDataObject.BoolProperty = !TestDataObject.BoolProperty;
    TestDataObject.IntProperty++;
}

[Modified 6 months ago]


Actipro Software Support

Posted 6 months ago by BenjaminLopVic - France
Avatar

Alright, this VM works, but in my actual case, what’s triggered by the button here doesn’t modify the properties—I want it to invalidate them so they get refreshed, just like it currently works with the bool. Do you know why it behaves this way with bool natively but not with other types?

Answer - Posted 6 months ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hello,

I looked at the sample project again and apologize that I missed where in CustomPropertyModel you were calling:

NotifyPropertyChanged(nameof(Value));

That is why a couple of the value editors updated.  The other value editors bind their UI to "ValueAsString" instead of "Value".  If you add this, then they all update:

NotifyPropertyChanged(nameof(ValueAsString));

However I would suggest that you replace both those lines in your EventTriggerer_EventTriggered event handler with this single line instead, which ends up doing both of those along with other things like updating any child properties, etc.

Refresh(PropertyRefreshReason.ValueChanged);

In general, it's often better to design the view models to be properly observable where the getter does not modify the value, and the setter does and raises INotifyPropertyChanged.PropertyChanged.  But if you have a case here where that typical observable design doesn't work, then using that Refresh call will do what you need.


Actipro Software Support

Posted 6 months ago by BenjaminLopVic - France
Avatar

Hello,

Thanks, that’s exactly what I was looking for! I tried to implement a truly observable VM, but as you mentioned, I found that in my case it I prefer this approach.

Thanks again,
Benjamin.

The latest build of this product (v24.1.5) was released 1 month ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.