silverlight   发布时间:2022-05-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Mvvm-Light Silverlight,使用EventToCommand和Combobox大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

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

<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所遇到的程序开发问题。

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

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。