使用LongListSelector进行事件处理,使用复选框进行列表项

时间:2012-02-18 12:39:20

标签: windows-phone-7 selectionchanged longlistselector

我有一个LongListSelector,其中包含以下项目模板:

    <DataTemplate x:Key="stopItemTemplate">
        <Grid Margin="{StaticResource PhoneTouchTargetOverhang}">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <CheckBox VerticalAlignment="Top" HorizontalAlignment="Left" IsChecked="{Binding Checked}" Click="AlarmActivationClicked" />
            <StackPanel Grid.Column="1" VerticalAlignment="Top">
                <TextBlock Text="{Binding Stop.Name}" Style="{StaticResource PhoneTextLargeStyle}" FontFamily="{StaticResource PhoneFontFamilySemiBold}" TextWrapping="Wrap" Margin="12,-12,12,6"/>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="Distanz:" Style="{StaticResource PhoneTextSmallStyle}" FontFamily="{StaticResource PhoneFontFamilySemiBold}" Width="120"/>
                    <TextBlock Text="{Binding Distance, Converter={StaticResource MyStringFormatConverter}, ConverterParameter=:1000:\{0:0.0\} km}" Style="{StaticResource PhoneTextSmallStyle}" FontFamily="{StaticResource PhoneFontFamilySemiBold}"/>
                </StackPanel>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="Alarm:" Style="{StaticResource PhoneTextSmallStyle}" FontFamily="{StaticResource PhoneFontFamilySemiBold}" Width="120"/>
                    <TextBlock Text="{Binding Alarm, Converter={StaticResource MyBooleanStringConverter}}" Style="{StaticResource PhoneTextSmallStyle}" FontFamily="{StaticResource PhoneFontFamilySemiBold}"/>
                </StackPanel>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="Vibration:" Style="{StaticResource PhoneTextSmallStyle}" FontFamily="{StaticResource PhoneFontFamilySemiBold}" Width="120"/>
                    <TextBlock Text="{Binding Vibration, Converter={StaticResource MyBooleanStringConverter}}" Style="{StaticResource PhoneTextSmallStyle}" FontFamily="{StaticResource PhoneFontFamilySemiBold}"/>
                </StackPanel>
            </StackPanel>
        </Grid>
    </DataTemplate>

使用如下:

<controls:PivotItem Header="ziele">
    <toolkit:LongListSelector x:Name="alarmList" Background="Transparent" IsFlatList="True"
        ItemTemplate="{StaticResource stopItemTemplate}" SelectionChanged="AlarmListSelectionChanged" />
</controls:PivotItem>

SelectionChanged事件:

    private void AlarmListSelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        var guid = ((AlarmItem)alarmList.SelectedItem).Id;
        NavigationService.Navigate(new Uri(string.Concat("/AlarmDetailPage.xaml?id=", guid), UriKind.Relative));
    }

要更改项目的详细信息,我已订阅LongListSelector的SelectionChanged事件。这符合预期。

但是,如果我点击复选框 - 首先会触发复选框的单击事件,但我无法识别单击了哪个复选框(属性longlistselector.SelectedItem已经过时),而且在SelectionChanged事件触发后,尝试导航到详细页面,就像我打算这样做一样。

如何将这些与事件分开以避免同时发射?我如何获得相应的数据项?

非常感谢...
亲切的问候,丹尼

2 个答案:

答案 0 :(得分:2)

您不需要收听SelectionChanged事件。在ClickCheckedUnchecked事件处理程序中,您可以获得一个项目:

private void CheckBox_Checked(object sender, System.Windows.RoutedEventArgs e)
{
    var guid = ((sender as CheckBox).DataContext as AlarmItem).Id;
    NavigationService.Navigate(new Uri(string.Concat("/AlarmDetailPage.xaml?id=", guid), UriKind.Relative));
}

答案 1 :(得分:1)

在AlarmActivationClicked(复选框上的事件处理程序)中,您将能够获取单击列表中项目的数据项。

    private void AlarmActivationClicked(object sender, RoutedEventArgs e)
    {
        var dataItem = ((FrameworkElement)sender).DataContext;
    }

我没有看到在调用AlarmActivationClicked方法后调用事件AlarmListSelectionChanged方法。如果单击复选框,复选框将更改为选中,但它不会更改列表框中的选择。