O'Rooney
O'Rooney

Reputation: 3100

How to assign a property of type Control in XAML - error

I have a WPF user control with a property of type Control, to associate it with another control (usually a textbox) that it will send key presses to. I can assign this property in XAML, and it compiles and works at runtime.

But the IDE shows an error "38 The method or operation is not implemented." on the TargetControl assignment.

<my:NumericButtonsControl 
 x:Name="NumericButtons" 
 TargetControl="{x:Reference Name=DataEntryTextBox}" />

The user control code in question looks like this:

public partial class NumericButtonsControl : UserControl
{
    private Control _TargetControl;

    public Control TargetControl
    {
        get 
        {
            return _TargetControl;
        }
        set
        {
            _TargetControl = value;
        }
    }
 }

Upvotes: 2

Views: 612

Answers (1)

Adi Lester
Adi Lester

Reputation: 25211

When you write a WPF application, you'd probably want to avoid using x:Reference, as it's a feature of XAML 2009 which is only available for loose XAML.

Alternatively, you can use binding and its ElementName property. But for that to work you'll need to make TargetControl a dependency property. Then it will look like this:

C#

public Control TargetControl
{
    get { return (Control)this.GetValue(TargetControlProperty); }
    set { this.SetValue(TargetControlProperty, value); } 
}

public static readonly DependencyProperty TargetControlProperty =
    DependencyProperty.Register("TargetControl",
                                typeof(Control),
                                typeof(NumericButtonsControl),
                                new PropertyMetadata(null));

XAML:

<my:NumericButtonsControl 
    x:Name="NumericButtons" 
    TargetControl="{Binding ElementName=DataEntryTextBox}" />

Upvotes: 3

Related Questions