ListBox with ItemTemplate: SelectionChanged not fired

ListBox with ItemTemplate: SelectionChanged not fired. What's wrong?

<ListBox ItemsSource="{Binding Source1}" SelectionChanged="ListBox_SelectionChanged" SelectedItem="{Binding CurrentItem, Mode=TwoWay}" HorizontalAlignment="Stretch" Margin="0" Padding="0">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid Margin="0">

                <Grid.RowDefinitions>
                    <RowDefinition />
                    <RowDefinition Height="1" />
                </Grid.RowDefinitions>

                <Button Grid.Row="0" BorderThickness="0" Background="Transparent" HorizontalAlignment="Stretch">
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="Click">
                            <cmd:EventToCommand Command="{Binding FirstCommand}" PassEventArgsToCommand="True" />
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
                    <Button.Template>
                        <ControlTemplate>
                            <Grid HorizontalAlignment="Stretch">

                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*"/>
                                    <ColumnDefinition Width="65" />
                                    <ColumnDefinition Width="30"/>
                                </Grid.ColumnDefinitions>

                                <TextBlock Grid.Row="0" Grid.Column="0" Text="{Binding Title}" Style="{StaticResource ListBoxTextStyle}" />
                                <TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Title1}" Margin="5,0" FontSize="25" HorizontalAlignment="Center" VerticalAlignment="Center" />

                            </Grid>
                        </ControlTemplate>
                    </Button.Template>
                </Button>

                <Border Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="3" BorderBrush="#FFFFFF" HorizontalAlignment="Stretch" BorderThickness="0,1,0,0" />

            </Grid>

        </DataTemplate>
    </ListBox.ItemTemplate>
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter>
        </Style>
    </ListBox.ItemContainerStyle>

</ListBox>
+3
source share
1 answer

ListBoxItemwill be selected when it ListBoxreceives the click event. However, the button inside the template will receive and handle the click event. Therefore, ListBoxdoes not see the event.

Since you are invoking a command in a view model, consider that the view model defines the current item as the selected item.

+3
source

Source: https://habr.com/ru/post/1771674/


All Articles