【问题标题】:Is there a way to specify a custom dependency property's default ValidatesOnDataErrors?有没有办法指定自定义依赖属性的默认 ValidatesOnDataErrors?
【发布时间】:2017-01-17 10:55:36
【问题描述】:

有没有办法为我的自定义 DependencyProperty 将 ValidatesOnDataErrors 设置为 True,这样我每次绑定到它时都不必这样做? this 中的内容。

public static readonly DependencyProperty TextProperty =
        DependencyProperty.Register(nameof(Text), typeof(string),
            typeof(ErrorTextEdit), new FrameworkPropertyMetadata(null)
            {
                BindsTwoWayByDefault = true,
                DefaultUpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged,
                // Something Here maybe???
            });

    public string Text
    {
        get { return (string) GetValue(TextProperty); }
        set { SetValue(TextProperty, value); }
    }

如果有帮助,我的控件也可以从 TextBox 继承。

【问题讨论】:

    标签: c# wpf data-binding dependency-properties


    【解决方案1】:

    不,恐怕不会。这是 Binding 类的属性,而不是依赖属性。您可以将 XAML 标记中的 {Binding} 标记扩展替换为为您设置 ValidatesOnDataErrors 属性的自定义标记扩展:

    How can i change the default values of the Binding Option in WPF?

    或者创建一个自定义绑定类:

    public class CustomBinding : Binding
    {
        public CustomBinding(string path)
            :base(path)
        {
            this.NotifyOnValidationError = true;
        }
    }
    

    用法:

    <TextBlock Text="{local:CustomBinding Name}" />
    

    【讨论】:

    • 这是一种非常优雅的方式,我没有考虑过。
    猜你喜欢
    • 2011-02-09
    • 2011-01-31
    • 1970-01-01
    • 1970-01-01
    • 2020-09-28
    • 1970-01-01
    • 2013-10-16
    • 1970-01-01
    相关资源
    最近更新 更多