Pergunta

For my listbox in windows phone I added a checkbox for each item with the help of this tutorial (option 2). Before I already had a SelectionChanged event for my listbox. How do I prevent firing the event, when the user just checks the checkbox? The SelectionChanged event should only fire when he hits the textbox in the listbox, but not the checkbox.

Thats my listbox:

                <ListBox x:Name="toDoItemsListBox" ItemsSource="{Binding Source={StaticResource cvs}}" Grid.Row="1" Margin="12, 0, 12, 0" Width="440" SelectionChanged="goToNavigation" IsSynchronizedWithCurrentItem="False">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal" >
                            <CheckBox IsChecked="{Binding IsFavorite}" Height="48" Width="48" Checked="CheckBox_Checked" Unchecked="CheckBox_Unchecked" Style="{StaticResource CheckBoxStyle1}"/>
                            <StackPanel Orientation="Vertical">
                                <TextBlock Text="{Binding Shortcut}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}" />
                                <TextBlock Text="{Binding BuildingName}" TextWrapping="Wrap" Style="{StaticResource PhoneTextSubtleStyle}" />
                            </StackPanel>
                        </StackPanel>
                    </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>

These are my check events:

        private void CheckBox_Checked(object sender, RoutedEventArgs e)
    {
        ListBoxItem checedItem = this.toDoItemsListBox.ItemContainerGenerator.ContainerFromItem((sender as CheckBox).DataContext) as ListBoxItem;
        if (checedItem != null)
        {
            checedItem.IsSelected = true;
        }
    }

    private void CheckBox_Unchecked(object sender, RoutedEventArgs e)
    {
        ListBoxItem checedItem = this.toDoItemsListBox.ItemContainerGenerator.ContainerFromItem((sender as CheckBox).DataContext) as ListBoxItem;
        if (checedItem != null)
        {
            checedItem.IsSelected = false;
        }
    }

Also I have to mention that the selectionChanged event raises only when I check the checkbox. Not when I uncheck it.

Foi útil?

Solução

I made it work without the selection changed event. As alternative I used a tap event:

                           <StackPanel Orientation="Vertical" Tap="StackPanel_Tap">
                                <TextBlock Text="{Binding Shortcut}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}" />
                                <TextBlock Text="{Binding BuildingName}" TextWrapping="Wrap" Style="{StaticResource PhoneTextSubtleStyle}" />
                            </StackPanel>

and in Code behind I get my listitem with:

        private void StackPanel_Tap(object sender, System.Windows.Input.GestureEventArgs e)
    {
        // Save current POI.
        Object object = (sender as StackPanel).DataContext as Object;
     ...}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top