文本框绑定到LostFocus和Property Update

ado*_*ero 5 wpf xaml binding textbox prism

目前我绑定到我的TextBoxes:

Text="{Binding DocValue,
         Mode=TwoWay,
         ValidatesOnDataErrors=True,
         UpdateSourceTrigger=PropertyChanged}"
Run Code Online (Sandbox Code Playgroud)

这非常适合每次按键进行按钮状态检查(我想要).

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

任何人都有关于如何完成两者的想法?

Har*_*tha 19

将命令绑定到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>
Run Code Online (Sandbox Code Playgroud)

查看模型

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            
}
Run Code Online (Sandbox Code Playgroud)

你必须参考System.Windows.Interactivity这个.并且您必须安装可再发行组件才能使用此库.你可以从这里下载


Bur*_*sBA 5

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

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

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

\n


ado*_*ero 1

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

命令定义

public static CompositeCommand TextBoxLostFocusCommand = new CompositeCommand();
Run Code Online (Sandbox Code Playgroud)

我的文本框

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

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

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