【问题标题】:Windows Phone does not set SelectedItem when ItemTemplate has ScrollViewer当 ItemTemplate 有 ScrollViewer 时,Windows Phone 不设置 SelectedItem
【发布时间】:2014-02-16 22:20:46
【问题描述】:

我正在使用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>

【问题讨论】:

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


    【解决方案1】:

    试试看:

    <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 事件而不是 Tap 并在此处获取真实选择的项目,然后对其进行处理。

    【讨论】:

    • 不能使用 SelectionChanged,因为如果您点击与上次相同的项目(或者只有一个项目),它不会触发。我以前用过。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多