无法绑定到 ReactiveUI 中覆盖的 WPF TextBox
Can't bind to overridden WPF TextBox in ReactiveUI
我有一个自定义 TextBox,它执行一些奇特的输入处理以确保只输入数值:
public class NumericTextBox : TextBox
{
// Some fancy input-handling here
}
我还有一个 ViewModel,它有一个 public 属性 来存储计数:
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 中的计数:
Cannot convert lambda expression to type 'object' because it is not a delegate type
如果我将 NumericTextBox 换成普通的 TextBox,它就可以正常工作。有什么我想念的吗?
您应该使用 x:Name 而不是名称。
这里有关于x:Name and Name
的详尽解释
这就像您的自定义控件没有继承名称 属性.. 有点..
我有一个自定义 TextBox,它执行一些奇特的输入处理以确保只输入数值:
public class NumericTextBox : TextBox
{
// Some fancy input-handling here
}
我还有一个 ViewModel,它有一个 public 属性 来存储计数:
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 中的计数:
Cannot convert lambda expression to type 'object' because it is not a delegate type
如果我将 NumericTextBox 换成普通的 TextBox,它就可以正常工作。有什么我想念的吗?
您应该使用 x:Name 而不是名称。
这里有关于x:Name and Name
的详尽解释这就像您的自定义控件没有继承名称 属性.. 有点..