繁体   English   中英

无法绑定到ReactiveUI中重写的WPF TextBox

[英]Can't bind to overridden WPF TextBox in ReactiveUI

我有一个自定义TextBox,它执行一些奇特的输入处理,以确保只输入数值:

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

我还有一个ViewModel,它有一个存储计数的公共属性:

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

我有一个包含名为Count的NumericTextBox的视图:

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

它将它绑定到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);
    }
}

当我尝试编译时,VS抱怨绑定到TheView中的Count:

无法将lambda表达式转换为类型“object”,因为它不是委托类型

如果我为正常的TextBox切换NumericTextBox,它可以正常工作。 有什么我想念的吗?

您应该使用x:Name而不是Name。

这里有关于x:名称和名称的广泛解释

这就像你的自定义控件没有继承Name属性..排序..

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM