Mvvm-Light Silverlight,使用EventToCommand和Combobox

Mvvm-Light Silverlight,使用EventToCommand和Combobox,第1张

概述我将视图模型中的ComboBox的SelectedItemChangeEvent连接到ICommand.一切似乎工作正常,但我不知道如何获取ComboxBox的SelectedItem.我想我需要使用EventToCommand的CommandParameter – 我将其绑定到ViewModel中具有ComboBox的selectedItem的内容吗?我试过这个: <ComboBox Wi 我将视图模型中的ComboBox的SelectedItemChangeEvent连接到ICommand.一切似乎工作正常,但我不知道如何获取ComBoxBox的SelectedItem.我想我需要使用EventToCommand的CommandParameter – 我将其绑定到viewmodel中具有ComboBox的selectedItem的内容吗?我试过这个:

<ComboBox   WIDth="422"  Height="24"  displayMemberPath="name"  ItemsSource="{Binding categoryTypes}"  SelectedItem="{Binding Selectedcategory}"  >    <i:Interaction.Triggers>        <i:EventTrigger Eventname="SelectionChanged">            <Mvvmlight:EventToCommand               Command="{Binding SelectcategoryCommand,Mode=TwoWay}"              CommandParameter="{Binding Selectedcategory,Mode=TwoWay}"              MustToggleIsEnabledValue="True" />        </i:EventTrigger>    </i:Interaction.Triggers></ComboBox>

在我看来,

public ICommand SelectcategoryCommand{    get    {        return new SelectcategoryCommand(this);    }}public categoryType Selectedcategory{    get; set;}

和ICommand

public class SelectcategoryCommand : ICommand{    private Rowviewmodel _rowviewmodel;    public SelectcategoryCommand(Rowviewmodel rowviewmodel)    {        _rowviewmodel = rowviewmodel;    }    public bool CanExecute(object parameter)    {        return true;    }    public event EventHandler CanExecuteChanged;    public voID Execute(object parameter)    {        categoryType categoryType = (categoryType) parameter;    }}

但是,ICommand的Execute方法中的参数始终为空.我对Silverlight没有经验,所以我觉得我真的很想念这里的东西.谁能帮忙?提前致谢!

解决方法 在做一些挖掘之后,我发现将实际的SelectionChangedEventArgs作为ICommand的execute参数传递很简单:

只要设置PassEventArgsToCommand =“True”

<ComboBox WIDth="422"          Height="24"          displayMemberPath="name"          ItemsSource="{Binding categoryTypes}"          SelectedItem="{Binding Selectedcategory}">    <i:Interaction.Triggers>        <i:EventTrigger Eventname="SelectionChanged">            <Mvvmlight:EventToCommand Command="{Binding SelectcategoryCommand,Mode=TwoWay}"                                      MustToggleIsEnabledValue="True"                                       PassEventArgsToCommand="True"/>        </i:EventTrigger>    </i:Interaction.Triggers></ComboBox>

然后在Execute方法中执行以下 *** 作:

public voID Execute(object parameter){    SelectionChangedEventArgs e = (SelectionChangedEventArgs)parameter;    categoryType categoryType = (categoryType)e.AddedItems[0];}
总结

以上是内存溢出为你收集整理的Mvvm-Light Silverlight,使用EventToCommand和Combobox全部内容,希望文章能够帮你解决Mvvm-Light Silverlight,使用EventToCommand和Combobox所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1004174.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存