我有一个Silverlight 2应用程序,用于验证数据OnTabSelectionChanged。我立即开始希望UpdateSourceTrigger不仅允许LostFocus,因为如果单击选项卡而不选择控件,则在验证之前不会更新LINQ对象。
我通过将焦点设置为另一个控件然后返回OnTextChanged解决了TextBoxes的问题:
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,但不会触发它。
有人有什么想法吗?
答案 0 :(得分:7)
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();
}
}
答案 1 :(得分:0)
此博客文章介绍了如何使用附加属性显式更新文本框的来源: http://www.thomasclaudiushuber.com/blog/2009/07/17/here-it-is-the-updatesourcetrigger-for-propertychanged-in-silverlight/
您可以轻松修改它以与其他控件一起使用...
答案 2 :(得分:0)
我使用MVVM和Silverlight 4遇到了同样的问题。问题是绑定在文本框失去焦点之前不会更新源,但是将焦点设置在另一个控件上并不起作用。
我找到了一个使用两个不同博客文章组合的解决方案。我使用了Patrick Cauldwell的DefaultButtonHub概念代码,以及来自SmallWorkarounds.net的一个“SmallWorkaround”
http://www.cauldwell.net/patrick/blog/DefaultButtonSemanticsInSilverlightRevisited.aspx
www.smallworkarounds.net/2010/02/elementbindingbinding-modes.html
我的更改导致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);
}
}
这应包含在Silverlight的某些文档中:)
答案 3 :(得分:-2)
我知道这是个老消息......但我这样做是为了解决这个问题:
Text =“{Binding Path = newQuantity,UpdateSourceTrigger = PropertyChanged}”