Question

I've created a user control that works together with a RichTextBox. The user control needs to work with the RichTextBox, so I've created a dependency property like this:

    public static DependencyProperty RichTextEditControlProperty;

    static RichTextBoxToolbar()
    {
        RichTextEditControlProperty = DependencyProperty.Register("RichTextEditControl", typeof(RichTextBox), typeof(RichTextBoxToolbar), new FrameworkPropertyMetadata(null));
    }

    public RichTextBoxToolbar()
    {
        InitializeComponent();
    }

    public RichTextBox RichTextBoxControl
    {
        get { return (RichTextBox)GetValue(RichTextEditControlProperty); }
        set { SetValue(RichTextEditControlProperty, value); }
    }

However, I do not know how I can set this property in XAML. I've tried like this

my:RichTextBoxToolbar Margin="0" DockPanel.Dock="Top" 
                      RichTextBoxControl="{Binding , ElementName=headerText}"

where headerText is the name of the RichTextBox.

But get the following error:

A 'Binding' cannot be set on the 'RichTextBoxControl' property of type 'RichTextBoxToolbar'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject. C:\Users\stefan.CORP\Documents\Visual Studio

Any ideas?

Was it helpful?

Solution

Property name in static DependencyProperty identifier is incorrect.

DependencyProperty.Register("RichTextEditControl", typeof(RichTextBox), 
              typeof(RichTextBoxToolbar), new FrameworkPropertyMetadata(null));

It should be RichTextBoxControl instead of RichTextEditControl.

OR

Rename wrapper CLR property to name RichTextEditControl:

public RichTextBox RichTextEditControl
{
    get { return (RichTextBox)GetValue(RichTextEditControlProperty); }
    set { SetValue(RichTextEditControlProperty, value); }
}

Registered property name and wrapper property name should be in sync always.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top