c# - 如何根据其值在 AutoGeneratingColumn 事件期间设置数据网格单元格的背景?

标签 c# wpf datagrid background autogeneratecolumn

我仍在与细胞背景的操纵作斗争,所以我要问一个新问题。

用户“H.B.”写道我实际上可以在 AutoGeneratingColumn 期间设置单元格样式事件 - Change DataGrid cell colour based on values .问题是我不确定该怎么做。

我想要的: 根据其值为每个单元格设置不同的背景颜色。如果该值为 null,我也希望它可点击的(我猜是可聚焦的)。

我拥有的/我正在尝试做的:

private void mydatagrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
    foreach (Cell cell in e.Column)
    {
        if (cell.Value < 1)
        { 
            cell.Background = Color.Black; 
            cell.isFocusable = false; 
        } 
        else
        {
            cell.Background = Color.Pink;
        }
    }
}

这只是伪代码。在列自动生成过程中是否可能发生这样的事情?如果是的话,我该如何编辑我的代码以使其有效?

我阅读了有关值转换器的内容,但我想知道它是否可以通过编程方式实现,而无需编写 XAML。

请理解,我仍然是 C#/WPF/DataGrid 的初学者。

解决方案第 1 部分:

我使用了我接受的答案。把它放进去

<Window.Resources> 
<local:ValueColorConverter x:Key="colorConverter"/>
        <Style x:Key="DataGridCellStyle1" TargetType="{x:Type DataGridCell}"> 
            <Setter Property="Padding" Value="5"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type DataGridCell}">
                        <Border Padding="{TemplateBinding Padding}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" SnapsToDevicePixels="True">
                            <Border.Background>
                                <MultiBinding Converter="{StaticResource colorConverter}">
                                    <Binding RelativeSource="{RelativeSource AncestorType=DataGridCell}" Path="Content.Text"/>
                                    <Binding RelativeSource="{RelativeSource AncestorType=DataGridCell}" Path="IsSelected"/>
                                </MultiBinding>
                            </Border.Background>
                            <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
</Window.Resources>

并为其制作了一个 MultiBinding 转换器,这样我还可以为选定的单元格设置背景颜色。

问题:

现在我只需要解决设置空单元格焦点的问题。有什么提示吗?

  <Style.Triggers>
        <Trigger Property="HasContent" Value="False">
            <Setter Property="Focusable" Value="False"/>
        </Trigger>
    </Style.Triggers>

这行不通。我在空单元格中有空字符串,但它们充满了“null”所以它应该可以工作,对吧?或者我做错了什么:| ?

解决方案第 2 部分:

所以只要单元格值为“TextBox”,上面的代码就无法工作,所以我决定找到另一种方法来处理它,可以在我的答案中找到:https://stackoverflow.com/a/16673602/2296407

感谢你试图帮助我:)

最佳答案

我可以为你的问题提出两种不同的解决方案:第一种是“代码隐藏式”(你要求的但我个人认为这不是 WPF 中的正确方法)和更多 WPF 式(更棘手但保持代码隐藏干净并利用样式、触发器和转换器)

解决方案 1. 着色的事件处理和代码隐藏逻辑

首先,您选择的方法不会直接起作用 - AutoGeneratingColumn 事件旨在用于更改整个列 的外观,而不是逐个单元格的基础。所以它可以用于,比如说,根据它的显示索引或绑定(bind)属性将正确的样式附加到整个列。

一般来说,第一次引发事件时,您的数据网格根本没有任何行(因此也没有单元格)。如果您确实需要捕获该事件 - 请考虑您的 DataGrid.LoadingRow 事件。而且您将无法轻松获得细胞 :)

那么,你要做的是:处理 LoadingRow 事件,获取行(它有 Item 属性保存(令人惊讶的是:))你的绑定(bind)项),获取绑定(bind)项,使所有需要的计算,得到你需要修改的单元格,最后修改目标单元格的样式。

这是代码(作为项目,我使用了一个示例对象,该对象具有我用于着色的 int“Value”属性)。

XAML

   <DataGrid Name="mygrid" ItemsSource="{Binding Items}" AutoGenerateColumns="True" LoadingRow="DataGrid_OnLoadingRow"/>

.CS

    private void DataGrid_OnLoadingRow(object sender, DataGridRowEventArgs e)
    {
        Dispatcher.BeginInvoke(DispatcherPriority.Render, new Action(() => AlterRow(e)));
    }

    private void AlterRow(DataGridRowEventArgs e)
    {
        var cell = GetCell(mygrid, e.Row, 1);
        if (cell == null) return;

        var item = e.Row.Item as SampleObject;
        if (item == null) return;

        var value = item.Value;

        if (value <= 1) cell.Background = Brushes.Red;
        else if (value <= 2) cell.Background = Brushes.Yellow;
        else cell.Background = Brushes.Green;
    }

    public static DataGridRow GetRow(DataGrid grid, int index)
    {
        var row = grid.ItemContainerGenerator.ContainerFromIndex(index) as DataGridRow;

        if (row == null)
        {
            // may be virtualized, bring into view and try again
            grid.ScrollIntoView(grid.Items[index]);
            row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(index);
        }
        return row;
    }

    public static T GetVisualChild<T>(Visual parent) where T : Visual
    {
        T child = default(T);
        int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
        for (int i = 0; i < numVisuals; i++)
        {
            var v = (Visual)VisualTreeHelper.GetChild(parent, i);
            child = v as T ?? GetVisualChild<T>(v);
            if (child != null)
            {
                break;
            }
        }
        return child;
    }

    public static DataGridCell GetCell(DataGrid host, DataGridRow row, int columnIndex)
    {
        if (row == null) return null;

        var presenter = GetVisualChild<DataGridCellsPresenter>(row);
        if (presenter == null) return null;

        // try to get the cell but it may possibly be virtualized
        var cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex);
        if (cell == null)
        {
            // now try to bring into view and retreive the cell
            host.ScrollIntoView(row, host.Columns[columnIndex]);
            cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex);
        }
        return cell;

    }

解决方案 2. WPF 样式

此解决方案仅将代码隐藏用于值到颜色的转换(假设着色逻辑比相等比较更复杂 - 在这种情况下,您可以使用触发器并且不要弄乱转换器)。

您所做的:使用包含数据触发器的样式设置 DataGrid.CellStyle 属性,它检查单元格是否在所需的列中(基于它的 DisplayIndex),如果是 - 通过转换器应用背景。

XAML

<DataGrid Name="mygrid" ItemsSource="{Binding Items}" AutoGenerateColumns="True">
        <DataGrid.Resources>
            <local:ValueColorConverter x:Key="colorconverter"/>
        </DataGrid.Resources>
        <DataGrid.CellStyle>
            <Style TargetType="DataGridCell">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Column.DisplayIndex}" Value="1">
                        <Setter Property="Background" Value="{Binding RelativeSource={RelativeSource Self}, Path=Content.Text, Converter={StaticResource colorconverter}}"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </DataGrid.CellStyle>
    </DataGrid>

.CS

public class ValueColorConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var str = value as string;
        if (str == null) return null;

        int intValue;
        if (!int.TryParse(str, out intValue)) return null;

        if (intValue <= 1) return Brushes.Red;
        else if (intValue <= 2) return Brushes.Yellow;
        else return Brushes.Green;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

UPD:如果您需要为整个数据网格着色,XAML 更容易(无需使用触发器)。使用以下 CellStyle:

    <DataGrid.CellStyle>
            <Style TargetType="DataGridCell">
                 <Setter Property="Background" Value="{Binding RelativeSource={RelativeSource Self}, Path=Content.Text, Converter={StaticResource colorconverter}}"/>
            </Style>
    </DataGrid.CellStyle>

关于c# - 如何根据其值在 AutoGeneratingColumn 事件期间设置数据网格单元格的背景?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16645688/

相关文章:

c# - 如何在 WPF 数据网格中为每列添加一个 ContextMenu

c# - golang net.Conn 没有数据可用属性

c# - 如何通过沙箱从托管代码调用外部非托管应用程序?

wpf - 24/7计算机,中介元素WPF出现问题,在3小时后变黑

.net - 如何在不关注其他内容的情况下取消对 TextBox 的关注

c# - 动态添加 DataGrid 日期时间列

c# - Linq 查询以列形式返回国家/地区,以行形式返回城市并在单元格中计数

c# - 在长字符串中查找所有以 [ 开头并以 ] 结尾的字符串部分

c# - 泛型:如何实现: "where T derives from xxx"

wpf - WPF ControlTrigger IsMouseOver