【问题标题】:Add IsDirty-Flag on TextBox with DependencyProperty使用 DependencyProperty 在 TextBox 上添加 IsDirty-Flag
【发布时间】: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


    【解决方案1】:

    将“IsDirty”(即依赖属性的 CLR 包装器的名称)传递给 DependencyProperty.Register 方法:

    public static DependencyProperty IsDirtyProperty = DependencyProperty.Register(
        "IsDirty",
        typeof(bool),
        typeof(MyTextField),
        new PropertyMetadata(false));
    

    如果您使用的是 C#6 / Visual Studio 2015 或更高版本,则可以使用 nameof 运算符:

    public static DependencyProperty IsDirtyProperty = DependencyProperty.Register(
        nameof(IsDirty),
        typeof(bool),
        typeof(MyTextField),
        new PropertyMetadata(false));
    

    【讨论】:

    • 您可以使用nameof(IsDirty)添加编译时安全并删除魔术字符串
    • 如果您使用最近的 VisualStudio,您几乎可以通过propdp + TAB + TAB 获得所有这些 - 用于在您的源代码中使用的依赖属性的 sn-p。该类必须从 DependencyObject 派生,但 TextBox 可以。
    【解决方案2】:

    或者你可以override the PropertyMetadata TextProperty:

    public class MyTextBox : TextBox
    {
        static MyTextBox()
        {
            TextProperty.OverrideMetadata(typeof(MyTextBox), new FrameworkPropertyMetadata("", IsDirtyUpdateCallback));
        }
    
        private static void IsDirtyUpdateCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d is MyTextBox tb && e.NewValue != e.OldValue && e.Property == TextProperty)
            {
                tb.IsDirty = (string)e.OldValue != (string)e.NewValue;
            }
        }
    
    
    
        public bool IsDirty
        {
            get { return (bool)GetValue(IsDirtyProperty); }
            set { SetValue(IsDirtyProperty, value); }
        }
    
        public static readonly DependencyProperty IsDirtyProperty =
            DependencyProperty.Register("IsDirty", typeof(bool), typeof(MyTextBox), new PropertyMetadata(true));
    }
    

    自动设置您的 IsDirty :o) 不止一种方式到罗马。但是对于您的问题,那有点用大炮射击小鸟(德国谚语)

    【讨论】:

    • 首先非常感谢。不幸的是,绑定不起作用。我的TextboxGridView 内,与ObservableCollection 绑定。 IsDirtyProperty -DP 设置正确。
    • @Marcel:你试过我的建议了吗?
    • 绑定?您的问题是关于 XamlParseException,不是吗?
    • 没错,异常消失了,但现在绑定不起作用
    猜你喜欢
    • 2015-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多