WPF : Define binding's default

标签 wpf xaml binding templates default

在 WPF 中,我希望能够对默认情况下如何应用我的绑定(bind)进行模板化。

例如,我想写:

Text="{Binding Path=PedigreeName}"

但就好像我输入了:
Text="{Binding Path=PedigreeName, Mode=TwoWay, UpdateSourceTrigger=LostFocus, NotifyOnValidationError=True, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}" 

任何的想法 ?

谢谢,
  • 帕特里克
  • 最佳答案

    使用 DependencyProperty.Register 的重载之一采用 PropertyMetadata。传递 FrameworkPropertyMetadata 的实例并设置其属性。

    public class Dog {
        public static readonly DependencyProperty PedigreeNameProperty =
            DependencyProperty.Register("PedigreeName", typeof(string), typeof(Dog),
                new FrameworkPropertyMetadata() {
                    BindsTwoWayByDefault = true,
                    DefaultUpdateSourceTrigger = UpdateSourceTrigger.LostFocus
                }
            );
    

    我没有立即看到设置 NotifyOnValidationError、ValidatesOnDataErrors 或 ValidatesOnExceptions 的默认值的方法,但我没有充分使用它来确定要查找的内容;他们可能在那里。

    关于WPF : Define binding's default,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1094509/

    相关文章:

    wpf - 如何在 DataBound 控件列表中依次启动 Storyboard

    c# - 用户控件不呈现

    xaml - Windows Phone - 禁用按钮背景

    c# - Wpf 文本框未从 View 模型更新

    WPF MVVM ListBox/ComboBox ItemsSource 不会从 ViewModel 更新 UI

    c# - 正确处理 MVVM 父级中子级引发的事件

    c# - 如何在 WPF 中更快地加载大型 Camera RAW 文件 (NEF)

    c# - 绑定(bind)到 getter 属性时,Xamarin IsVisible 不会动态更新

    c# - 当按下 'Enter' 键而不是提交时移动到下一个文本框 (Windows Phone)

    c# - 为什么我的 WPF 绑定(bind)不起作用?