WPF MVVM:将命令绑定到事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25809655/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 12:31:33  来源:igfitidea点击:

WPF MVVM: binding command to event

c#wpfxamlmvvmbinding

提问by Piero Alberto

I have my view and viewmodel files. In my viewmodel, I have this simply code:

我有我的视图和视图模型文件。在我的视图模型中,我有这个简单的代码:

private void Filter(string keyword)
{
    Debug.Print("******START********");
    string stringToSearch = keyword.ToLower();
    ObservableCollection<TabImpianti> listBoxSource = new ObservableCollection<TabImpianti>();
    foreach (TabImpianti ti in p_ListaImpianti)
    {
        if (ti.NOME.ToString().ToLower().Contains(stringToSearch))
            listBoxSource.Add(ti);
    }
    p_ListaImpianti = listBoxSource;
    Debug.Print("******END********");
}

In my xaml I have:

在我的 xaml 中,我有:

<dxe:TextEdit  ValidateOnTextInput="True" Margin="105,10,797,631" />

DUMB QUESTION: how can I bind my function to the event EditValueChanged, passing also like parameter the content of the textbox? the simply goal is: when the user writes something in the textbox, filter the collection binded to the viewmodel.

愚蠢的问题:我怎样才能将我的函数绑定到事件 EditValueChanged,也像参数一样传递文本框的内容?简单的目标是:当用户在文本框中写入内容时,过滤绑定到视图模型的集合。

Online I have found a lot of tutorial, code snippet and so on, but anyone of these is helping me to understand.

我在网上找到了很多教程、代码片段等,但其中任何一个都帮助我理解。

回答by Jay Nirgudkar

it can be done with something similar to this.

它可以用类似的东西来完成。

Eg:-

例如:-

<TextBox Margin="89,116,69,123" x:Name="txtFilter" Background="AliceBlue" >
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="TextChanged">
                <cmd:EventToCommand Command="{Binding SearchedTextChanged}" CommandParameter="{Binding Text, ElementName=txtFilter}"/>
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </TextBox>

look into

调查

http://www.c-sharpcorner.com/Blogs/11789/example-of-eventtrigger-in-mvvm-application.aspxhttp://social.msdn.microsoft.com/Forums/vstudio/en-US/fd819518-605a-46ae-a9e4-26556d0f3e15/wpf-textbox-trigger?forum=wpf

http://www.c-sharpcorner.com/Blogs/11789/example-of-eventtrigger-in-mvvm-application.aspx http://social.msdn.microsoft.com/Forums/vstudio/en-US/fd819518 -605a-46ae-a9e4-26556d0f3e15/wpf-textbox-trigger?forum=wpf

for further example.

进一步的例子。

回答by DmitryG

Take a look at the following good article that clearly describes all the aspects related to the DevExpress implementation of the EventToCommand behavior: DevExpress MVVM Framework. EventToCommand.. With using this approach you can implement your task as follows:

看看下面的好文章,它清楚地描述了与 EventToCommand 行为的 DevExpress 实现相关的所有方面:DevExpress MVVM 框架。事件到命令。. 通过使用这种方法,您可以按如下方式实现您的任务:

<dxe:TextEdit Margin="89,116,69,123" x:Name="txtFilter" Background="AliceBlue" >
    <dxmvvm:Interaction.Behaviors> 
        <dxmvvm:EventToCommand EventName="EditValueChanged" Command="{Binding FilterCommand}"
            CommandParameter="{Binding ElementName=txtFilter, Path=Text}"/> 
    </dxmvvm:Interaction.Behaviors> 
...
[POCOViewModel]
public class CoolectionViewModel {
    [Command]
    public void Filter(string searchText) {
        ...
    }
}

P.S. Using the DevExpress controls you can accomplish the filtering of the listbox-control items via search-box without any coding at all. Just bind the ListBoxEdit.FilterCriteriaproperty to the SearchControl.FilterCriteriaproperty:

PS 使用 DevExpress 控件,您可以通过搜索框完成列表框控件项的过滤,无需任何编码。只需将ListBoxEdit.FilterCriteria属性绑定到SearchControl.FilterCriteria属性:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <dxe:SearchControl x:Name="searchControl" Grid.Row="0" Margin="10"
                     HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
    <dxe:ListBoxEdit Name="listBox" Grid.Row="1"  Margin="10"
                     HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
                     DisplayMember="Name" ValueMember="ID"
                     FilterCriteria="{Binding FilterCriteria, ElementName=searchControl}"/>
</Grid>

You can play with this approach using the following demo(the link points to the Silverlight version, but the WPF version behaves exactly the same). You can use the same approach with any list-control from DX (ListBoxEdit, ComboBoxEdit, DXGrid etc.)

您可以使用以下演示来尝试这种方法(链接指向 Silverlight 版本,但 WPF 版本的行为完全相同)。您可以对来自 DX 的任何列表控件(ListBoxEdit、ComboBoxEdit、DXGrid 等)使用相同的方法

回答by Erno

Instead of binding the ViewModel function to the event of the View you could bind the TextEdit's text property to a property of the ViewModel and monitor that property of the ViewModel (PropertyChanged?!).

您可以将 TextEdit 的文本属性绑定到 ViewModel 的属性并监视 ViewModel 的该属性(PropertyChanged?!),而不是将 ViewModel 函数绑定到 View 的事件。

As soon as that property changes you could execute the function.

一旦该属性发生变化,您就可以执行该函数。

This way you can still test the ViewModel without tying it to the View by relying to the raised event.

通过这种方式,您仍然可以通过依赖引发的事件来测试 ViewModel,而无需将其绑定到 View。

回答by user469104

Your TextEdit control should be bound to a property in the ViewModel. In the setter of that property you would call the Filter method.

您的 TextEdit 控件应该绑定到 ViewModel 中的一个属性。在该属性的 setter 中,您将调用 Filter 方法。

This example with the assumption that your TextEdit control has a Text property. If not, change to bind to whatever the property name is.

此示例假设您的 TextEdit 控件具有 Text 属性。如果不是,请更改为绑定到任何属性名称。

<dxe:TextEdit Text="{Binding MyTextValue}"  ValidateOnTextInput="True" Margin="105,10,797,631" />

Then in the ViewModel class:

然后在 ViewModel 类中:

private string _myTextValue;
public string MyTextValue {
    get {
        return _myTextValue;
    }
    set {
        if (value != _myTextValue) {
            _myTextValue = value;
            Filter(_myTextValue);
        }
    }
 }