Silverlight Datagrid上UpdateSourceTrigger LostFocus的解决方法? [英] Workaround for UpdateSourceTrigger LostFocus on Silverlight Datagrid?
问题描述
我通过将焦点设置为另一个控件,然后重新启动OnTextChanged:
Private Sub OnTextChanged(ByVal sender As Object,ByVal e As TextChangedEventArgs)
txtSetFocus.Focus()
sender.Focus()
End Sub
现在我试图在DataGrid中完成同样的攻击。我的DataGrid使用在运行时生成的CellTemplate和CellEditingTemplate的DataTemplates。我尝试将TextChanged =OnTextChanged写入DataTemplate中的TextBox,但不会触发。
任何人都有任何想法?
// xmlns:int是System.Windows.Interactivity.DLL中的System.Windows.Interactivity
// xmlns:behavior是您下面的类的命名空间
< 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();
}
}
I have a Silverlight 2 application that validates data OnTabSelectionChanged. Immediately I began wishing that UpdateSourceTrigger allowed more than just LostFocus because if you click the tab without tabbing off of a control the LINQ object is not updated before validation.
I worked around the issue for TextBoxes by setting focus to another control and then back OnTextChanged:
Private Sub OnTextChanged(ByVal sender As Object, ByVal e As TextChangedEventArgs)
txtSetFocus.Focus()
sender.Focus()
End Sub
Now I am trying to accomplish the same sort of hack within a DataGrid. My DataGrid uses DataTemplates generated at runtime for the CellTemplate and CellEditingTemplate. I tried writing the TextChanged="OnTextChanged" into the TextBox in the DataTemplate, but it is not triggered.
Anyone have any ideas?
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();
}
}
这篇关于Silverlight Datagrid上UpdateSourceTrigger LostFocus的解决方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!