需要帮助根据绑定 属性 管理 wpf DataGrid 中的选定行样式
need help to manage selected row style in wpf DataGrid depending on bound property
我需要移动到 wpf windows 表单应用程序。我在将相同样式的 DataGridView 制作成 Wpf DataGrid 时遇到了一些问题。
DataGridView 通过 DataSource 使用列表填充 属性。
public class GameItem
{
public string Name { get; set; }
public bool Selected { get; set; }
}
当 Selected = True 时,DGV 上的整行为红色,否则为绿色黄色。
SelectionColor 始终与标准颜色相同,因此在行之间移动不会改变任何颜色。
这是 DefaultCellStyle:
dataGridViewCellStyle4.BackColor = System.Drawing.Color.GreenYellow;
dataGridViewCellStyle4.ForeColor = System.Drawing.SystemColors.ControlText;
dataGridViewCellStyle4.SelectionBackColor = System.Drawing.Color.GreenYellow;
dataGridViewCellStyle4.SelectionForeColor = System.Drawing.SystemColors.ControlText;
void dgvDetail_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.RowIndex >= 0)
{
var gameItem = (sender as DataGridView).Rows[e.RowIndex].DataBoundItem as GameItem;
if (gameItem != null && gameItem.Selected)
{
e.CellStyle.SelectionForeColor = Color.White;
e.CellStyle.SelectionBackColor = Color.Red;
e.CellStyle.ForeColor = Color.White;
e.CellStyle.BackColor = Color.Red;
}
}
}
这是网格在 wpf 应用程序中的定义:
<Window x:Class="TestGrid.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:TestGrid"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Red"/>
<Style TargetType="DataGrid">
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="Red"/>
</Style.Resources>
<Setter Property="FontSize" Value="10pt" ></Setter>
<Setter Property="FontFamily" Value="Microsoft Sans Serif" ></Setter>
<Setter Property="FontWeight" Value="Bold" ></Setter>
<Setter Property="SelectionUnit" Value="FullRow" ></Setter>
<Setter Property="HeadersVisibility" Value="Row" ></Setter>
<Setter Property="AutoGenerateColumns" Value="False" ></Setter>
</Style>
</Window.Resources>
<Grid>
<DataGrid Name="dg1">
<DataGrid.RowStyle>
<Style TargetType="{x:Type DataGridRow}">
<Setter Property="Background" Value="GreenYellow"></Setter>
<Style.Triggers>
<DataTrigger Binding="{Binding Selected}" Value="True">
<Setter Property="Background" Value="Red"/>
<Setter Property="Foreground" Value="White"/>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
<DataGrid.Columns>
<DataGridTextColumn Header="Description" Binding="{Binding Description}" Width="Auto" />
<DataGridTextColumn Header="Manufacturer" Binding="{Binding Manufacturer}" Width="*" />
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
我有这个问题:
在 wpf 中,我不明白如何将行指示器箭头显示到行 header。
在 wpf 中,当用户单击被选中的行并更改颜色时,我不希望出现这种行为。我希望仅当有界选定项 属性 为真时,一行才为红色。所以点击一行它不能改变颜色,背景色必须保持绿色,前景白色在数据源中被选中=false。
这是 windows 表格版本:
这是 wpf,正确绘制了第 1 行和第 3 行,因为绑定项的 Selected=True。问题是当一行被点击时(图片中的 1943)即使 Selected 为 false,它也会突出显示,我想避免活动行像 windows 表单版本一样突出显示。
要禁用鼠标选择时的红色突出显示,请移除 HighlightBrushKey 资源(或至少更改其颜色以查看区别)
更改行 headers 为 DataGridRowHeader 类型创建样式:
<Style TargetType="{x:Type DataGridRowHeader}" BasedOn="{StaticResource {x:Type DataGridRowHeader}}">
<Setter Property="BorderBrush" Value="Black"/>
<Setter Property="BorderThickness" Value="0,0,1,1"/>
<Setter Property="Background" Value="GreenYellow"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Selected}" Value="True">
<Setter Property="Background" Value="Red"/>
</DataTrigger>
<Trigger Property="IsRowSelected" Value="True">
<Setter Property="Content" Value="▸"/>
<Setter Property="Background" Value="{StaticResource {x:Static SystemColors.HighlightBrushKey}}"/>
</Trigger>
</Style.Triggers>
</Style>
当 Selected 属性 为真时,行 header 为红色,否则为绿色。当使用输入选择行时,它显示黑色 right-pointing 小三角形字符。
或者甚至可能隐藏行 headers 并使用 DataGridTemplateColumn 而不是所选单元格将显示 >
修改行和单元格样式以更改选择前景
<DataGrid.RowStyle>
<Style TargetType="{x:Type DataGridRow}">
<Setter Property="Background" Value="GreenYellow"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Selected}" Value="True">
<Setter Property="Background" Value="Red"/>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="Foreground" Value="Black"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Selected}" Value="True">
<Setter Property="Foreground" Value="White"/>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.CellStyle>
我需要移动到 wpf windows 表单应用程序。我在将相同样式的 DataGridView 制作成 Wpf DataGrid 时遇到了一些问题。
DataGridView 通过 DataSource 使用列表填充 属性。
public class GameItem
{
public string Name { get; set; }
public bool Selected { get; set; }
}
当 Selected = True 时,DGV 上的整行为红色,否则为绿色黄色。 SelectionColor 始终与标准颜色相同,因此在行之间移动不会改变任何颜色。
这是 DefaultCellStyle:
dataGridViewCellStyle4.BackColor = System.Drawing.Color.GreenYellow;
dataGridViewCellStyle4.ForeColor = System.Drawing.SystemColors.ControlText;
dataGridViewCellStyle4.SelectionBackColor = System.Drawing.Color.GreenYellow;
dataGridViewCellStyle4.SelectionForeColor = System.Drawing.SystemColors.ControlText;
void dgvDetail_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.RowIndex >= 0)
{
var gameItem = (sender as DataGridView).Rows[e.RowIndex].DataBoundItem as GameItem;
if (gameItem != null && gameItem.Selected)
{
e.CellStyle.SelectionForeColor = Color.White;
e.CellStyle.SelectionBackColor = Color.Red;
e.CellStyle.ForeColor = Color.White;
e.CellStyle.BackColor = Color.Red;
}
}
}
这是网格在 wpf 应用程序中的定义:
<Window x:Class="TestGrid.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:TestGrid"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Red"/>
<Style TargetType="DataGrid">
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="Red"/>
</Style.Resources>
<Setter Property="FontSize" Value="10pt" ></Setter>
<Setter Property="FontFamily" Value="Microsoft Sans Serif" ></Setter>
<Setter Property="FontWeight" Value="Bold" ></Setter>
<Setter Property="SelectionUnit" Value="FullRow" ></Setter>
<Setter Property="HeadersVisibility" Value="Row" ></Setter>
<Setter Property="AutoGenerateColumns" Value="False" ></Setter>
</Style>
</Window.Resources>
<Grid>
<DataGrid Name="dg1">
<DataGrid.RowStyle>
<Style TargetType="{x:Type DataGridRow}">
<Setter Property="Background" Value="GreenYellow"></Setter>
<Style.Triggers>
<DataTrigger Binding="{Binding Selected}" Value="True">
<Setter Property="Background" Value="Red"/>
<Setter Property="Foreground" Value="White"/>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
<DataGrid.Columns>
<DataGridTextColumn Header="Description" Binding="{Binding Description}" Width="Auto" />
<DataGridTextColumn Header="Manufacturer" Binding="{Binding Manufacturer}" Width="*" />
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
我有这个问题:
在 wpf 中,我不明白如何将行指示器箭头显示到行 header。
在 wpf 中,当用户单击被选中的行并更改颜色时,我不希望出现这种行为。我希望仅当有界选定项 属性 为真时,一行才为红色。所以点击一行它不能改变颜色,背景色必须保持绿色,前景白色在数据源中被选中=false。
这是 windows 表格版本:
这是 wpf,正确绘制了第 1 行和第 3 行,因为绑定项的 Selected=True。问题是当一行被点击时(图片中的 1943)即使 Selected 为 false,它也会突出显示,我想避免活动行像 windows 表单版本一样突出显示。
要禁用鼠标选择时的红色突出显示,请移除 HighlightBrushKey 资源(或至少更改其颜色以查看区别)
更改行 headers 为 DataGridRowHeader 类型创建样式:
<Style TargetType="{x:Type DataGridRowHeader}" BasedOn="{StaticResource {x:Type DataGridRowHeader}}">
<Setter Property="BorderBrush" Value="Black"/>
<Setter Property="BorderThickness" Value="0,0,1,1"/>
<Setter Property="Background" Value="GreenYellow"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Selected}" Value="True">
<Setter Property="Background" Value="Red"/>
</DataTrigger>
<Trigger Property="IsRowSelected" Value="True">
<Setter Property="Content" Value="▸"/>
<Setter Property="Background" Value="{StaticResource {x:Static SystemColors.HighlightBrushKey}}"/>
</Trigger>
</Style.Triggers>
</Style>
当 Selected 属性 为真时,行 header 为红色,否则为绿色。当使用输入选择行时,它显示黑色 right-pointing 小三角形字符。
或者甚至可能隐藏行 headers 并使用 DataGridTemplateColumn 而不是所选单元格将显示 >
修改行和单元格样式以更改选择前景
<DataGrid.RowStyle>
<Style TargetType="{x:Type DataGridRow}">
<Setter Property="Background" Value="GreenYellow"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Selected}" Value="True">
<Setter Property="Background" Value="Red"/>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="Foreground" Value="Black"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Selected}" Value="True">
<Setter Property="Foreground" Value="White"/>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.CellStyle>