wpf TextBox.TextChanged & ICommandSource
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/55855/
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.TextChanged & ICommandSource
提问by Brad Leach
I am following the M-V-VMpattern for my WPF UI. I would like to hook up a command to the TextChanged event of a TextBox to a command that is in my ViewModel class. The only way I can conceive of completing this task is to inherit from the TextBox control, and implement ICommandSource. I can then instruct the command to be fired from the TextChanged event. This seems to be too much work for something which appears to be so simple.
我正在为我的 WPF UI遵循MV-VM模式。我想将一个命令连接到一个 TextBox 的 TextChanged 事件到我的 ViewModel 类中的一个命令。我能想到的完成此任务的唯一方法是从 TextBox 控件继承,并实现 ICommandSource。然后我可以指示从 TextChanged 事件触发命令。对于看起来如此简单的事情来说,这似乎是太多的工作。
Is there an easier way (than subclassing the TextBox and implementing ICommandSource) to hook up the TextChanged event to my ViewModel class?
有没有更简单的方法(比继承 TextBox 并实现 ICommandSource)将 TextChanged 事件连接到我的 ViewModel 类?
回答by Samuel Hyman
First off, you've surely considered two-way data binding to your viewmodel, with an UpdateSourceTrigger of PropertyChanged? That way the property setter of the property you bind to will be called every time the text is changed?
首先,您肯定考虑过使用 PropertyChanged 的 UpdateSourceTrigger 对视图模型进行双向数据绑定?这样每次更改文本时都会调用您绑定到的属性的属性设置器?
If that's not enough, then I would tackle this problem using Attached Behaviours. On Julian Dominguez's Blog you'll find an articleabout how to do something very similar in Silverlight, which should be easily adaptable to WPF.
如果这还不够,那么我将使用附加行为来解决这个问题。在 Julian Dominguez 的博客上,您会找到一篇关于如何在 Silverlight 中做一些非常相似的事情的文章,它应该很容易适应 WPF。
Basically, in a static class (called, say TextBoxBehaviours) you define an Attached Property called (perhaps) TextChangedCommand of type ICommand. Hook up an OnPropertyChanged handler for that property, and within the handler, check that the property is being set on a TextBox; if it is, add a handler to the TextChanged event on the textbox that will call the command specified in the property.
基本上,在静态类(称为 TextBoxBehaviours)中,您定义了一个名为(可能)ICommand 类型的 TextChangedCommand 的附加属性。为该属性连接一个 OnPropertyChanged 处理程序,并在处理程序中检查该属性是否设置在 TextBox 上;如果是,则向文本框上的 TextChanged 事件添加一个处理程序,该处理程序将调用属性中指定的命令。
Then, assuming your viewmodel has been assigned to the DataContext of your View, you would use it like:
然后,假设您的 viewmodel 已分配给您的 View 的 DataContext,您可以像这样使用它:
<TextBox
x:Name="MyTextBox"
TextBoxBehaviours.TextChangedCommand="{Binding ViewModelTextChangedCommand}" />
回答by Nidonocu
Using the event binding and command method might not be the right thing to use. What exactly will this command do?
使用事件绑定和命令方法可能不正确。这个命令究竟会做什么?
You might want to consider using a Databinding to a string field in your VM. This way you can make a call to a command or function from there rather than having the UI care at all.
您可能需要考虑对 VM 中的字符串字段使用数据绑定。通过这种方式,您可以从那里调用命令或函数,而根本无需关注 UI。
<TextBox Text="{Binding WorldName}"/>
....
public string WorldName
{
get
{
return WorldData.Name;
}
set
{
WorldData.Name = value;
OnPropertyChanged("WorldName");
// CallYourCustomFunctionHere();
}
}
回答by Kent Boogaart
Can you not just handle the TextChanged event and execute the command from there?
你不能只处理 TextChanged 事件并从那里执行命令吗?
private void _textBox_TextChanged(object sender, EventArgs e)
{
MyCommand.Execute(null);
}
The alternative, as you say, is to create a TextBox
that acts as a command source, but that does seem like overkill unless it's something you're planning on sharing and leveraging in many places.
正如您所说,另一种方法是创建一个TextBox
充当命令源的工具,但这似乎有点矫枉过正,除非您计划在许多地方共享和利用它。