【问题标题】:need help to manage selected row style in wpf DataGrid depending on bound property需要帮助来根据绑定属性管理 wpf DataGrid 中的选定行样式
【发布时间】: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


    【解决方案1】:

    要禁用鼠标选择时的红色突出显示,请删除 HighlightBrushKey 资源(或至少更改其颜色以查看差异)

    要更改行标题,请为 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="&#9656;"/>                
                <Setter Property="Background" Value="{StaticResource {x:Static SystemColors.HighlightBrushKey}}"/>
            </Trigger>
        </Style.Triggers>
    </Style>
    

    当 Selected 属性为真时,行标题为红色,否则为绿色黄色。当使用的输入选择行时,它会显示黑色的右指小三角字符。

    或者甚至可能隐藏行标题并使用 DataGridTemplateColumn 代替所选单元格将显示&gt;


    修改行和单元格样式以更改选择前景

    <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>
    

    【讨论】:

    • 一切正常,除了当绿色行成为 selectedRow 时,文本从黑色变为白色,我希望通过鼠标选择行时文本不会改变。所以我添加了这个 。现在绿色行很好,但是当看到红色行时,文本变为黑色,即使 DataGridRow 样式中的 DataTrigger 设置文本 Foreground->white
    猜你喜欢
    • 2014-08-15
    • 2010-12-06
    • 2012-11-18
    • 1970-01-01
    • 1970-01-01
    • 2011-05-31
    • 2013-03-10
    • 2011-03-16
    • 2013-02-28
    相关资源
    最近更新 更多