2008-10-31 25 views
5

Tôi có ứng dụng Silverlight 2 xác thực dữ liệu OnTabSelectionChanged. Ngay lập tức tôi bắt đầu có nhu cầu UpdateSourceTrigger cho phép nhiều hơn chỉ là LostFocus bởi vì nếu bạn nhấp vào tab mà không tabbing tắt của một điều khiển đối tượng LINQ không được cập nhật trước khi xác nhận.Giải pháp cho UpdateSourceTrigger LostFocus trên Silverlight Datagrid?

tôi làm việc xung quanh vấn đề cho textbox bằng cách thiết lập tập trung vào kiểm soát khác và sau đó trở lại OnTextChanged:

Private Sub OnTextChanged(ByVal sender As Object, ByVal e As TextChangedEventArgs) 
    txtSetFocus.Focus() 
    sender.Focus() 
End Sub 

Bây giờ tôi đang cố gắng để đạt được cùng một loại xe cho thuê trong một DataGrid. DataGrid của tôi sử dụng DataTemplates được tạo ra trong thời gian chạy cho CellTemplate và CellEditingTemplate. Tôi đã thử viết TextChanged = "OnTextChanged" vào TextBox trong DataTemplate, nhưng nó không được kích hoạt.

Bất kỳ ai có ý tưởng nào?

+0

có ai có ý tưởng về điều này không? –

Trả lời

6

You can do it with a behavior applied to the textbox too

// xmlns:int is System.Windows.Interactivity from System.Windows.Interactivity.DLL) 
// xmlns:behavior is your namespace for the class below 
<TextBox Text="{Binding Description,Mode=TwoWay,UpdateSourceTrigger=Explicit}"> 
    <int:Interaction.Behaviors> 
     <behavior:TextBoxUpdatesTextBindingOnPropertyChanged /> 
    </int:Interaction.Behaviors> 
</TextBox> 


public class TextBoxUpdatesTextBindingOnPropertyChanged : Behavior<TextBox> 
{ 
    protected override void OnAttached() 
    { 
     base.OnAttached(); 

     AssociatedObject.TextChanged += new TextChangedEventHandler(TextBox_TextChanged); 
    } 

    protected override void OnDetaching() 
    { 
     base.OnDetaching(); 

     AssociatedObject.TextChanged -= TextBox_TextChanged; 
    } 

    void TextBox_TextChanged(object sender, TextChangedEventArgs e) 
    { 
     var bindingExpression = AssociatedObject.GetBindingExpression(TextBox.TextProperty); 
     bindingExpression.UpdateSource(); 
    } 
} 
+0

Điều này có hiệu quả đối với tôi, cảm ơn bạn – McAden

-2

Tôi biết đó là tin tức cũ ... nhưng tôi đã nhận việc này bằng cách thực hiện điều này:

Text = "{Binding Path = newQuantity, UpdateSourceTrigger = PropertyChanged}"

bài
+2

Chu, từ tất cả mọi thứ tôi đã đọc không có sự lựa chọn PropertyChanged cho UpdateSourceTrigger trong SL2 hoặc SL3.? –

+2

PropertyChanged là giá trị cho UpdateSourceTrigger trong WPF. – sparks

0

tôi chạy vào vấn đề này cùng sử dụng MVVM và S ilverlight 4. Vấn đề là ràng buộc không cập nhật nguồn cho đến sau khi hộp văn bản mất tập trung, nhưng việc đặt tiêu điểm trên một điều khiển khác không làm việc lừa.

Tôi đã tìm thấy giải pháp bằng cách kết hợp hai bài đăng trên blog khác nhau. Tôi đã sử dụng mã từ khái niệm DefaultButtonHub Patrick Cauldwell, với một "SmallWorkaround" từ SmallWorkarounds.net

http://www.cauldwell.net/patrick/blog/DefaultButtonSemanticsInSilverlightRevisited.aspx

www.smallworkarounds.net/2010/02/elementbindingbinding-modes.html

sự thay đổi của tôi dẫn trong đoạn mã sau cho lớp DefaultButtonHub:

public class DefaultButtonHub 
{ 
    ButtonAutomationPeer peer = null; 

    private void Attach(DependencyObject source) 
    { 
     if (source is Button) 
     { 
      peer = new ButtonAutomationPeer(source as Button); 
     } 
     else if (source is TextBox) 
     { 
      TextBox tb = source as TextBox; 
      tb.KeyUp += OnKeyUp; 
     } 
     else if (source is PasswordBox) 
     { 
      PasswordBox pb = source as PasswordBox; 
      pb.KeyUp += OnKeyUp; 
     } 
    } 

    private void OnKeyUp(object sender, KeyEventArgs arg) 
    { 
     if (arg.Key == Key.Enter) 
      if (peer != null) 
      { 
       if (sender is TextBox) 
       { 
        TextBox t = (TextBox)sender; 
        BindingExpression expression = t.GetBindingExpression(TextBox.TextProperty); 
        expression.UpdateSource(); 
       } 
       ((IInvokeProvider)peer).Invoke(); 
      } 
    } 

    public static DefaultButtonHub GetDefaultHub(DependencyObject obj) 
    { 
     return (DefaultButtonHub)obj.GetValue(DefaultHubProperty); 
    } 

    public static void SetDefaultHub(DependencyObject obj, DefaultButtonHub value) 
    { 
     obj.SetValue(DefaultHubProperty, value); 
    } 

    // Using a DependencyProperty as the backing store for DefaultHub. This enables animation, styling, binding, etc... 
    public static readonly DependencyProperty DefaultHubProperty = 
     DependencyProperty.RegisterAttached("DefaultHub", typeof(DefaultButtonHub), typeof(DefaultButtonHub), new PropertyMetadata(OnHubAttach)); 

    private static void OnHubAttach(DependencyObject source, DependencyPropertyChangedEventArgs prop) 
    { 
     DefaultButtonHub hub = prop.NewValue as DefaultButtonHub; 
     hub.Attach(source); 
    } 

} 

này cần được đưa vào một số loại tài liệu cho Silverlight :)

Các vấn đề liên quan