2010-11-02 9 views

Trả lời

6

Có lẽ nó phù hợp hơn để ghi đè giá trị mặc định cho Bindings, bạn có thể sử dụng cái này cho mục đích đó:

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

Sau đó, bạn xác định một số lớp CustomBinding (thiết lập mặc định thích hợp trong constructor) và một MarkupExtension 'CustomBindingExtension'. Sau đó thay thế các ràng buộc trong XAML của bạn bằng một cái gì đó như thế này:

Text = "{CustomBinding Path = Xy ...}"

Tôi đã thử thành công một cái gì đó tương tự với một ràng buộc mà bộ nhất định mặc định cho ValidatesOnDataError và NotifyOnValidationError, cũng sẽ hoạt động trong trường hợp của bạn. Câu hỏi đặt ra là nếu bạn cảm thấy thoải mái khi thay thế tất cả các ràng buộc của mình, nhưng bạn có thể tự động hóa tác vụ này.

1

số Hành vi này được xử lý bởi DefaultUpdateSourceTrigger của lớp FrameworkPropertyMetadata, được thông qua khi đăng ký một DependencyProperty. Có thể ghi đè lên điều này trong một lớp kế thừa của TextBox và mỗi ràng buộc, nhưng không thể cho mỗi TextBox trong ứng dụng.

+0

Tôi sẽ cố gắng giải kiến ​​kiến ​​đó nhờ thông tin. – Cinaird

-1

Giống như Pieter đề nghị tôi giải quyết nó bằng một lớp kế thừa như thế này:

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); 
      } 
     } 
    } 

và helpmethod này:

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; 
     } 

Bất kỳ đề nghị làm thế nào để giải quyết nó tốt hơn?

+0

'DependencyProperty.OverrideMetadata()' là một chiến lược tốt hơn. Bạn có thể sử dụng nó với một 'TextProperty' như' TextBox.TextProperty.OverrideMetadata (typeof (ActiveTextBox), << properties >>) '. –

+0

Một cái gì đó như thế này ?: var defaultMetadata = TextProperty.GetMetadata (typeof (TextBox)); TextProperty.OverrideMetadata (typeof (ActiveTextBox), FrameworkPropertyMetadata mới ( string.Empty, FrameworkPropertyMetadataOptions.Journal | FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, defaultMetadata.PropertyChangedCallback, defaultMetadata.CoerceValueCallback, đúng, System.Windows.Data.UpdateSourceTrigger.PropertyChanged)); – Cinaird