LostFocus 绑定缺少 setter 调用
LostFocus Binding missing setter call
我在 TextBox
上遇到双向 Binding
的问题。
<TextBox Text="{Binding Path=MyText, Mode="TwoWay", UpdateSourceTrigger=LostFocus}" />
在离开此元素的焦点时,我希望 setter 调用 MyText
,即使 Text
属性 没有改变。
public string MyText {
get { return _myText; }
set {
if (value == _myText) {
RefreshOnValueNotChanged();
return;
}
_myText = value;
NotifyOfPropertyChange(() => MyText);
}
}
从未调用测试函数RefreshOnValueNotChanged()
。有人知道诀窍吗?我需要 UpdateSourceTrigger=LostFocus
,因为 Enter
的附加行为(我需要完整的用户输入...)。
<TextBox Text="{Binding Path=MyText, Mode="TwoWay", UpdateSourceTrigger=LostFocus}" >
<i:Interaction.Behaviors>
<services2:TextBoxEnterBehaviour />
</i:Interaction.Behaviors>
</TextBox>
与 class:
public class TextBoxEnterBehaviour : Behavior<TextBox>
{
#region Private Methods
protected override void OnAttached()
{
if (AssociatedObject != null) {
base.OnAttached();
AssociatedObject.PreviewKeyUp += AssociatedObject_PKeyUp;
}
}
protected override void OnDetaching()
{
if (AssociatedObject != null) {
AssociatedObject.PreviewKeyUp -= AssociatedObject_PKeyUp;
base.OnDetaching();
}
}
private void AssociatedObject_PKeyUp(object sender, KeyEventArgs e)
{
if (!(sender is TextBox) || e.Key != Key.Return) return;
e.Handled = true;
((TextBox) sender).MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
}
#endregion
}
我自己找到了解决方法。但也许有人有比这更好的解决方案。现在我操纵 GotFocus
处的值。然后总是在离开控件焦点时调用 setter...
<TextBox Text="{Binding Path=MyText, Mode="TwoWay", UpdateSourceTrigger=LostFocus}" GotFocus="OnGotFocus" >
<i:Interaction.Behaviors>
<services2:TextBoxEnterBehaviour />
</i:Interaction.Behaviors>
</TextBox>
与:
private void OnGotFocus(object sender, RoutedEventArgs e)
{
var tb = sender as TextBox;
if(tb == null) return;
var origText = tb.Text;
tb.Text += " ";
tb.Text = origText;
}
我在 TextBox
上遇到双向 Binding
的问题。
<TextBox Text="{Binding Path=MyText, Mode="TwoWay", UpdateSourceTrigger=LostFocus}" />
在离开此元素的焦点时,我希望 setter 调用 MyText
,即使 Text
属性 没有改变。
public string MyText {
get { return _myText; }
set {
if (value == _myText) {
RefreshOnValueNotChanged();
return;
}
_myText = value;
NotifyOfPropertyChange(() => MyText);
}
}
从未调用测试函数RefreshOnValueNotChanged()
。有人知道诀窍吗?我需要 UpdateSourceTrigger=LostFocus
,因为 Enter
的附加行为(我需要完整的用户输入...)。
<TextBox Text="{Binding Path=MyText, Mode="TwoWay", UpdateSourceTrigger=LostFocus}" >
<i:Interaction.Behaviors>
<services2:TextBoxEnterBehaviour />
</i:Interaction.Behaviors>
</TextBox>
与 class:
public class TextBoxEnterBehaviour : Behavior<TextBox>
{
#region Private Methods
protected override void OnAttached()
{
if (AssociatedObject != null) {
base.OnAttached();
AssociatedObject.PreviewKeyUp += AssociatedObject_PKeyUp;
}
}
protected override void OnDetaching()
{
if (AssociatedObject != null) {
AssociatedObject.PreviewKeyUp -= AssociatedObject_PKeyUp;
base.OnDetaching();
}
}
private void AssociatedObject_PKeyUp(object sender, KeyEventArgs e)
{
if (!(sender is TextBox) || e.Key != Key.Return) return;
e.Handled = true;
((TextBox) sender).MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
}
#endregion
}
我自己找到了解决方法。但也许有人有比这更好的解决方案。现在我操纵 GotFocus
处的值。然后总是在离开控件焦点时调用 setter...
<TextBox Text="{Binding Path=MyText, Mode="TwoWay", UpdateSourceTrigger=LostFocus}" GotFocus="OnGotFocus" >
<i:Interaction.Behaviors>
<services2:TextBoxEnterBehaviour />
</i:Interaction.Behaviors>
</TextBox>
与:
private void OnGotFocus(object sender, RoutedEventArgs e)
{
var tb = sender as TextBox;
if(tb == null) return;
var origText = tb.Text;
tb.Text += " ";
tb.Text = origText;
}