0

I use PropertyGrid from WPF Toolkit and using default Editor. I want to set the default value using DefaultValue Attribute, but it doesn't work.

This is my model:

    public class PropertyDummyViewModel : NotifyPropertyChanged
    {
        [DisplayName(@"Double Value Default Editor")]
        [DefaultValue(70)]
        public double DoubleValueDefaultEditor { get; set; }
    }

The XAML of PropertyDummyView:

<Grid ...
    x:Name="PropertyGrid">
    <xctk:PropertyGrid SelectedObject="{Binding Path=DataContext, ElementName=PropertyGrid}" />
</Grid>

And the PropertyGrid is:

    public void ShowDialog()
    {
        var model = new PropertyDummyViewModel();
        var view = new PropertyDummyView() { DataContext = model };
        var window = new Window() { Content = view };
        window.ShowDialog();
    }

When I run it, the value of the property is 0, instead of 70. How to set default value on Wpf Toolkit PropertyGrid control?

Yohanes Nurcahyo
  • 601
  • 8
  • 19
  • 1
    Could it be because the `double` gets initialized automatically with `0.0d` as it is a value type? I don't know anything about the property grid so this is just a guess. Maybe the default value attribute only works with `Nullable` and / or reference types. I that should be the case then you will have to set the default values in the constructor of your model instead. – Frank J Oct 06 '15 at 21:03
  • 1
    Yes it works, but I want cleaner code, without constructor that set the default values and not scroll up down to see the default values. The DefaultValueAttribute as far as I know is used by Winform PropertyGrid. But I think the other clean solution is using C# 6 feature: public double DoubleValueDefaultEditor { get; set; } = 70; I am creating some default property editors as well. So I am wondering how to set default value using DefaultValueAttribute on my own editor. – Yohanes Nurcahyo Oct 06 '15 at 21:40
  • 1
    http://stackoverflow.com/questions/2329868/net-defaultvalue-attribute – jsanalytics Oct 06 '15 at 21:53
  • @jstreet, yes, that is very clear for my goal, "Cannot be used to auto property and just a decorator", so I must just forget about it and just use Frank J suggestion, just using default value. – Yohanes Nurcahyo Oct 06 '15 at 22:07

0 Answers0