带有ItemTemplate的ListBox:SelectionChanged未被触发

时间:2010-10-27 10:08:51

标签: silverlight data-binding

使用ItemTemplate的ListBox:SelectionChanged未被触发 怎么了?

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

1 个答案:

答案 0 :(得分:3)

ListBoxItem收到点击事件时,ListBox将被选中。但是,模板内的按钮将接收并处理click事件。因此ListBox无法看到该事件。

由于您在视图模型上调用命令,请考虑让视图模型将当前项设置为所选项。