目前,我将TextBox绑定(bind)为:

Text="{Binding DocValue,
         Mode=TwoWay,
         ValidatesOnDataErrors=True,
         UpdateSourceTrigger=PropertyChanged}"

这在使每个击键都进行按钮状态检查(我想要的)时效果很好。

另外,我想跟踪LostFocus上的TextBox事件(通过绑定(bind)),并进行一些其他的计算,这些计算对于每个击键都可能过于费力。

任何人都对如何实现这两者有想法吗?

最佳答案

将命令绑定(bind)到TextBox LostFocus事件。

XAML

xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"

<TextBox Margin="0,287,0,0">
     <i:Interaction.Triggers>
          <i:EventTrigger EventName="LostFocus">
               <i:InvokeCommandAction Command="{Binding LostFocusCommand}" />
          </i:EventTrigger>
     </i:Interaction.Triggers>
</TextBox>

查看模型

private ICommand lostFocusCommand;

public ICommand LostFocusCommand
{
    get
    {
        if (lostFocusCommand== null)
        {
            lostFocusCommand= new RelayCommand(param => this.LostTextBoxFocus(), null);
        }
        return lostFocusCommand;
     }
}

private void LostTextBoxFocus()
{
    // do your implementation
}

您必须为此引用System.Windows.Interactivity。而且,您必须安装可重新分发的文件才能使用此库。您可以从here下载它

关于wpf - 文本框绑定(bind)到LostFocus和属性更新,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15488033/

10-11 22:11
查看更多