WPF - ListView 选择样式
我使用的是具有两列的列表视图控件,一列是文本,一列有一个矩形初始化。 当选择一个项目时,我希望该项目的内容为白色(第一列中的文本和第二列中的矩形),但发生的情况是只有文本变白。
这是我的 XAML:
<Window x:Class="Selection.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" >
<Grid>
<Grid.Resources>
<ResourceDictionary>
<Style x:Key="@ListViewItemStyle" TargetType="{x:Type ListViewItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType='{x:Type ListViewItem}'>
<Grid SnapsToDevicePixels="True" Margin="0">
<Border x:Name="Bd" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}" />
<GridViewRowPresenter x:Name="Content" TextBlock.Foreground="{TemplateBinding Foreground}"
Content="{TemplateBinding Content}" Columns="{TemplateBinding GridView.ColumnCollection}" />
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="true">
<Setter Property="TextElement.Foreground" Value="White" TargetName="Content" />
<Setter Property="Background" Value="DarkGray" TargetName="Bd"/>
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsSelected" Value="true" />
<Condition Property="Selector.IsSelectionActive" Value="false" />
</MultiTrigger.Conditions>
<Setter Property="Background" TargetName="Bd"
Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" />
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" />
</MultiTrigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<DataTemplate x:Key="@TextCellTemplate">
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
<DataTemplate x:Key="@TrubleCellTemplate">
<Rectangle Width="20" Height="20" Fill="Black"></Rectangle>
</DataTemplate>
</ResourceDictionary>
</Grid.Resources>
<ListView ItemsSource="{Binding Persons}" Style="{DynamicResource @ListView}" ItemContainerStyle="{DynamicResource @ListViewItemStyle}">
<ListView.View>
<GridView>
<GridViewColumn Width="40" CellTemplate="{DynamicResource @TextCellTemplate}" />
<GridViewColumn Width="131" CellTemplate="{DynamicResource @TrubleCellTemplate}" />
</GridView>
</ListView.View>
</ListView>
</Grid>
这是背后的代码:
public partial class MainWindow : Window
{
public List<Person> Persons { get; set; }
public MainWindow()
{
Persons = new List<Person> {new Person {Name = "Ashton"}};
DataContext = this;
InitializeComponent();
}
}
public class Person
{
public string Name { get; set; }
}
I'm using a Listview Control with two columns, one is text and one has a rectangle init.
When an Item is selected I want the content of the item to be white (both the text in column 1 and the rectangle in column 2) but what happens is that only the text is getting white.
This is my XAML:
<Window x:Class="Selection.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" >
<Grid>
<Grid.Resources>
<ResourceDictionary>
<Style x:Key="@ListViewItemStyle" TargetType="{x:Type ListViewItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType='{x:Type ListViewItem}'>
<Grid SnapsToDevicePixels="True" Margin="0">
<Border x:Name="Bd" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}" />
<GridViewRowPresenter x:Name="Content" TextBlock.Foreground="{TemplateBinding Foreground}"
Content="{TemplateBinding Content}" Columns="{TemplateBinding GridView.ColumnCollection}" />
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="true">
<Setter Property="TextElement.Foreground" Value="White" TargetName="Content" />
<Setter Property="Background" Value="DarkGray" TargetName="Bd"/>
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsSelected" Value="true" />
<Condition Property="Selector.IsSelectionActive" Value="false" />
</MultiTrigger.Conditions>
<Setter Property="Background" TargetName="Bd"
Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" />
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" />
</MultiTrigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<DataTemplate x:Key="@TextCellTemplate">
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
<DataTemplate x:Key="@TrubleCellTemplate">
<Rectangle Width="20" Height="20" Fill="Black"></Rectangle>
</DataTemplate>
</ResourceDictionary>
</Grid.Resources>
<ListView ItemsSource="{Binding Persons}" Style="{DynamicResource @ListView}" ItemContainerStyle="{DynamicResource @ListViewItemStyle}">
<ListView.View>
<GridView>
<GridViewColumn Width="40" CellTemplate="{DynamicResource @TextCellTemplate}" />
<GridViewColumn Width="131" CellTemplate="{DynamicResource @TrubleCellTemplate}" />
</GridView>
</ListView.View>
</ListView>
</Grid>
This is the code behind:
public partial class MainWindow : Window
{
public List<Person> Persons { get; set; }
public MainWindow()
{
Persons = new List<Person> {new Person {Name = "Ashton"}};
DataContext = this;
InitializeComponent();
}
}
public class Person
{
public string Name { get; set; }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将 IsSelected 的 DataTrigger 添加到 @TrubleCellTemplate 中,如下所示
You can add a DataTrigger for IsSelected to your @TrubleCellTemplate like this