带有环绕面板的列表视图的选择样式
本文关键字:列表 视图 选择 样式 | 更新日期: 2023-09-27 18:32:59
我正在使用带有WrapPanel
的ListView
作为其ItemsPanel
。我需要更改所选项目的样式 - 使用不同的背景颜色并在所选项目周围添加边框,就像 Windows 7 的资源管理器一样。
<ListView ItemsSource="{Binding Items}" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel IsItemsHost="True" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical" Margin="10">
<Rectangle Width="100" Height="100" Fill="Pink" />
<TextBlock Text="{Binding Caption}" Margin="0,10,0,0" />
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
只需为默认类型和选定类型设计ControlTemplate
即可。然后,可以在选择列表视图中的任何项时设置"选定"ControlTemplate
,否则保留"默认类型"。
<Window.Resources>
<ControlTemplate x:Key="DEFAULT">
<StackPanel Orientation="Vertical" Margin="10">
<Rectangle Width="100" Height="100" Fill="Green" />
<TextBlock Text="{Binding Caption}" Margin="0,10,0,0" />
</StackPanel>
</ControlTemplate>
<ControlTemplate x:Key="SELECTED_TYPE">
<Border BorderBrush="Gray" BorderThickness="1">
<StackPanel Orientation="Vertical" Margin="10">
<Rectangle Width="100" Height="100" Fill="Pink" />
<TextBlock Text="{Binding Caption}" Margin="0,10,0,0" />
</StackPanel>
</Border>
</ControlTemplate>
<Style x:Key="ListItemStyle" TargetType="{x:Type ListBoxItem}">
<Setter Property="Background" Value="White"/>
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Template" Value="{StaticResource SELECTED_TYPE}"/>
<Setter Property="Background" Value="Orange"/>
</Trigger>
<Trigger Property="IsSelected" Value="False">
<Setter Property="Template" Value="{StaticResource DEFAULT}"/>
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<ListView ItemsSource="{Binding Items}" ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ItemContainerStyle="{StaticResource ListItemStyle}">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel IsItemsHost="True" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>