WPF dataGrid查找单个单元格的X和Y并设置背景颜色

2024-03-25

我有一个绑定到数据网格(MVVM)的类型化数据集。我还有一个点列表(输入数据集中的 X 和 Y),表明哪些单元格有错误。检测这一点的逻辑很复杂并且在服务器端运行。

我的目标是如果每个单元格有错误,则将其背景绘制为不同的颜色(即点列表包含该单元格的 X 和 Y)。

数据网格定义为:

 <DataGrid x:Name="gridData" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="3" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" 
              BorderThickness="0 0 0 0" Margin="0 0 0 0" AutoGenerateColumns="True" AlternationCount="1" AlternatingRowBackground="AliceBlue"
              ItemsSource="{Binding Path=EquisDataTable}" SelectionUnit="Cell" SelectedCellsChanged="gridData_SelectedCellsChanged"
              AutoGeneratedColumns="GridData_OnAutoGeneratedColumns" AutoGeneratingColumn="gridData_AutoGeneratingColumn" Height="350">
                        <DataGrid.CellStyle>
            <Style TargetType="{x:Type DataGridCell}">
                <Style.Setters>
                    <Setter Property="Background">
                        <Setter.Value>
                            <MultiBinding Converter="{StaticResource onErrorConverter}">
                                <Binding RelativeSource="{RelativeSource Self}" />
                                <Binding RelativeSource="{RelativeSource AncestorType=SampleTests:SampleTestUserControlBase}" Path="DataContext.Problems" />
                                <Binding RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}" />
                            </MultiBinding>
                        </Setter.Value>
                    </Setter> 
                </Style.Setters>
            </Style>
        </DataGrid.CellStyle>
    </DataGrid>

我相信我现在已经接近它了(经过大量测试和谷歌搜索才到达这里)。当填充所有内容后更改单元格选择时,我可以找到单元格的 X 和 Y,“SelectedCellsChanged”方法。这并不能让我到达每个单元格并在加载时检查其值。

    private void gridData_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
    {
        int x;
        int y;
        if (TryGetDataGridCellXandY(gridData.CurrentCell, gridData, out x, out y))               
        { }
    }      

    private bool TryGetDataGridCellXandY(DataGridCellInfo dgc, DataGrid dataGrid, out int x, out int y)
    {
        int columnIndex = dgc.Column.DisplayIndex;
        return TryGetDataGridCellXandY(columnIndex, dataGrid, out x, out y);
    }

    private bool TryGetDataGridCellXandY(int columnIndex, DataGrid dataGrid, out int x, out int y)
    {
        DataGridCellInfo currentCell = dataGrid.CurrentCell;

        int rowIndex = int.MinValue;
        DataRowView rowView = currentCell.Item as DataRowView;
        if (rowView != null)
        {
            DataRow dataRow = rowView.Row;
            FieldInfo fi = typeof(DataRow).GetField("_rowID", BindingFlags.NonPublic | BindingFlags.Instance);
            try
            {
                if (fi != null)
                {
                    rowIndex = System.Convert.ToInt32(fi.GetValue(dataRow));
                }
            }
            catch (InvalidCastException) { }

        }

        x = columnIndex;
        y = rowIndex;

        return x > 0 && y > 0;
    }

所以我创建了一个多值转换器来完成同样的事情。使用转换器时,当单元格的列为空时,就会出现问题,并且 currentCell.Item (DataGridCellInfo).Item 将具有 {DependencyProperty.UnsetValue}。

public class DataGridCellOnErrorConversion : IMultiValueConverter
{
    private const string DefaultColour = "White";
    private const string ErrorColour = "Red";
 public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        if (values.Length != 3)
            return DefaultColour;

        DataGridCell dgc = values[0] as DataGridCell;
        if(dgc == null)
            return DefaultColour;

        IList<Point> problems = values[1] as IList<Point>;
        if(problems == null)
            return DefaultColour;

        DataGrid grid = values[2] as DataGrid;
        if (grid == null)
            return DefaultColour;


        int x;
        int y;
        if (TryGetDataGridCellXandY(grid.CurrentCell, grid, out x, out y))
        {
            if (problems.Any(problem => System.Convert.ToInt32(problem.X) == x && System.Convert.ToInt32(problem.Y) == y))
            {
                return ErrorColour;
            }
        }

        return DefaultColour;
  }
 private bool TryGetDataGridCellXandY(DataGridCellInfo dgc, DataGrid dataGrid, out int x, out int y)
    {
        int columnIndex = dgc.Column.DisplayIndex;
        return TryGetDataGridCellXandY(columnIndex, dataGrid, out x, out y);
    }

    private bool TryGetDataGridCellXandY(int columnIndex, DataGrid dataGrid, out int x, out int y)
    {
        DataGridCellInfo currentCell = dataGrid.CurrentCell;

        int rowIndex = int.MinValue;
        DataRowView rowView = currentCell.Item as DataRowView;
        if (rowView != null)
        {
            DataRow dataRow = rowView.Row;
            FieldInfo fi = typeof(DataRow).GetField("_rowID", BindingFlags.NonPublic | BindingFlags.Instance);
            try
            {
                if (fi != null)
                {
                    rowIndex = System.Convert.ToInt32(fi.GetValue(dataRow));
                }
            }
            catch (InvalidCastException) { }

        }

        x = columnIndex;
        y = rowIndex;

        return x > 0 && y > 0;
    }  
}

这是因为绑定/创建顺序吗?有没有解决的办法?

Thanks


这就是我解决问题的方法,(不确定它是否是最佳的,但似乎有效,(到目前为止):

我在数据网格单元格的样式上有一个多值转换器:

      <Style TargetType="{x:Type DataGridCell}">
           <Style.Setters>
               <Setter Property="Background">
                   <Setter.Value>
                       <MultiBinding Converter="{StaticResource onErrorConverter}">
                           <Binding RelativeSource="{RelativeSource Self}" />
                           <Binding RelativeSource="{RelativeSource AncestorType=SampleTests:SampleTestUserControlBase}" Path="DataContext.Problems" />
                           <Binding RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}" />
                       </MultiBinding>
                   </Setter.Value>
               </Setter> 
           </Style.Setters>
      </Style>

和转换器:

public class DataGridCellOnErrorConversion : IMultiValueConverter
{
    private readonly SolidColorBrush DefaultColour = new SolidColorBrush(Colors.White);
    private readonly SolidColorBrush ErrorColour = new SolidColorBrush(Colors.Red);
    private readonly SolidColorBrush AlternatingColour = new SolidColorBrush(Colors.AliceBlue);


    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        if (values.Length < 3)
            return DefaultColour;

        DataGridCell dgc = values[0] as DataGridCell;
        if(dgc == null)
            return DefaultColour;

        IList<Point> problems = values[1] as IList<Point>;
        if(problems == null)
            return DefaultColour;

        DataGrid grid = values[2] as DataGrid;
        if (grid == null)
            return DefaultColour;
        int x;
        int y = -1;

        ItemCollection itemCollection = grid.Items;

        for (int i = 0; i < itemCollection.Count; i++)
        {
            if (itemCollection.CurrentItem == itemCollection[i])
                y = i;
        }

        x = dgc.Column.DisplayIndex;

        DataRowView currentRowView = null;
        FieldInfo fi = dgc.GetType().GetField("_owner", BindingFlags.NonPublic | BindingFlags.Instance);
        try
        {
            if (fi != null)
            {
                DataGridRow dataGridRow = fi.GetValue(dgc) as DataGridRow;
                if(dataGridRow != null)
                    currentRowView = dataGridRow.Item as DataRowView;
            }
        }
        catch (InvalidCastException) { }

        if(currentRowView != null)
        {
            for (int i = 0; i < itemCollection.Count; i++)
            {
                if (currentRowView == itemCollection[i])
                    y = i;
            }
        }

        if (problems.Any(problem => System.Convert.ToInt32(problem.X) == x && System.Convert.ToInt32(problem.Y) == y))
        {
            return ErrorColour;
        }

        return y % 2 == 0 ? AlternatingColour : DefaultColour;
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

WPF dataGrid查找单个单元格的X和Y并设置背景颜色 的相关文章

  • 创建面向鼠标和触摸的 WPF 应用程序的最佳实践[关闭]

    就目前情况而言 这个问题不太适合我们的问答形式 我们希望答案得到事实 参考资料或专业知识的支持 但这个问题可能会引发辩论 争论 民意调查或扩展讨论 如果您觉得这个问题可以改进并可能重新开放 访问帮助中心 help reopen questi
  • C# - 继承WPF布局 - Window from Window

    我的 Window 继承有问题 我不明白问题是什么 我认为 我的布局 MediaLibrary xaml 必须继承 MainWindow 但我不知道该怎么做 有2类 主窗口 xaml
  • 将集合绑定到自定义控件属性

    我没有运气尝试将数据集合绑定到我的自定义控件的属性 我已经实现了该控件的字符串属性的机制 在此处提供了一些帮助 并期望集合类型同样简单 但是我无法让它再次工作 这是我的自定义控件视图
  • 如何在标准 WPF ListView 中启用 UI 虚拟化

    我正在使用 NET 4 5 VS2012 并且我有一个 ListView 看起来像这样
  • 我的 WPF 应用程序中的 SaveFileDialog 异常

    我的一位客户在保存文件时遇到 WPF 应用程序崩溃的情况 我的保存文件代码是 var saveFileDialog new SaveFileDialog InitialDirectory string Concat Environment
  • 在Linux上编译C# + WPF以便在Windows上运行

    我有一个 C 应用程序 其中某些部分是使用 WPF 编写的 Mono 不支持 可以在 Linux 上编译这个应用程序吗 最终 该应用程序将在 Windows 上运行 但它是更大框架的一部分 并且我们的整个构建过程在 Linux 上运行 因此
  • 关闭主窗口时 WPF 应用程序不会关闭

    我习惯了在 Visual Studio 中进行 WinForms 编程 但我想尝试一下 WPF 我向我的项目添加了另一个窗口 名为 Window01 主窗口称为MainWindow 之前public MainWindow 构造函数我声明Wi
  • wpf 中带有复选框通用控件的多选组合框

    我想创建控件 允许用户使用复选框从下拉列表中选择多个选项 我在 Google 上进行了搜索 得到了一些链接 例如 http code msdn microsoft com windowsapps Multi Select ComboBox
  • 如果在代码中添加元素,“FindName”将不起作用

    在 WPF 应用程序中 如果在 XAML 中声明 ContentControl
  • WPF DataGrid - 在每行末尾添加按钮

    我想在数据网格的每一行的末尾添加一个按钮 我找到了以下 xaml 但它将按钮添加到开头 有人知道如何在所有数据绑定列之后添加它吗 这会将按钮添加到开头而不是末尾
  • 如何将 WPF 窗口置于前面?

    我创建了一个单实例应用程序 并且希望在用户多次启动该应用程序时激活已打开的窗口 这工作正常 但我有一个问题 如果已经打开的窗口超出了另一个应用程序窗口 我必须将其放在前面 我尝试过 window Focus 和 window Show 但它
  • 通过等待任务或访问其 Exception 属性都没有观察到任务的异常

    这些是我的任务 我应该如何修改它们以防止出现此错误 我检查了其他类似的线程 但我正在使用等待并继续 那么这个错误是怎么发生的呢 通过等待任务或访问其 Exception 属性都没有观察到任务的异常 结果 未观察到的异常被终结器线程重新抛出
  • 跨项目/dll 访问 Xaml 中的资源

    是否可以从另一个项目引用存储在 ResourceDictionary 构建操作 资源 中的 Xaml 资源 我想将资产合并到主项目的资源字典中或单独访问它们 例如 项目 MyResources 包含一个名为 Assets 的文件夹 其中有一
  • ViewModel 中的 TextBox CaretIndex 属性

    是否可以通过视图中定义的 Binding 获取 设置 wpf 视图模型中 TextBox 控件的 CaretIndex 属性的值 Thanks 这里的问题是如何获得CaretIndex of the TextBox通过视图模型进行控制 如果
  • 如何在MVVM中管理多个窗口

    我知道有几个与此类似的问题 但我还没有找到明确的答案 我正在尝试深入研究 MVVM 并尽可能保持纯粹 但不确定如何在坚持模式的同时启动 关闭窗口 我最初的想法是向 ViewModel 发送数据绑定命令 触发代码来启动一个新视图 然后通过 X
  • 从固定文档中删除页面?

    如何从固定文档中删除页面 我添加这样的页面 Add page to pageContent PageContent pageContent new PageContent IAddChild pageContent AddChild fix
  • 异步WCF调用来保存线程?

    In 另一个问题 https stackoverflow com q 19731600 279516 建议我发送异步网络请求 而不是在后台线程上发送同步请求 原因是为了不浪费一根线 我试图理解这是怎么回事 这是最初的做法 我可以理解这里怎么
  • 如何将文本放在 RadioButton 的顶部

    我正在尝试实现附件中显示的效果 但没有成功 甚至有可能吗 我试图将文本框放在单选按钮内 并将其设置为水平和垂直内容对齐 但它没有按我想要的方式工作 欢迎任何建议 Resource
  • 如何从 XAML 设置 WPF 用户控件属性?

    我试图从 XAML 设置同一用户控件的多个实例的 fill 属性 以便区分它们 我在控件的 C 代码隐藏中使用依赖属性 并在实例化控件时在 XAML 中引用该属性 这是我尝试过的简化示例 首先是用户控件的 XAML
  • 访问 XAML 中的静态字段

    如何在 xaml 中引用类的静态属性 换句话说 我想做这样的事情 Class BaseThingy public static readonly Style BaseStyle

随机推荐