开发者

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>
              开发者_StackOverflow社区  </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>


The ListBoxItem will become selected when the ListBox receives a click event. However a button inside the template will receive and handle the click event. Hence the ListBox doesn't get to see the event.

Since you are invoking a command on the view model consider having the view model set the current item as the selected item.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜