2008-10-31 31 views
5

Tengo una aplicación Silverlight 2 que valida los datos OnTabSelectionChanged. Inmediatamente comencé a desear que UpdateSourceTrigger permitiera algo más que LostFocus porque si hace clic en la pestaña sin quitar el control de un control, el objeto LINQ no se actualiza antes de la validación.Solución alternativa para UpdateSourceTrigger LostFocus en Silverlight Datagrid?

trabajé en torno al tema de cuadros de texto mediante el establecimiento de foco a otro control y luego de vuelta OnTextChanged:

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

Ahora estoy tratando de lograr el mismo tipo de corte dentro de una cuadrícula de datos. My DataGrid usa DataTemplates generadas en tiempo de ejecución para CellTemplate y CellEditingTemplate. Traté de escribir el TextChanged = "OnTextChanged" en el TextBox en el DataTemplate, pero no se desencadena.

¿Alguien tiene alguna idea?

+0

Alguien tiene una idea en este caso? –

Respuesta

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

Esto funcionó muy bien para mí, gracias – McAden

-2

sé que es una noticia vieja ... pero me dio todo esto haciendo esto:

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

posterior
+2

Chu, por todo lo que he leído, no hay opción de PropertyChanged para UpdateSourceTrigger en SL2 o SL3.? –

+2

PropertyChanged es el valor de UpdateSourceTrigger en WPF. – sparks

0

me encontré con este mismo problema usando MVVM y S ilverlight 4. El problema es que el enlace no actualiza la fuente hasta después de que el cuadro de texto pierde el foco, pero establecer el foco en otro control no funciona.

Encontré una solución usando una combinación de dos publicaciones de blog diferentes. He utilizado el código de concepto DefaultButtonHub de Patrick Cauldwell, con una "SmallWorkaround" de SmallWorkarounds.net

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

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

Mi cambio resultó en el siguiente código para la clase 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); 
    } 

} 

Esto debe ser incluido en algún tipo de documentación para Silverlight :)

Cuestiones relacionadas