XAML 中 TextBox.TextChanged 的​​ WPF EventHandler 或代码隐藏? [英] WPF EventHandler for TextBox.TextChanged in XAML or code behind?

查看:19
本文介绍了XAML 中 TextBox.TextChanged 的​​ WPF EventHandler 或代码隐藏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 WPF 中,使用 MvvmLight,我有一个带有整数属性 SelectedIndex 的 viewModel.更改此属性的值是一项代价高昂的操作,因此我只想在操作员相当确定他已完成输入时更新该属性.

In WPF, using MvvmLight, I have a viewModel with an integer property SelectedIndex. Changing the value of this property is an expensive operation, so I only want to update the property if the operator is fairly certain that he finished typing.

我有一个文本框和一个按钮.操作员输入一个数字,然后按下按钮.这应该会导致更新属性的命令.

I have a TextBox and a button. The operator types a number, and presses the button. This should lead to a command that updates the property.

class MyViewModel
{
    private int selectedIndex;

    public MyViewModel()
    {
        this.CommandSelectIndex = new RelayCommand(ExecuteSelectIndex, CanSelectIndex);
    }

    public public RelayCommand<int> CommandSelectIndex { get; }

    public int SelectedIndex
    {
        get => this.selectedIndex;
        set => base.Set(nameof(SelectedIndex), ref this.selectedIndex, value);
    }

    private bool CanSelectIndex(int proposedIndex)
    {
         return proposedIndex > 0 && proposedIndex < MyData.Count;
    }

    private void ExecuteSelectIndex(int proposedIndex)
    {
        this.SelectedIndex = proposedIndex;
        ProcessSelectedIndex(proposedIndex);  // Expensive!
    }
}

对于了解 MvvmLight 的人来说,这相当简单.

For those who know MvvmLight, this is fairly straightforward.

因此,当操作员输入数字时,我只想更新按钮.我不想对中间值做任何事情:

So while the operator is typing a number, I only want to update the button. I don't want to do anything with the intermediate values:

1 --> 12 --> 123 --> (typing error, backspace) --> 124 [press button]

XAML

<StackPanel Name="Test1" Orientation="Horizontal">
    <TextBox Name="ProposedValue1" Text="1234" Width="300" Height="20"/>
    <Button x:Name="ButtonChangeText1" Content="Change"
                    Height="30" Width="74" Padding="5,2"
                    Command="{Binding Path=CommandSelectedIndex}"
                    CommandParameter="{Binding ElementName=ProposedValue1, Path=Text}"/>
</StackPanel>

这部分起作用:在启动时 CanSelectIndex(1234) 被调用;如果按钮被按下 ExecuteSelectedIndex(1234) 被调用.

This works partly: at startup CanSelectIndex(1234) is called; If the button is pressed ExecuteSelectedIndex(1234) is called.

但是,如果 TextBox 的文本发生变化,则不会调用 CanSelectIndex.

原因是当文本框改变时没有引发事件ICommand.CanExecuteChanged.

The reason is because event ICommand.CanExecuteChanged is not raised when the textbox changes.

解决方案:

添加事件处理程序:

XAML:

<TextBox Name="ProposedValue1" Text="1234" Width="300" Height="20"
         TextChanged="textChangedEventHandler"/>

背后的代码:

private void textChangedEventHandler(object sender, TextChangedEventArgs args)
{
    ((MyViewModel)this.DataContext).CommandSelectedIndex.RaiseCanExecuteChanged();
}

每当我不得不在后面写代码时,我总是感到有点不安.在后面的代码中编写事件处理程序是标准的,还是我只在教程中看到的简化.

I always feel a bit uneasy whenever I have to write code behind. Is it standard to write eventhandlers in code behind, or is that a simplification that I only see in tutorials.

是否有一种方法可以在 XAML 中执行此操作?有绑定的东西?

Is there a method that I can do this in XAML? Something with Binding?

TextChanged="TextChanged="{Binding Path=CommandSelectIndex ??? RaiseCanExecuteChanged() }

推荐答案

MvvmLight 中的 RelayCommand 类有两个实现.在 GalaSoft.MvvmLight.Command 命名空间和 GalaSoft.MvvmLight.CommandWpf 命名空间中.

The RelayCommand class in MvvmLight has two implementations. In the GalaSoft.MvvmLight.Command namespace and in the GalaSoft.MvvmLight.CommandWpf namespace.

您可能使用过 from 命名空间 GalaSoft.MvvmLight.Command.而这种类型实际上并不更新命令的状态.

You've probably used from namespace GalaSoft.MvvmLight.Command. And this type doesn't actually update the state of the command.

如果在 GalaSoft.MvvmLight.CommandWpf 命名空间中使用,则命令的状态会根据预定逻辑进行更新.

If used from the GalaSoft.MvvmLight.CommandWpf namespace, then the state of the command is updated according to the predetermined logic.

这篇关于XAML 中 TextBox.TextChanged 的​​ WPF EventHandler 或代码隐藏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆