如何使用带有可编辑组合框的 EventToCommand 将 TextBoxBase.TextChanged 与命令绑定?

Fum*_*idu 3 wpf xaml combobox mvvm-light eventtocommand

我有一个可编辑的组合框。

<ComboBox IsEditable="True">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="SelectionChanged">
            <cmd:EventToCommand Command="{Binding CritereChangedCommand, Mode=OneWay}"/>
        </i:EventTrigger>
        <i:EventTrigger EventName="TextBoxBase.TextChanged">
            <cmd:EventToCommand Command="{Binding CritereChangedCommand, Mode=OneWay}"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>
</ComboBox>
Run Code Online (Sandbox Code Playgroud)

我使用 GalaSoft.MvvmLight.Command.EventToCommand 绑定 SelectionChanged 事件。
我还想绑定 TextChanged 事件,但有点棘手:此事件只能通过 ComboBox TextBoxBase 属性访问,并且我找不到绑定此事件的正确方法。
您可以看到我不成功的尝试之一:SelectionChanged 绑定工作正常,但 TextChanged 绑定不行。

我也尝试过这个语法:

<ComboBox IsEditable="True">
    <TextBoxBase>
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="TextChanged">
                <cmd:EventToCommand Command="{Binding CritereChangedCommand, Mode=OneWay}" />
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </TextBoxBase>
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="SelectionChanged">
            <cmd:EventToCommand Command="{Binding CritereChangedCommand, Mode=OneWay}" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
</ComboBox>
Run Code Online (Sandbox Code Playgroud)

但这甚至无法编译。我在 TextBoxBase 标记上收到错误“可以预期实例化的类型”。

任何想法 ?

use*_*639 5

我知道,回复很晚了......但我希望它会对某人有所帮助。

这里的问题是,EventTrigger中的类Microsoft.Expression.Interactivity.dll使用reflection属性值来查找事件EventName,这对于像TextBoxBase.TextChanged.

我使用的一种选择是按照此处EventTrigger所述实现您自己的自定义类,并使用这个类而不是 EventTrigger(那里的实现链接已损坏,但我设法使用互联网存档获取它)。CommandAction

另一个选项(我不喜欢它,因为它不是经典MVVM Command用法)是将Text的属性绑定ComboBox到 中的属性ViewModel,并从 setter 上的 ViewModel 代码调用命令,如下所示:

<ComboBox IsEditable="True"
          Text="{Binding Text, Mode=TwoWay}" />
Run Code Online (Sandbox Code Playgroud)

在视图模型中:

private string text;
public string Text
{
    get { return text; }
    set
    {
       text = value;
       OnPropertyChanged("Text");
       if(CritereChangedCommand.CanExecute())
          CritereChangedCommand.Execute();//call your command here
    }
}
Run Code Online (Sandbox Code Playgroud)