1

I have a custom TextBox that does some fancy input handling to ensure only numeric values are entered:

public class NumericTextBox : TextBox
{
    // Some fancy input-handling here
}

I also have a ViewModel which has a public property to store a count:

public class TheViewModel : ReactiveObject
{
    public int Count { get; set; }
}

I have a view that contains a NumericTextBox named Count:

<Window x:Class="MyProject.TheView"
    xmlns:controls="clr-namespace:MyProject.Controls">
    <controls:NumericTextBox
        Name="Count">
</Window>

which binds it to the ViewModel:

public partial class TheView: Window, IViewFor<TheViewModel>
{
    public static readonly DependencyProperty ViewModelProperty = DependencyProperty.Register("ViewModel",
        typeof(TheViewModel),
        typeof(TheView));

    public TheView()
    {
        InitializeComponent();
    }

    /// <summary/>
    object IViewFor.ViewModel
    {
        get { return ViewModel; }
        set { ViewModel = (TheViewModel)value; }
    }

    /// <summary>
    /// The ViewModel corresponding to this specific View. This should be
    ///             a DependencyProperty if you're using XAML.
    /// </summary>
    public TheViewModel ViewModel
    {
        get
        {
            return (TheViewModel) GetValue(ViewModelProperty);
        }
        set
        {
            SetValue(ViewModelProperty, value);
            BindToViewModel();
        }
    }

    private void BindToViewModel()
    {
        this.Bind(ViewModel, vm => vm.Count, v => v.Count.Text);
    }
}

When I try to compile, VS complains about binding to the Count in TheView:

Cannot convert lambda expression to type 'object' because it is not a delegate type

If I switch out the NumericTextBox for a normal TextBox, it works fine. Is there something I'm missing?

MSinger
  • 62
  • 7

1 Answers1

3

You should use x:Name instead of Name.

There is a extensive explanation here about x:Name and Name

It's like your custom control is not inheriting the Name property.. sort of..

Community
  • 1
  • 1
tgpdyk
  • 1,203
  • 9
  • 13