views:

13

answers:

1

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

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.

AnthonyWJones
Is there other solution? Do you mean, that I need to send a message to the parent ViewModel to set the SelectedItem?
SuperXMan
Yes that is what I mean. Your use of `EventCommand` leads me to the conclusion that your prefer to not put code in your view. There is not other solution that would avoid writing more code and given the choice its better to put the code in the ViewModel than in the View itself.
AnthonyWJones