wpf 文本框绑定到 LostFocus 和属性更新
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15488033/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Textbox Binding to both LostFocus and Property Update
提问by adondero
Currently I bind to my TextBoxes as:
目前我绑定到我的TextBoxes 为:
Text="{Binding DocValue,
Mode=TwoWay,
ValidatesOnDataErrors=True,
UpdateSourceTrigger=PropertyChanged}"
This works great in getting every keystroke to do button status checking (which I want).
这对于让每次按键都进行按钮状态检查(我想要的)非常有用。
In addition, I would like to track the LostFocusevent on the TextBox(through binding) and do some additional calculations that might be too intensive for each keystroke.
此外,我想跟踪(通过绑定)LostFocus上的事件TextBox并进行一些额外的计算,这些计算对于每次击键都可能过于密集。
Anyone have thoughts on how to accomplish both?
有人对如何实现这两个目标有任何想法吗?
采纳答案by adondero
I think I have found a solution... I created a composite command and use that for the additional communication.
我想我找到了一个解决方案......我创建了一个复合命令并将其用于额外的通信。
Command definition
命令定义
public static CompositeCommand TextBoxLostFocusCommand = new CompositeCommand();
My textbox
我的文本框
private void TextboxNumeric_LostFocus(object sender, RoutedEventArgs e)
{
if (Commands.TextBoxLostFocusCommand.RegisteredCommands.Count > 0)
{
Commands.TextBoxLostFocusCommand.Execute(null);
}
}
then in my ViewModel, I create a Delegate Command and wire to it..
然后在我的 ViewModel 中,我创建了一个委托命令并连接到它..
It seems like it is working, wonder if there is a better way. One downfall to this is that every Textbox will fire this, not just my items that are attached to formulas I want to calculate. Might need to think of ways to improve on this.
看起来它正在工作,不知道是否有更好的方法。对此的一个缺点是每个文本框都会触发它,而不仅仅是我想要计算的公式中附加的项目。可能需要想办法改进这一点。
回答by Haritha
Bind a command to the TextBoxLostFocusevent.
将命令绑定到TextBoxLostFocus事件。
XAML
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>
View Model
查看模型
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
}
you have to reference System.Windows.Interactivityfor this. And you have to install a redistributable to use this library. you can download it from here
你必须参考System.Windows.Interactivity这个。并且您必须安装可再发行组件才能使用此库。你可以从这里下载
回答by BurnsBA
To supplement the highest voted answer, dotnet core has migrated the interactivity library. Steps to get this working:
为了补充投票最高的答案,dotnet core 迁移了交互库。使这个工作的步骤:
- Remove reference to "Microsoft.Expression.Interactions" and "System.Windows.Interactivity"
- Install the "Microsoft.Xaml.Behaviors.Wpf" NuGet package.
- XAML files – replace the xmlns namespaces "http://schemas.microsoft.com/expression/2010/interactivity" and "http://schemas.microsoft.com/expression/2010/interactions"with "http://schemas.microsoft.com/xaml/behaviors"
- C# files – replace the usings in c# files "Microsoft.Xaml.Interactivity" and "Microsoft.Xaml.Interactions" with "Microsoft.Xaml.Behaviors"
- 删除对“Microsoft.Expression.Interactions”和“System.Windows.Interactivity”的引用
- 安装“Microsoft.Xaml.Behaviors.Wpf”NuGet 包。
- XAML 文件 – 将 xmlns 命名空间“ http://schemas.microsoft.com/expression/2010/interactivity”和“ http://schemas.microsoft.com/expression/2010/interactions”替换为“ http: //schemas.xml” 。 microsoft.com/xaml/behaviors”
- C# 文件 – 用“Microsoft.Xaml.Behaviors”替换 c# 文件“Microsoft.Xaml.Interactivity”和“Microsoft.Xaml.Interactions”中的使用

