当ItemTemplate具有ScrollViewer时,Windows Phone不会设置SelectedItem

时间:2014-02-16 22:20:47

标签: c# windows-phone-8 listbox mvvm-light relaycommand

我正在使用允许在列表框中拖放项目的ReorderListBox control

我还使用MvvmLightEventTriggerEventToCommand类来捕获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>

1 个答案:

答案 0 :(得分:0)

试试:

<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 事件代替点按并获取在那里选择的项目,然后进行处理。