查找DataGrid某个单元格中的控件

/// <summary>
        /// 查找DataGrid某个单元格中的控件
        /// </summary>
        /// <param name="myDataGrid">DataGrid名称</param>
        /// <param name="columnIndex"></param>
        /// <param name="rowIndex"></param>
        /// <param name="controlName">要查找的控件名称</param>
        /// <returns></returns>
        public object FindName(System.Windows.Controls.DataGrid myDataGrid, int columnIndex, int rowIndex, string controlName)
        {
            FrameworkElement item = myDataGrid.Columns[columnIndex].GetCellContent(myDataGrid.Items[rowIndex]);
            DataGridTemplateColumn temp = (myDataGrid.Columns[columnIndex] as DataGridTemplateColumn);
            return temp.CellTemplate.FindName(controlName, item);
        }

 

转载于:https://www.cnblogs.com/happinesshappy/p/5019511.html

在 WPF 的 DataGrid 控件中,要实现相同内容合并单元格,需要自定义一个合并单元格的行为,可以使用 Attached Behavior 或者 Behavior 实现。 以下是使用 Attached Behavior 的实现方法: 1. 创建一个名为 DataGridMergeCellBehavior 的类,并实现一个名为 MergeCells 的附加属性,用于启用合并单元格的行为。代码如下: ```csharp public static class DataGridMergeCellBehavior { public static bool GetMergeCells(DependencyObject obj) { return (bool)obj.GetValue(MergeCellsProperty); } public static void SetMergeCells(DependencyObject obj, bool value) { obj.SetValue(MergeCellsProperty, value); } public static readonly DependencyProperty MergeCellsProperty = DependencyProperty.RegisterAttached("MergeCells", typeof(bool), typeof(DataGridMergeCellBehavior), new PropertyMetadata(false, OnMergeCellsChanged)); private static void OnMergeCellsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { if (!(d is DataGrid dataGrid)) return; if ((bool)e.NewValue) dataGrid.LoadingRow += DataGrid_LoadingRow; else dataGrid.LoadingRow -= DataGrid_LoadingRow; } private static void DataGrid_LoadingRow(object sender, DataGridRowEventArgs e) { var dataGrid = sender as DataGrid; if (dataGrid == null) return; // 获取当前行的数据项 var currentRowItem = e.Row.Item; // 获取当前行的所有单元格 var currentRowCells = e.Row.Descendants<DataGridCell>().ToList(); // 遍历当前行的所有单元格,将与下一行相同的单元格合并 for (int i = 0; i < currentRowCells.Count; i++) { var currentCell = currentRowCells[i]; // 如果当前单元格已经被合并,则跳过 if (currentCell.IsMerged()) continue; var column = currentCell.Column; var binding = (column as DataGridBoundColumn)?.Binding as Binding; // 如果当前列没有绑定数据,则跳过 if (binding == null) continue; var path = binding.Path.Path; var currentCellValue = currentRowItem.GetType().GetProperty(path).GetValue(currentRowItem, null); // 查找下一行与当前行相同的单元格 var nextRow = dataGrid.ItemContainerGenerator.ContainerFromIndex(e.Row.GetIndex() + 1) as DataGridRow; if (nextRow == null) break; var nextRowItem = nextRow.Item; var nextRowCells = nextRow.Descendants<DataGridCell>().ToList(); var nextCell = nextRowCells[i]; var nextCellValue = nextRowItem.GetType().GetProperty(path).GetValue(nextRowItem, null); // 如果下一行与当前行的单元格值相同,则将它们合并 if (Equals(currentCellValue, nextCellValue)) currentCell.Merge(nextCell); } } } ``` 2. 在 DataGrid 控件中应用 DataGridMergeCellBehavior 提供的 MergeCells 附加属性,启用合并单元格的行为。例如,以下代码将启用合并 DataGrid 的第一列单元格的行为: ```xml <DataGrid ItemsSource="{Binding Items}" AutoGenerateColumns="False" behaviors:DataGridMergeCellBehavior.MergeCells="True"> <DataGrid.Columns> <DataGridTextColumn Header="Column1" Binding="{Binding Column1}" /> <DataGridTextColumn Header="Column2" Binding="{Binding Column2}" /> <DataGridTextColumn Header="Column3" Binding="{Binding Column3}" /> </DataGrid.Columns> </DataGrid> ``` 在上面的例子中,DataGridMergeCellBehavior 提供的 MergeCells 附加属性被设置为 True,启用了合并单元格的行为。同时,第一列的单元格将会被合并,如果某个单元格的值与下一行相同,则它们会被合并成一个单元格
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值