小编Mar*_*cel的帖子

使用DependencyProperty在TextBox上添加IsDirty-Flag

我有问题,我有一个我无法扩展的现有模型对象.实际问题有点复杂,所以我试图将其分解.

我想扩展一个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); }
    }
}
Run Code Online (Sandbox Code Playgroud)

XAML:

<my:MiaTextField Text="{Binding Barcode}" IsDirty="{Binding IsDirty}"/>
Run Code Online (Sandbox Code Playgroud)

因此,如果我更改了文本TextBox,则isDirty属性应更改为true.但我得到了一个System.Windows.Markup.XamlParseException:绑定只能设置"DependencyObject"的"DependencyProperty".

c# wpf dependency-properties

0
推荐指数
1
解决办法
118
查看次数

标签 统计

c# ×1

dependency-properties ×1

wpf ×1