6

目前我绑定到我的TextBoxes 为:

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

这非常适合让每次击键都进行按钮状态检查(我想要的)。

此外,我想跟踪(通过绑定)LostFocus上的事件TextBox并进行一些额外的计算,这些计算对于每次击键来说可能过于密集。

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

4

3 回答 3

22

将命令绑定到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这个。你必须安装一个可再发行组件才能使用这个库。你可以从这里下载

于 2013-03-19T03:54:51.723 回答
4

为了补充最高投票的答案,dotnet core 迁移了交互库。使其工作的步骤:

  1. 删除对“Microsoft.Expression.Interactions”和“System.Windows.Interactivity”的引用
  2. 安装“Microsoft.Xaml.Behaviors.Wpf”NuGet 包。
  3. XAML 文件 – 将 xmlns 命名空间“ http://schemas.microsoft.com/expression/2010/interactivity ”和“ http://schemas.microsoft.com/expression/2010/interactions ”替换为“ http://schemas. microsoft.com/xaml/behaviors "
  4. C# 文件 – 将 c# 文件“Microsoft.Xaml.Interactivity”和“Microsoft.Xaml.Interactions”中的使用替换为“Microsoft.Xaml.Behaviors”

通过博客(2018 年 12 月)在此处发布

于 2020-01-09T01:46:37.263 回答
1

我想我找到了一个解决方案......我创建了一个复合命令并将其用于额外的通信。

命令定义

public static CompositeCommand TextBoxLostFocusCommand = new CompositeCommand();

我的文本框

private void TextboxNumeric_LostFocus(object sender, RoutedEventArgs e)
{
    if (Commands.TextBoxLostFocusCommand.RegisteredCommands.Count > 0)
    {
        Commands.TextBoxLostFocusCommand.Execute(null);
    }
}

然后在我的 ViewModel 中,我创建了一个委托命令并连接到它..

好像可以了,不知道有没有更好的方法。一个缺点是每个文本框都会触发它,而不仅仅是附加到我想要计算的公式的项目。可能需要想办法改进这一点。

于 2013-03-18T23:23:42.020 回答