wpf listbox 无选中背景,及纵向横向排列完整实例

建议使用ItemsControl替代,ListBox限制太多

<
TabItem Header="我的Tab页" Name="tabItem1"> <TabItem.Resources> <SolidColorBrush x:Key="Item.MouseOver.Background" Color="Transparent"/> <SolidColorBrush x:Key="Item.MouseOver.Border" Color="Transparent"/> <SolidColorBrush x:Key="Item.SelectedInactive.Background" Color="Transparent"/> <SolidColorBrush x:Key="Item.SelectedInactive.Border" Color="Transparent"/> <SolidColorBrush x:Key="Item.SelectedActive.Background" Color="Transparent"/> <SolidColorBrush x:Key="Item.SelectedActive.Border" Color="Transparent"/> <Style x:Key="ListBoxTitleStyle" TargetType="{x:Type ListBoxItem}"> <Setter Property="SnapsToDevicePixels" Value="True"/> <Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/> <Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/> <Setter Property="Background" Value="Transparent"/> <Setter Property="BorderBrush" Value="Transparent"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type ListBoxItem}"> <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true"> <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/> </Border> <ControlTemplate.Triggers> <MultiTrigger> <MultiTrigger.Conditions> <Condition Property="IsMouseOver" Value="True"/> </MultiTrigger.Conditions> <Setter Property="Background" TargetName="Bd" Value="{StaticResource Item.MouseOver.Background}"/> <Setter Property="BorderBrush" TargetName="Bd" Value="{StaticResource Item.MouseOver.Border}"/> </MultiTrigger> <MultiTrigger> <MultiTrigger.Conditions> <Condition Property="Selector.IsSelectionActive" Value="False"/> <Condition Property="IsSelected" Value="True"/> </MultiTrigger.Conditions> <Setter Property="Background" TargetName="Bd" Value="{StaticResource Item.SelectedInactive.Background}"/> <Setter Property="BorderBrush" TargetName="Bd" Value="{StaticResource Item.SelectedInactive.Border}"/> </MultiTrigger> <MultiTrigger> <MultiTrigger.Conditions> <Condition Property="Selector.IsSelectionActive" Value="True"/> <Condition Property="IsSelected" Value="True"/> </MultiTrigger.Conditions> <Setter Property="Background" TargetName="Bd" Value="{StaticResource Item.SelectedActive.Background}"/> <Setter Property="BorderBrush" TargetName="Bd" Value="{StaticResource Item.SelectedActive.Border}"/> </MultiTrigger> <Trigger Property="IsEnabled" Value="False"> <Setter Property="TextElement.Foreground" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> </TabItem.Resources> <ListBox x:Name="lstTypes" ScrollViewer.CanContentScroll="false" HorizontalContentAlignment="Stretch" ItemContainerStyle="{StaticResource ListBoxTitleStyle}" > <ListBox.Template> <ControlTemplate TargetType="{x:Type ListBox}"> <ScrollViewer HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Disabled"> <StackPanel Orientation="Vertical" IsItemsHost="True" ScrollViewer.CanContentScroll="False" /> </ScrollViewer> </ControlTemplate> </ListBox.Template> <ListBox.ItemTemplate> <DataTemplate> <GroupBox Grid.Row="0" Header="{Binding Path=societyType.name}" Margin="10" Padding="10"> <ListBox x:Name="lstSocieties" ItemsSource="{Binding Path=societies}" BorderThickness="0"> <ListBox.Template> <ControlTemplate TargetType="{x:Type ListBox}"> <ScrollViewer HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto"> <WrapPanel Orientation="Horizontal" IsItemsHost="True" ScrollViewer.CanContentScroll="True"/> </ScrollViewer> </ControlTemplate> </ListBox.Template> <ListBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Vertical"> <TextBlock Name="tbSociety" Text="{Binding Path=name}" Padding="5" /> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </GroupBox> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </TabItem>

 

posted on 2022-06-21 19:01  空明流光  阅读(419)  评论(0编辑  收藏  举报

导航