【问题标题】:How to use an EventToCommand with an editable Combobox to bind TextBoxBase.TextChanged with a command?如何使用带有可编辑组合框的 EventToCommand 将 TextBoxBase.TextChanged 与命令绑定?
【发布时间】:2023-03-23 08:46:01
【问题描述】:

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

<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>

我使用 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>

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

有什么想法吗?

【问题讨论】:

    标签: wpf xaml combobox mvvm-light eventtocommand


    【解决方案1】:

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

    这里的问题是Microsoft.Expression.Interactivity.dll 中的EventTrigger 类使用reflection 通过EventName 属性的值来查找事件,这不适用于TextBoxBase.TextChanged 等附加事件。

    我使用的一个选项是实现您自己的自定义 EventTrigger 类,如 here 所述,并使用该类而不是 EventTrigger(指向 CommandAction 实现的链接已损坏,但我设法使用互联网档案获得它)。

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

    <ComboBox IsEditable="True"
              Text="{Binding Text, Mode=TwoWay}" />
    

    在 ViewModel 中:

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

    【讨论】:

      【解决方案2】:

      我找到了解决问题的方法:
      我创建了一个不可见的 TextBox,绑定到 ComboBox,并将命令绑​​定到 TextBox 的 TextChanged 事件。 它不漂亮,但它有效......

      <ComboBox Name="CbRubrique" IsEditable="True">
          <i:Interaction.Triggers>
              <i:EventTrigger EventName="SelectionChanged">
                  <cmd:EventToCommand Command="{Binding CritereChangedCommand, Mode=OneWay}" />
              </i:EventTrigger>
          </i:Interaction.Triggers>
      </ComboBox>
      <TextBox Text="{Binding ElementName=CbRubrique, Path=Text, UpdateSourceTrigger=PropertyChanged}" Visibility="Collapsed">
          <i:Interaction.Triggers>
              <i:EventTrigger EventName="TextChanged">
                  <cmd:EventToCommand Command="{Binding CritereChangedCommand, Mode=OneWay}" />
              </i:EventTrigger>
          </i:Interaction.Triggers>
      </TextBox>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-02-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多