2008-10-31 17 views
5

OnTabSelectionChanged verilerini doğrulayan bir Silverlight 2 uygulamasına sahibim. Derhal, UpdateSourceTrigger'ın sadece LostFocus'dan daha fazlasına izin vermesini istemeye başladım çünkü bir sekmeyi tıklatmadan sekmeyi tıklatırsanız, LINQ nesnesi doğrulamadan önce güncellenmez. Silverlight Datagrid'teki UpdateSourceTrigger LostFocus için geçici çözüm?

ben odağı başka bir denetime ayarlayarak metin kutularının için soruna çalıştı ve sonra geri OnTextChanged:
Private Sub OnTextChanged(ByVal sender As Object, ByVal e As TextChangedEventArgs) 
    txtSetFocus.Focus() 
    sender.Focus() 
End Sub 

Şimdi, DataGrid içinde hack aynı tür başarmaya çalışıyorum. DataGrid, CellTemplate ve CellEditingTemplate için çalışma zamanında oluşturulan DataTemplates'leri kullanır. TextChanged = "OnTextChanged" ı DataTemplate'de TextBox'a yazmayı denedim, ancak tetiklenmedi.

Herhangi bir fikri olan var mı?

+0

herkes bu sefer bir fikir var mı? –

cevap

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

Bu benim için güzel çalıştı, teşekkürler – McAden

-2

Ben eski bir haber biliyorum ... ama bunu yaparak bu sorunu var:

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

+2

Chu, okuduğum her şeyden SL2 veya SL3'te UpdateSourceTrigger için PropertyChanged seçeneği yok mu? –

+2

PropertyChanged, WPF'deki UpdateSourceTrigger değeridir. – sparks

0
Ben MVVM ve S kullanırken bu aynı sorun koştu

ilverlight 4. Sorun, metin kutusunun odağı kaybettikten sonra bağlamanın kaynağı güncellememesidir, ancak başka bir kontrole odaklanma ayarı hile yapmaz.

İki farklı blog yazısının bir bileşimini kullanarak bir çözüm buldum.

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

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

üstüm sonuçlandı ben SmallWorkarounds.net itibaren bir "SmallWorkaround" ile Patrick Cauldwell en DefaultButtonHub kavramından kod kullanılmış DefaultButtonHub sınıfı için aşağıdaki kodu:

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

} 

Bu Silverlight :)

belgelerine çeşit dahil edilmelidir
İlgili konular