c# - 无法绑定(bind)到 ReactiveUI 中覆盖的 WPF TextBox

标签 c# wpf xaml mvvm reactiveui

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

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

我还有一个 ViewModel,它有一个公共(public)属性来存储计数:

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

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

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

将它绑定(bind)到 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 提示绑定(bind)到 TheView 中的计数:

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

如果我将 NumericTextBox 换成普通的 TextBox,它工作正常。有什么我想念的吗?

最佳答案

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

这里有关于 x:Name and Name 的详尽解释

这就像您的自定义控件没有继承 Name 属性..有点..

关于c# - 无法绑定(bind)到 ReactiveUI 中覆盖的 WPF TextBox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33793271/

相关文章:

c# - Box2D body 速度上限?

c# - 从哪里捕获自定义 ModelBinder 的异常

wpf - 在上下文菜单命令参数 mvvm 中获取行

c# - 奇怪的 WPF 错误

c# - 如何修复从Yahoo下载报价文件并插入数据库的脚本任务代码?

c# - 通过分页检查复选框

c# - WPF 错误 : "Cannot find governing FrameworkElement or Framework..."

wpf - 是否可以在 WPF TabControl 中左对齐标题?

c# - WPF - 程序运行时图像不显示

c# - 在 Windows Phone 8.1 应用程序中过滤 ListView