简体   繁体   中英

Windows Phone does not set SelectedItem when ItemTemplate has ScrollViewer

I'm using the ReorderListBox control that allows for items to be drag/dropped around in a listbox.

I'm also using MvvmLight , an EventTrigger and the EventToCommand class to capture the Tap event and execute a RelayCommand handler. Everything works fine when I have a plain old StackPanel as my item template for my listbox. However, as soon as I stick a ScrollViewer in there, my SelectedItem comes up as null. Is there a way to get the item that should be bound to that scroll viewer? Code below.

ViewModel

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>

Try that:

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

Height is important here since you need to specify it to make ScrollViewer working.

However that makes selecting working kinda bad? Like multiple items get selected... I don't know if it's bug with control itself or what.

I would try to use SelectionChanged event instead of Tap and get item which is selected for real there, then process it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM