Windows phone 7 MVVM EventToCommand参数为空

Windows phone 7 MVVM EventToCommand参数为空,windows-phone-7,mvvm,mvvm-light,Windows Phone 7,Mvvm,Mvvm Light,我正在Windows Phone 7.1中使用MVVM Light Toolkit ViewModel的一部分: public RelayCommand<object> ChangeVATCommand { get { return _changeVATCommand ?? (_changeVATCommand = new RelayCommand&

我正在Windows Phone 7.1中使用MVVM Light Toolkit

ViewModel的一部分:

public RelayCommand<object> ChangeVATCommand
        {
            get
            {
                return _changeVATCommand
                    ?? (_changeVATCommand = new RelayCommand<object>(
                                          (vat) =>
                                          {

                                          }));
            }
        }
public RelayCommand changewatcommand
{
得到
{
返回_changewatcommand
?(_changewatcommand=新继电器命令(
(增值税)=>
{
}));
}
}
Xaml的一部分:

    <toolkit:ListPicker  ItemsSource="{Binding VATs}" x:Name="VATs" SelectedIndex="0"  DisplayMemberPath="Name">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="SelectionChanged">
                <cmd:EventToCommand Command="{Binding ChangeVATCommand, Mode=OneWay}"
                                    CommandParameter="{Binding Path=SelectedItem, ElementName=VATs}"
                />
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </toolkit:ListPicker>

当命令运行时,传递给lambda的vat为null。如果我在xaml CommandParameter中设置为“{Binding ElementName=VATs}”,则lambda中的vat具有正确属性SelectedItem object(非空)的ListPicker

这是虫子还是我做错了什么? 更新

总结:

  • CommandParameter=“{Binding Path=SelectedItem,ElementName=VATs}”-我有空值

  • CommandParameter=“{Binding Path=SelectedIndex,ElementName=VATs}”-工作正常!我有所选增值税的索引

  • CommandParameter=“{Binding,ElementName=VATs}”-工作正常!我有一个具有适当selecteditem的ListPicker

  • 不久前,我遇到了一个类似的问题,解决方案是指示对应的VM:

    <toolkit:ListPicker  ItemsSource="{Binding VATs}" x:Name="VATs" SelectedIndex="0"  DisplayMemberPath="Name">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="SelectionChanged">
                <cmd:EventToCommand Command="{Binding Path=ViewModelName.ChangeVATCommand, Source={StaticResource Locator}, Mode=OneWay}"
                                    CommandParameter="{Binding Path=SelectedItem, ElementName=VATs}"
                />
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </toolkit:ListPicker>
    
    
    

    请注意,现在绑定引用ViewModel并使用定位器作为源。

    我更推荐使用双向绑定将ListPicker的SelectedItem绑定到VM上的属性。然后通过命令,您可以轻松访问此属性

    如果选择此解决方案,您甚至可能不需要该命令,您可以直接从VM中的SelectedItem属性中触发计算

    干杯
    劳伦特

    我做了您的更改,但增值税仍然为空。奇怪的是,例如SelectedIndex(CommandParameter=“{Binding Path=SelectedIndex,ElementName=VATs}”)属性被正确地传递给command如果您想接收selectedItem,请尝试
    CommandParameter=“{Binding}”
    CommandParameter=“{Binding}”传递整个视图模型,因为这是trigger/element的数据上下文。无论如何,我检查了这个……当然,这是实现我目标的更好方法。更少的代码更少的bug!谢谢你。我使用第二个选项-CommandParameter=“{Binding Path=SelectedIndex,ElementName=VATs}”