TextBox.TextChanged & 命令源

发布于 2024-07-05 04:39:09 字数 377 浏览 4 评论 0原文

我的 WPF 遵循 MV-VM 模式用户界面。 我想将 TextBox 的 TextChanged 事件的命令连接到 ViewModel 类中的命令。 我想到完成此任务的唯一方法是继承 TextBox 控件,并实现 ICommandSource。 然后我可以指示从 TextChanged 事件触发该命令。 对于看起来如此简单的事情来说,这似乎是太多的工作。

有没有更简单的方法(比子类化 TextBox 和实现 ICommandSource)将 TextChanged 事件连接到我的 ViewModel 类?

I am following the M-V-VM pattern 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.

Is there an easier way (than subclassing the TextBox and implementing ICommandSource) to hook up the TextChanged event to my ViewModel class?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

七分※倦醒 2024-07-12 04:39:09

您不能只处理 TextChanged 事件并从那里执行命令吗?

private void _textBox_TextChanged(object sender, EventArgs e)
{
    MyCommand.Execute(null);
}

正如您所说,另一种方法是创建一个充当命令源的 TextBox ,但这似乎有点矫枉过正,除非您计划在许多地方共享和利用它。

Can you not just handle the TextChanged event and execute the command from there?

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.

明媚如初 2024-07-12 04:39:09

使用事件绑定和命令方法可能不是正确的选择。
这个命令到底会做什么?

您可能需要考虑对虚拟机中的字符串字段使用数据绑定。 这样,您就可以从那里调用命令或函数,而不必关心 UI。

<TextBox Text="{Binding WorldName}"/>
....
public string WorldName
{
    get
    {
        return WorldData.Name;
    }
    set
    {
        WorldData.Name = value;
        OnPropertyChanged("WorldName");
        // CallYourCustomFunctionHere();
    }
}

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.

<TextBox Text="{Binding WorldName}"/>
....
public string WorldName
{
    get
    {
        return WorldData.Name;
    }
    set
    {
        WorldData.Name = value;
        OnPropertyChanged("WorldName");
        // CallYourCustomFunctionHere();
    }
}
阳光下慵懒的猫 2024-07-12 04:39:09

首先,您肯定考虑过使用 PropertyChanged 的​​ UpdateSourceTrigger 将双向数据绑定到您的视图模型? 这样每次文本更改时都会调用您绑定的属性的属性设置器?

如果这还不够,那么我将使用附加行为来解决这个问题。 在 Julian Dominguez 的博客上,您会找到一篇文章 关于如何在 Silverlight 中做一些非常类似的事情,它应该很容易适应 WPF。

基本上,在静态类(称为 TextBoxBehaviours)中,您定义一个名为(可能)ICommand 类型的 TextChangedCommand 的附加属性。 为该属性连接一个 OnPropertyChanged 处理程序,并在处理程序中检查该属性是否在 TextBox 上设置; 如果是,请将处理程序添加到文本框上的 TextChanged 事件,该处理程序将调用属性中指定的命令。

然后,假设您的视图模型已分配给视图的 DataContext,您将使用它,如下所示:

<TextBox
  x:Name="MyTextBox"
  TextBoxBehaviours.TextChangedCommand="{Binding ViewModelTextChangedCommand}" />

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?

If that's not enough, then I would tackle this problem using Attached Behaviours. On Julian Dominguez’s Blog you'll find an article about how to do something very similar in Silverlight, which should be easily adaptable to 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.

Then, assuming your viewmodel has been assigned to the DataContext of your View, you would use it like:

<TextBox
  x:Name="MyTextBox"
  TextBoxBehaviours.TextChangedCommand="{Binding ViewModelTextChangedCommand}" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文