Binding PropertyGrid at ViewModel class?

Grids for WPF Forum

Posted 8 years ago by kyy8080
Version: 11.1.0542
Avatar
Hi all,
i want to show the properties of my custom control in propertyGrid, but not all properties, only the properties from ViewModel-Class of my custom control. How can i bind a propertyGrid at ViewModel-Class of my custom control?

Best regards

Comments (6)

Posted 8 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hello,

I'm not sure exactly what you are asking. Do you want to display the properties of your custom control, but filter what is displayed based on your view-model? If so, I'm assuming your view-model has a list of properties you'd like displayed. Is that correct?


Actipro Software Support

Posted 8 years ago by kyy8080
Avatar
Hallo,

that's right! I want to diplay only the properties from viewmodel. Can i use that:

var _uc = sender as UserControl;
if (_uc != null)
{
PropertiesUserControl.SelectedObject = null;
PropertiesUserControl.SelectedObject = (object)_uc.DataContext;
?
It seems to work... But my usercontrol don't react on the chages in propertygrid. What must i do?

Best regards
Vadim
Posted 8 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Vadim,

You would need to set PropertyGrid.SelectedObject to your control and set PropertyGrid.DataFilter to a custom IDataFilter. Your custom IDataFitler could then filter out properties based on whatever logic you want, such as whether it's defined by a referenced view-model.

The Filters (Custom) QuickStart shows a simple custom filter. Keep in mind that filters are applied to both properties and categories. So in your case, you'd probably want to only filter properties. The IDataAccessor passed into the filter with be either an ICategoryDataAccessor (categories) or an IPropertyDataAccessor (properties).


Actipro Software Support

Posted 8 years ago by kyy8080
Avatar
Hallo,

thank you for the answer. But this has not solved the problem. I bind the properties of viewmodel to my user control via databinding and these properties are not displayed by usercontrol. I can't filter this properties.
Here my code. I bind the StartValues and EndValues at properties AlarmMinWertMin and AlarmMinWertMax from my viewmodel:

...
<gauge:CircularRange StartValue="{Binding AlarmMinWertMin, Mode=TwoWay,    UpdateSourceTrigger=PropertyChanged}"                                                 EndValue="{Binding AlarmMinWertMax, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
StartExtent="5" EndExtent="5" HasDropShadow="False"  ScalePlacement="Overlay" x:Name="ALAlarmMin1">
...
Here is my viewmodel:

    public class ControlViewModel: ViewModelBase
    {
        double _alarmMinWertMin;
        double _alarmMinWertMax;

        ....
        [Category("Grenzwerte")]
        [Description("Min. Alarm min")]
        public double AlarmMinWertMin
        {

            get { 
                   //Get the data from database
                   .....
                   return _alarmMinWertMin;
            }
            set
            {
                ...
                RaisePropertyChanged("AlarmMinWertMin");
            }
        }

        [Category("Grenzwerte")]
        [Description("Min. Alarm max")]
        public double AlarmMinWertMax
        {

            get {
                   //Get the data from database
                   .....
                   return _alarmMinWertMax; 
            }
            set
            {
                 ...
                RaisePropertyChanged("AlarmMinWertMax");
            }
        }
}
If i set PropertyGrid.SelectedObject to my control i can see all properties but not StartValue and EndValue...

Best regards
Vadim
Posted 8 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hello,

If you are setting the SelectedObject to an instance of CircularRange, then there isn't an easy way to see what properties on that are bound to your View Model and only display those. The PropertyGrid in general deals with the underlying values, not the bindings if there are any. The WinForms PropertyGrid Functionality Demo shows how you can get a BindingExpression from an IPropertyDataAccessor, which is effectively:
IPropertyDataAccessor propertyDataAccessor = ... ;

PropertyDescriptorDataAccessor propertyDescriptorDataAccessor = propertyDataAccessor as PropertyDescriptorDataAccessor;
if (propertyDescriptorDataAccessor == null) {
    PropertyGridDataAccessorItem item = propertyDataAccessor as PropertyGridDataAccessorItem;
    if (item != null)
        propertyDescriptorDataAccessor = item.DataContext as PropertyDescriptorDataAccessor;
}

if (propertyDataAccessor != null && propertyDataAccessor.PropertyDescriptor != null) {
    // Determine if the dependency property in the object is data bound
    DependencyObject depObj = propertyDataAccessor.Target as DependencyObject;
    DependencyPropertyDescriptor depProp = DependencyPropertyDescriptor.FromProperty(propertyDataAccessor.PropertyDescriptor);
    if (depObj != null && depProp != null) {
        BindingExpression bindingExpression = BindingOperations.GetBindingExpression(depObj, depProp.DependencyProperty);
        // TODO: Check for View Model in DataItem property
    }
}
You could perform this in the custom DataFilter to remove properties that are not bound to your View Model.

Otherwise, you could simply set SelectedObject to an instance of your View Model. That would display all the properites of your View Model, but they may not all be bound to the CircularRange.


Actipro Software Support

Posted 8 years ago by kyy8080
Avatar
Thank you very much
The latest build of this product (v2019.1 build 0683) 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.