C# WPF 取DataGrid当前单元格(CurrentCell)的数据

函数:

        public object GetCellValue(DataGridCellInfo cellInfo) 

        {
            if (cellInfo != null)
            {
                var column = cellInfo.Column as DataGridBoundColumn;
                if (column != null)
                {
                    var element = new FrameworkElement() { DataContext = cellInfo.Item };
                    BindingOperations.SetBinding(element, FrameworkElement.TagProperty, column.Binding);

                    var cellValue = element.Tag;

                    if(cellValue!=null)

                            return (cellValue);

                }
            }
            return (null);

        }

调用示例:

var cellValue = GetCellValue(dataGrid.CurrentCell);

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: WPF DataGrid是一种功能强大的控件,可以用于展示和编辑大量数据。在某些情况下,我们可能需要合并单元来提高表的可读性和美观性。 要实现WPF DataGrid单元合并,我们可以使用DataGrid的合并行(MergeRow)和合并列(MergeColumn)属性。这些属性可用于设置数据单元的合并方式。 对于合并行(MergeRow),我们需要在DataGrid中的各个单元中设置合并行的属性值。我们可以使用Grid标签来定义单元,然后设置合并行的StartingColumn和EndingColumn属性的值。这将决定哪些列将被合并为同一行。 对于合并列(MergeColumn),同样需要在DataGrid中的单元中设置合并列的属性值。我们可以使用DataGridTextColumn或者其他列类型,并设置它们的合并列属性的值。合并列的StartingRow和EndingRow属性将决定哪些行将被合并为同一列。 需要注意的是,DataGrid的合并行和合并列属性只能在静态绑定(StaticResource)的情况下工作。这意味着我们需要在XAML文件中预先定义合并行和合并列的样式。然后,在DataGrid的列或单元中引用这些样式。 总的来说,通过使用DataGrid的合并行和合并列属性,我们可以轻松地实现WPF DataGrid单元合并。这样可以大幅提升表的可读性和美观性,使数据展示更为清晰明了。 ### 回答2: WPFDataGrid控件本身并不直接支持合并单元的功能。但是我们可以通过编写自定义的单元合并逻辑来实现这个功能。 首先,我们可以使用DataGrid中的Loaded事件来获DataGridDataGridCellsPanel对象。DataGridCellsPanel是一个容纳所有单元的面板。然后,我们可以遍历DataGrid中的每个单元,并比较相邻单元的值来判断是否需要合并。如果需要合并,我们可以设置相邻单元的Visibility属性为Collapsed来隐藏掉它们。 以下是一个简单的实现示例: ```csharp private void DataGrid_Loaded(object sender, RoutedEventArgs e) { DataGrid dataGrid = (DataGrid)sender; DataGridCellsPanel cellsPanel = GetVisualChild<DataGridCellsPanel>(dataGrid); for (int i = 0; i < dataGrid.Items.Count; i++) { DataGridRow row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(i); for (int j = 0; j < dataGrid.Columns.Count - 1; j++) { DataGridCell currentCell = GetCell(cellsPanel, row, j); DataGridCell nextCell = GetCell(cellsPanel, row, j + 1); if (currentCell.Content.ToString() == nextCell.Content.ToString()) { nextCell.Visibility = Visibility.Collapsed; currentCell.ColumnSpan++; } } } } private T GetVisualChild<T>(Visual parent) where T : Visual { T child = default(T); for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++) { Visual visual = (Visual)VisualTreeHelper.GetChild(parent, i); child = visual as T; if (child == null) { child = GetVisualChild<T>(visual); } if (child != null) { break; } } return child; } private DataGridCell GetCell(DataGridCellsPanel cellsPanel, DataGridRow row, int column) { return (DataGridCell)cellsPanel.Children.Cast<UIElement>().FirstOrDefault(e => Grid.GetColumn(e) == column && Grid.GetRow(e) == row.GetIndex()); } ``` 这只是一个简单的示例,实际应用中可能需要根据具体需求进行适当的修改和扩展。希望以上的回答能对你有所帮助。 ### 回答3: WPF中的DataGrid是一个强大的控件,可以用来展示和编辑数据。然而,DataGrid并没有直接支持单元合并的功能。如果我们想要实现合并单元的效果,我们需要自己来处理。 最常用的一种方法是通过自定义DataGrid的样式和模板来实现单元的合并。我们可以重写DataGrid的模板,并在模板中添加一些自定义的元素来模拟单元的合并。 首先,我们需要定义一个自定义的单元样式。我们可以使用一个Grid来模拟单元的合并效果,将需要合并的单元的内容放在一个Grid中,然后通过设置Grid的行和列的合并方式来实现合并效果。 在DataGrid中,我们可以通过CellTemplate属性来设置单元的样式。我们可以定义一个DataTemplate,在这个模板中包含我们自定义的合并单元的样式。 接着,我们需要通过继承DataGrid来自定义一个新的控件,例如MergeDataGrid。在新的控件中,我们可以重写OnApplyTemplate方法,在此方法中获DataGrid的模板,并对模板中的单元样式进行修改和设置。 最后,我们可以使用MergeDataGrid来替代DataGrid的使用,从而实现合并单元的效果。我们可以通过设置需要合并的单元的行和列的合并方式来实现我们想要的合并效果。 需要注意的是,自定义DataGrid样式和模板是一项较复杂的任务,需要一定的WPF编程经验。此外,单元合并可能会影响到DataGrid的排序、过滤等功能,需要根据实际需求进行适当的调整和修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值