有没有办法全局更改wpf中绑定的默认行为?

有没有办法改变绑定的默认行为,所以我不需要在每个文件框中设置’UpdateSourceTrigger = PropertyChanged’?

这可以通过ControlTemplate或Style完成吗?

也许它更适合覆盖Bindings的默认值,你可以将它用于此目的:

http://www.hardcodet.net/2008/04/wpf-custom-binding-class

然后定义一些CustomBinding类(在构造函数中设置适当的默认值)和MarkupExtension’CustomBindingExtension’。 然后通过以下方式替换XAML中的绑定:

Text =“{CustomBinding Path = Xy …}”

我已成功尝试类似于绑定设置ValidatesOnDataError和NotifyOnValidationError的某些默认值,也适用于您的情况。 问题是您是否愿意更换所有绑定,但您可以自动执行此任务。

否。此行为由FrameworkPropertyMetadata类的DefaultUpdateSourceTrigger处理,该类在注册DependencyProperty时传递。 可以在inheritance的TextBox类和每个绑定中覆盖它,但不能覆盖应用程序中的每个TextBox

就像Pieter提出的那样,我用这样的inheritance类解决了它:

 public class ActiveTextBox:TextBox { public ActiveTextBox() { Loaded += ActiveTextBox_Loaded; } void ActiveTextBox_Loaded(object sender, System.Windows.RoutedEventArgs e) { Binding myBinding = BindingOperations.GetBinding(this, TextProperty); if (myBinding != null && myBinding.UpdateSourceTrigger != UpdateSourceTrigger.PropertyChanged) { Binding bind = (Binding) Allkort3.Common.Extensions.Extensions.CloneProperties(myBinding); bind.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged; BindingOperations.SetBinding(this, TextBox.TextProperty, bind); } } } 

这个帮助方法:

 public static object CloneProperties(object o) { var type = o.GetType(); var clone = Activator.CreateInstance(type); foreach (var property in type.GetProperties()) { if (property.GetSetMethod() != null && property.GetValue(o, null) != null) property.SetValue(clone, property.GetValue(o, null), null); } return clone; } 

有什么建议如何更好地解决?