交互之前的触发触发在Windows Phone 8中更改ListPicker

时间:2013-04-26 07:52:46

标签: xaml mvvm windows-phone-8 windows-phone eventtrigger

当ViewModel中出现触发器时,我遇到了一个问题,SelectedItem(参数)来自之前选择的Item。我需要新选择的项目作为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>

由于 维诺德

3 个答案:

答案 0 :(得分:2)

通常情况下,您应该将当前的SelectionItem作为参数传递给您的Command。由于事件是以过去时态编写的,因此您应该获取当前的SelectedItem而不是之前的.I /。

您可以尝试将SelectedItem的绑定添加到ListPicker,并省略将SelectedItem作为参数传递给Command。

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

然后您的命令需要访问属性SelectedBuilding来执行

  public class BuildingSelectionCommand{

      // a reference to your ViewModel that contains the SelectedBuilding-Property
      public BuildingsViewModel ViewModel {
         get;
         set;
      }

      public bool CanExecute(object parameter) {
         return ViewModel.SelectedBuilding != null;
      }

      public void Execute(object parameter){
         var selectedItem = ViewModel.SelectedBuilding;

         // execute command logic with selectedItem
      }
  }

代码可能与您不同,因为它取决于您实现ViewModel和Command的方式,但我认为您应该得到它。

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

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

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

答案 1 :(得分:2)

XAML:

<i:EventTrigger EventName="SelectionChanged">
    <command:EventToCommand Command="{Binding BuildingSelectionCommand}" PassEventArgsToCommand="True"/>
</i:EventTrigger>

命令:

RelayCommand<SelectionChangedEventArgs> BuildingSelectionCommand { get; set; }

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

你错过了“PassEventArgsToCommand”。确保您的命令具有SelectionChangedEventArgs。

答案 2 :(得分:1)

解决它的简单方法是使用

SelectedItem="{Binding SelectedBuilding, Mode=TwoWay}"

正如Jehof建议并摆脱所有“<i:”触发器设置但只是处理SelectedBuilding属性设置器中的更改并调用方法而不是使用命令来包装方法调用。你没有获得任何命令,因为你甚至没有在这里使用CanExecute,只是添加更多代码。