How can I show the font name in the PropertyGrid when SelectedObject is set?

Posted by: info on 28 April 2022, 5:09 pm EST

  • Posted 28 April 2022, 5:09 pm EST


    I'm using a list of FontNames in the PropertyGrid.

    I have ported from C1.WPF.4.5.2 to C1.WPF.PropertyGrid.Ja 6.0.20221.218.

    It worked fine in 4.5.2.
    In 6.0, this BindingError occurs.

    System.Windows.Data Error: 8 : Cannot save value from target back to source. BindingExpression:Path=FontFamily; DataItem='TextBox' (Name=''); target element is 'CustomEditor' (Name=''); target property is 'SelectedItem' (type 'Object') ArgumentException:'System.ArgumentException: ''
  • Marked as Answer

    Replied 28 April 2022, 10:26 pm EST


    Thanks for the sample.

    ComboBox is getting null FontFamily at the time of loading. You can do a workaround by setting a TextBox's default FontFamily when ComboBox's SelectedItem is null.(see code snippet)

    Binding editorBinding = new Binding("SelectedItem") { TargetNullValue = new FontFamily("") };
    editorBinding.Source = this;
    editorBinding.Mode = BindingMode.TwoWay;
    BindingOperations.SetBinding(property.SelectedObject as TextBox, TextBox.FontFamilyProperty, editorBinding);

    if (this.SelectedItem == null)
    (property.SelectedObject as TextBox).FontFamily = new FontFamily("Arial");//default

    Please refer the attached sample for the same:

    Best Regards,
  • Replied 29 April 2022, 12:21 pm EST

    Thank you for your help. It works.
Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels