C# 当ItemTemplate具有ScrollViewer时,Windows Phone不会设置SelectedItem
我正在使用允许在列表框中拖放项目的 我还使用了C# 当ItemTemplate具有ScrollViewer时,Windows Phone不会设置SelectedItem,c#,windows-phone-8,listbox,mvvm-light,relaycommand,C#,Windows Phone 8,Listbox,Mvvm Light,Relaycommand,我正在使用允许在列表框中拖放项目的 我还使用了MvvmLight、一个EventTrigger和EventToCommand类来捕获Tap事件并执行一个RelayCommand处理程序。当我有一个普通的StackPanel作为我的列表框的项目模板时,一切正常。但是,只要我在其中粘贴一个ScrollViewer,我的SelectedItem就会显示为null。是否有方法获取应绑定到该滚动查看器的项目?代码如下 视图模型 public ViewModelClass { ... pu
MvvmLight
、一个EventTrigger
和EventToCommand
类来捕获Tap事件并执行一个RelayCommand
处理程序。当我有一个普通的StackPanel
作为我的列表框的项目模板时,一切正常。但是,只要我在其中粘贴一个ScrollViewer
,我的SelectedItem
就会显示为null。是否有方法获取应绑定到该滚动查看器的项目?代码如下
视图模型
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
}
}
公共视图模型类
{
...
公共ObservableCollection MyItemsSource{get;private set;}
public RelayCommand EditItemCommand{get;private set;}
公共ViewModelClass()
{
EditItemCommand=新的RelayCommand(OnEditItem);
}
私有void OnEditItem(MyItemViewModel参数)
{
//参数始终为null,即使我将RelayCommand的类型更改为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>
试试看:
<rlb:ReorderListBox x:Name="MyListBox" SelectionMode="Single"
ItemsSource="{Binding MyItemsSource}"
IsReorderEnabled="True"
Height="400">
<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.Template>
<ControlTemplate>
<ScrollViewer Margin="0,4" toolkit:TiltEffect.IsTiltEnabled="True" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
<ItemsPresenter />
</ScrollViewer>
</ControlTemplate>
</rlb:ReorderListBox.Template>
<rlb:ReorderListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding SomeProperty}" 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>
</DataTemplate>
</rlb:ReorderListBox.ItemTemplate>
高度在这里很重要,因为需要指定它才能使ScrollViewer正常工作
然而,这使得选择工作有点糟糕?像多个项目被选中。。。我不知道这是控制本身的错误还是什么
我会尝试使用SelectionChanged事件,而不是点击,然后获取所选的真实项目,然后进行处理。无法使用SelectionChanged,因为如果您点击的项目与上次相同(或者只有一个项目),则不会触发该事件。我以前用过。