【发布时间】:2017-08-17 11:00:41
【问题描述】:
我需要转到 wpf 一个 Windows 窗体应用程序。我在将相同样式的 DataGridView 制作为 Wpf DataGrid 时遇到了一些问题。
DataGridView 通过 DataSource 属性填充了一个列表。
public class GameItem
{
public string Name { get; set; }
public bool Selected { get; set; }
}
当 Selected = True 时,DGV 上的整行为红色,否则为 GreenYellow。 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;
}
}
}
这是 Grid 在 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 中,我不明白如何在行标题中显示行指示箭头。
在 wpf 中,当用户单击被选中的行并更改颜色时,我不想要这种行为。只有当有界的 Selected 项目属性为真时,我才希望一行是红色的。所以点击一行它不能改变颜色,背景颜色必须保持绿色,前景白色在数据源中是 Selected=false。
这是 Windows 窗体版本:
这是 wpf,第 1 行和第 3 行已正确绘制,因为绑定的项目已 Selected=True。问题是当单击一行时(图片中的 1943),即使 Selected 为假,它也会突出显示,我想避免活动行像 windows 窗体版本一样突出显示。
【问题讨论】:
标签: wpf datagridview wpfdatagrid