【发布时间】:2014-02-16 22:20:46
【问题描述】:
我正在使用ReorderListBox control,它允许在列表框中拖放项目。
我还使用MvvmLight、EventTrigger 和EventToCommand 类来捕获Tap 事件并执行RelayCommand 处理程序。当我有一个普通的旧 StackPanel 作为我的列表框的项目模板时,一切正常。但是,只要我将ScrollViewer 插入其中,我的SelectedItem 就会显示为空。有没有办法获取应该绑定到该滚动查看器的项目?代码如下。
视图模型
public ViewModelClass
{
...
public ObservableCollection<MyItemViewModel> MyItemsSource { get; private set; }
public RelayCommand<MyItemViewModel> EditItemCommand { get; private set; }
public ViewModelClass()
{
EditItemCommand = new RelayCommand<MyItemViewModel>(OnEditItem);
}
private void OnEditItem(MyItemViewModel parameter)
{
// parameter is always null, even when I change the type of the RelayCommand to object
}
}
XAML
<rlb:ReorderListBox x:Name="MyListBox" SelectionMode="Single" ItemsSource="{Binding MyItemsSource}" IsReorderEnabled="True">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Tap">
<command:EventToCommand Command="{Binding EditItemCommand}" CommandParameter="{Binding ElementName=MyListBox, Path=SelectedItem}" />
</i:EventTrigger>
</i:Interaction.Triggers>
<rlb:ReorderListBox.ItemTemplate>
<DataTemplate>
<ScrollViewer Margin="0,4" toolkit:TiltEffect.IsTiltEnabled="True" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Disabled">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding SomeProperty}" Style="{StaticResource BaseTextStyle}" FontSize="{StaticResource PhoneFontSizeMediumLarge}" FontFamily="Segoe WP SemiLight" VerticalAlignment="Center" />
<Border Background="DarkGoldenrod" Margin="3" VerticalAlignment="Center">
<TextBlock Margin="6,3" Text="{Binding AnotherProperty}" Foreground="White" FontFamily="Segoe WP Light" FontSize="16" VerticalAlignment="Center" TextAlignment="Right" />
</Border>
</StackPanel>
</ScrollViewer>
</DataTemplate>
</rlb:ReorderListBox.ItemTemplate>
</rlb:ReorderListBox>
【问题讨论】:
标签: c# windows-phone-8 listbox mvvm-light relaycommand