Solução alternativa para atualizações LostFocus Focus no Silverlight Datagrid?
-
05-07-2019 - |
Pergunta
Eu tenho um aplicativo Silverlight 2 que valida os dados OntabSelectionChanged. Imediatamente comecei a desejar que o UpdateSourCetRigger permitisse mais do que apenas LostFocus, porque se você clicar na guia sem abordar um controle, o objeto LINQ não será atualizado antes da validação.
Eu trabalhei em torno da questão das caixas de texto definindo o foco para outro controle e depois de volta otextChanged:
Private Sub OnTextChanged(ByVal sender As Object, ByVal e As TextChangedEventArgs)
txtSetFocus.Focus()
sender.Focus()
End Sub
Agora estou tentando realizar o mesmo tipo de hacker dentro de um datagrid. Meu datagrid usa o DataTemplates gerados em tempo de execução para o CellTemplate e CellEditingTemplate. Tentei escrever o textChanged = "OntextChanged" na caixa de texto no datatemplate, mas não é acionado.
Alguém tem alguma ideia?
Solução
Você pode fazer isso com um comportamento aplicado à caixa de texto também
// 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();
}
}
Outras dicas
Esta postagem do blog mostra como atualizar a fonte de uma caixa de texto explicitamente usando a propriedade anexada:http://www.thomasclaudiushuber.com/blog/2009/07/17/here-it-is-the-updatesourcetrigger-for-propertychanged-in-silverlight/
Você poderia modificá -lo facilmente para trabalhar com outros controles também ...
Eu encontrei esse mesmo problema usando o MVVM e o Silverlight 4. O problema é que a ligação não atualiza a fonte até que a caixa de texto tenta focar, mas definir o foco em outro controle não faz o truque.
Encontrei uma solução usando uma combinação de duas postagens diferentes do blog. Eu usei o código do conceito DefaultButtonHub de Patrick Cauldwell, com um "Smallworkound" de SmallworkAlounds.net
http://www.cauldwell.net/patrick/blog/defaultbuttonsemanticsinsilverlightRevestisited.aspx
www.smallworkarounds.net/2010/02/ElementBindingBinding-Modes.html
Minha alteração resultou no código a seguir para a 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);
}
}
Isso deve ser incluído em algum tipo de documentação para Silverlight :)
Eu sei que são notícias antigas ... mas eu entendi isso fazendo isso:
Text = "{Caminho de ligação = NewQuantity, UpdateSourCetRigger = PropertyChanged}"