WPF MVVM:将命令绑定到事件 [英] WPF MVVM: binding command to event

查看:252
本文介绍了WPF MVVM:将命令绑定到事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的view和viewmodel文件.在我的视图模型中,我有以下简单代码:

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********");
}

在我的xaml中,我有:

In my xaml I have:

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

哑巴问题:如何将我的函数绑定到事件EditValueChanged,并像参数一样传递文本框的内容?简单的目标是:当用户在文本框中输入内容时,过滤绑定到视图模型的集合.

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.

在线上我发现了很多教程,代码段等等,但是其中任何一个都可以帮助我理解.

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

推荐答案

可以使用与此类似的方法来完成.

it can be done with something similar to this.

例如:-

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

调查

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

例如.

这篇关于WPF MVVM:将命令绑定到事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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