Xaml Windows Phone 8中ListPicker选择更改前的交互触发器

Xaml Windows Phone 8中ListPicker选择更改前的交互触发器,xaml,mvvm,windows-phone-8,windows-phone,eventtrigger,Xaml,Mvvm,Windows Phone 8,Windows Phone,Eventtrigger,我有一个问题,当触发器出现在ViewModel中时,SelectedItem(参数)出现在先前选择的项中。我需要新选择的项目作为selectionChanged的参数 我是WP8的新手。下面是代码 <toolkit:ListPicker Header="Background" ExpansionMode="FullscreenOnly" T

我有一个问题,当触发器出现在ViewModel中时,SelectedItem(参数)出现在先前选择的项中。我需要新选择的项目作为selectionChanged的参数

我是WP8的新手。下面是代码

    <toolkit:ListPicker Header="Background"
                                    ExpansionMode="FullscreenOnly"
                                    Template="{StaticResource ListPickerControlTemplate}"
                                    VerticalAlignment="Top"
                                    ItemsSource="{Binding Path=Buildings.ObjectList}"
                                    Margin="0"
                                    x:Name="buldings"
                                    Padding="0">
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="SelectionChanged">
                            <i:InvokeCommandAction  Command="{Binding Path=BuildingSelectionCommand}"
                                                    CommandParameter="{Binding Path=SelectedItem, ElementName=buldings}" />
                        </i:EventTrigger>
                    </i:Interaction.Triggers>

谢谢
Vinod

通常情况下,您应该将当前SelectionItem作为参数传递给您的命令。因为事件是用过去时写的,所以您应该得到当前选择的项,而不是之前的项

您可以尝试将SelectedItem的绑定添加到ListPicker,而不将SelectedItem作为参数传递给命令

<toolkit:ListPicker SelectedItem="{Binding SelectedBuilding, Mode=TwoWay}" >
  <i:Interaction.Triggers>
    <i:EventTrigger EventName="SelectionChanged">
      <i:InvokeCommandAction  Command="{Binding Path=BuildingSelectionCommand}"/>
    </i:EventTrigger>
  </i:Interaction.Triggers>
</toolkit:ListPicker>
代码在您这边可能不同,因为它取决于您如何实现ViewModel和命令,但我认为您应该得到它

另一种不使用EventTrigger的方法是直接在SelectedBuilding属性中执行命令

public Building SelectBuilding{
     get {
       return _selectedBuilding
     }
     set{
       _selectedBuilding = value;
       RaisePropertyChanged("SelectedBuilding");

       if (BuildingSelectionCommand.CanExecute(_selectedBuilding)) {
         BuildingSelectionCommand.Execute(_selectedBuilding);
       }
     }

解决这个问题的简单方法是使用

SelectedItem="{Binding SelectedBuilding, Mode=TwoWay}"
正如Jehof所建议的,去掉所有“
XAML:


命令:

RelayCommand<SelectionChangedEventArgs> BuildingSelectionCommand { get; set; }

BuildingSelectionCommand = new RelayCommand<SelectionChangedEventArgs>(async (args) => { });
RelayCommand buildingselection命令{get;set;}
BuildingSelectionCommand=newrelaycommand(异步(args)=>{});

您刚刚错过了“passeventargsto”命令“。请确保您的命令具有SelectionChangedEventArgs。

我没有找到SelectedItemChanged作为ListPicker的事件。”。我尝试了上面的代码,但没有成功。它没有被触发。@vinod8812正确,ListPicker只有SelectionChanged事件。这是我的错。我已经更新了我的答案,以提供另一个解决方案。我认为这不会起作用,因为我想执行一个异步方法。这两种方法都不能执行异步方法。@vinod8812异步方法是什么意思。我认为你应该在你的问题中添加更多细节异步方法不在UI线程上运行,即使你做了一些繁重的操作,如调用web方法等,也会使UI响应。这就是为什么在c#4.5中,我们在windows 8/phone中使用此方法的原因。dude我不能使用属性更改事件来调用过于异步的繁重方法methods@vinod8812我真的不知道知道你的问题是什么,你现在得到了两个答案,你总是谈论你的异步方法。您的问题是,您总是将错误的项作为参数传递给您的命令。您应该更新您的问题以提供更多信息,或者您应该回答另一个您不使用PropertyChanged的问题。我认为您对什么是异步方法有误解。您可以从SelectedBuilding setter调用异步方法,仅此而已。我不喜欢这种方法。如果您的应用程序正在恢复,将点击设置程序以恢复状态。这意味着您可能正在运行您实际上只想在用户显式更改列表框时执行的代码。阅读其他答案后,我不确定您的问题是什么。
<i:EventTrigger EventName="SelectionChanged">
    <command:EventToCommand Command="{Binding BuildingSelectionCommand}" PassEventArgsToCommand="True"/>
</i:EventTrigger>
RelayCommand<SelectionChangedEventArgs> BuildingSelectionCommand { get; set; }

BuildingSelectionCommand = new RelayCommand<SelectionChangedEventArgs>(async (args) => { });