如何使用EventToCommand与可编辑的Combobox绑定TextBoxBase.TextChanged与命令? [英] How to use an EventToCommand with an editable Combobox to bind TextBoxBase.TextChanged with a command?

查看:999
本文介绍了如何使用EventToCommand与可编辑的Combobox绑定TextBoxBase.TextChanged与命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可编辑的ComboBox。

I have an editable ComboBox.

<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绑定不。

I use GalaSoft.MvvmLight.Command.EventToCommand to bind the SelectionChanged event.
I also would like to bind the TextChanged event, but it is a little bit tricky: This event is only accessible by the ComboBox TextBoxBase property, and I can't find the proper way to bind this event.
You can see one of my unsuccessful attempt: SelectionChanged binding works fine, but TextChanged binding does not.

我也尝试过这种语法:

<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标签上得到一个错误可以实例化的类型。

But this won't even compile. I get an error "Type that can be instantiated expected" on the TextBoxBase tag.

任何想法?

推荐答案

我找到了解决这个问题的方法:

我创建一个不可见的TextBox,绑定到ComboBox,并绑定到TextChanged事件的命令文本框。
它不漂亮,但它工作...

I have found a way to work around the problem:
I create an invisible TextBox, bound to the ComboBox, and I bind the command on the TextChanged event of the TextBox. It's not pretty, but it works...

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

这篇关于如何使用EventToCommand与可编辑的Combobox绑定TextBoxBase.TextChanged与命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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