【发布时间】:2017-10-11 15:00:31
【问题描述】:
我有一个问题,我有一个无法扩展的现有模型对象。实际问题有点复杂,所以我试着分解一下。
我想使用依赖属性扩展TextBox 以指示文本已更改。所以我想出了以下解决方案:
public class MyTextField : TextBox
{
public MyTextField()
{
this.TextChanged += new TextChangedEventHandler(MyTextField_TextChanged);
}
private void MyTextField_TextChanged(object sender, TextChangedEventArgs e)
{
IsDirty = true;
}
public static DependencyProperty IsDirtyProperty = DependencyProperty.Register(
"IsDirtyProperty",
typeof(bool),
typeof(MyTextField),
new PropertyMetadata(false));
public bool IsDirty
{
get { return (bool)GetValue(IsDirtyProperty); }
set { SetValue(IsDirtyProperty, value); }
}
}
XAML:
<my:MiaTextField Text="{Binding Barcode}" IsDirty="{Binding IsDirty}"/>
因此,如果我更改 TextBox 中的文本,isDirty 属性应更改为 true。
但是我得到了一个System.Windows.Markup.XamlParseException:绑定只能为“DependencyObject”的“DependencyProperty”设置。
【问题讨论】:
标签: c# wpf dependency-properties