WPF_ListBox实现单击选中,重复单击取消选中效果(MVVM模式)

时间:2024-10-08 07:03:56
<UserControl x:Class="_5_RoutedEventTest." xmlns="/winfx/2006/xaml/presentation" xmlns:x="/winfx/2006/xaml" xmlns:mc="/markup-compatibility/2006" xmlns:d="/expression/blend/2008" mc:Ignorable="d" d:DesignHeight="450" d:DesignWidth="800"> <> <Style TargetType="ListBox" x:Key="list"> <Setter Property="ItemTemplate"> <> <DataTemplate> <StackPanel Orientation="Horizontal"> <TextBlock Text="{Binding ID}" Margin="5"/> <TextBlock Text="{Binding Name}" Margin="5"/> </StackPanel> </DataTemplate> </> </Setter> <Setter Property="ItemContainerStyle"> <> <Style TargetType="ListBoxItem"> <> <Setter Property="IsSelected" Value="{Binding IsSelected}"/> <EventSetter Event="PreviewMouseLeftButtonDown" Handler="ListBoxItem_PreviewMouseLeftButtonDown"/> </> </Style> </> </Setter> </Style> </> <Grid> <StackPanel Orientation="Vertical"> <ListBox ItemsSource="{Binding TestCollection}" Style="{StaticResource list}"/> </StackPanel> </Grid> </UserControl>