2008-10-31 10 views
5

J'ai une application Silverlight 2 qui valide les données OnTabSelectionChanged. Immédiatement j'ai commencé à souhaiter que UpdateSourceTrigger permette plus que simplement LostFocus car si vous cliquez sur l'onglet sans tabulation d'un contrôle, l'objet LINQ n'est pas mis à jour avant la validation.Solution de contournement pour UpdateSourceTrigger LostFocus sur Datagrid Silverlight?

J'ai travaillé autour de la question des zones de texte en mettant l'accent à un autre contrôle, puis de nouveau OnTextChanged:

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

Maintenant, je suis en train d'accomplir le même genre de bidouille dans un DataGrid. Mon DataGrid utilise des DataTemplates générés lors de l'exécution pour CellTemplate et CellEditingTemplate. J'ai essayé d'écrire TextChanged = "OnTextChanged" dans le TextBox dans le DataTemplate, mais il n'est pas déclenché.

Vous avez des idées?

+0

ce que quelqu'un a une idée sur celui-ci? –

Répondre

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

Cela a fonctionné magnifiquement pour moi, merci – McAden

-2

Je sais qu'il est de vieilles nouvelles ... mais je me suis autour de cela en faisant ceci:

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

+2

Chu, de tout ce que j'ai lu il n'y a pas de choix PropertyChanged pour le UpdateSourceTrigger dans SL2 ou SL3. –

+2

PropertyChanged est la valeur de UpdateSourceTrigger dans WPF. – sparks

0

je suis tombé sur ce même problème en utilisant MVVM et S Ilverlight 4. Le problème est que la liaison ne met pas à jour la source tant que la zone de texte n'a pas été mise au point, mais que la mise au point sur un autre contrôle ne fait pas l'affaire.

J'ai trouvé une solution en utilisant une combinaison de deux billets de blog différents. J'ai utilisé le code de concept de Patrick Cauldwell DefaultButtonHub, avec un "SmallWorkaround" de SmallWorkarounds.net

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

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

Mon changement a entraîné dans le code suivant pour la classe 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); 
    } 

} 

Cela devrait être inclus dans une sorte de documentation pour Silverlight :)

Questions connexes