WPF关于GetVisualChild(dr)

taojialin27 2018-01-12 03:26:36
想根据值改变某个单元格的背景色
但是一直提示
错误 1 非泛型 方法“System.Windows.FrameworkElement.GetVisualChild(int)”不能与类型实参一起使用

还有个小小问题,datag的combobox要怎么改变背景色,没有画笔

...全文
582 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
bluk44444 2018-01-23
  • 打赏
  • 举报
回复
引用 5 楼 taojialin27 的回复:
[quote=引用 3 楼 bluk44444 的回复:]

问题一:
第一步:
获取你需要改变背景的行。                
DataGridRow row = (dg.ItemContainerGenerator.ContainerFromItem(dg.SelectedItem) as DataGridRow);
第二步:
得到单元格
                List<DataGridCell> tbs = GetChildObjects<DataGridCell>(row, "");
第三步:
设置单元格样式
                        dataGrid.CellStyle = this.Resources["dgCellStyle"] as Style;


问题二:
你需要自定义combobox的样式:
在设计视图里,选中combobox,然后右键,编辑模板/编辑其他模板。
根据自动生成的样式代码,调整成你需要的样子。
GetChildObjects是。。? combobox是datagrid里的列,也能套用combobox的模板吗[/quote] private List<T> GetChildObjects<T>(DependencyObject obj, string name) where T : FrameworkElement { DependencyObject child = null; List<T> childList = new List<T>(); for (int i = 0; i <= VisualTreeHelper.GetChildrenCount(obj) - 1; i++) { child = VisualTreeHelper.GetChild(obj, i); if (child is T && (((T)child).Name == name | string.IsNullOrEmpty(name))) { childList.Add((T)child); } childList.AddRange(GetChildObjects<T>(child, name)); } return childList; } combobox是datagrid里的列,也能套用combobox的模板吗 ?可以的。例如: <DataGridTemplateColumn.CellTemplate> <DataTemplate> <CheckBox x:Name="xz" HorizontalAlignment="Center" VerticalAlignment="Center" Checked="CheckBox_Checked" IsChecked="{Binding check}" Unchecked="CheckBox_Unchecked" /> </DataTemplate> </DataGridTemplateColumn.CellTemplate>
bluk44444 2018-01-15
  • 打赏
  • 举报
回复

问题一:
第一步:
获取你需要改变背景的行。                
DataGridRow row = (dg.ItemContainerGenerator.ContainerFromItem(dg.SelectedItem) as DataGridRow);
第二步:
得到单元格
                List<DataGridCell> tbs = GetChildObjects<DataGridCell>(row, "");
第三步:
设置单元格样式
                        dataGrid.CellStyle = this.Resources["dgCellStyle"] as Style;


问题二:
你需要自定义combobox的样式:
在设计视图里,选中combobox,然后右键,编辑模板/编辑其他模板。
根据自动生成的样式代码,调整成你需要的样子。
taojialin27 2018-01-15
  • 打赏
  • 举报
回复
滴滴。。有大腿子吗
taojialin27 2018-01-15
  • 打赏
  • 举报
回复
引用 3 楼 bluk44444 的回复:

问题一:
第一步:
获取你需要改变背景的行。                
DataGridRow row = (dg.ItemContainerGenerator.ContainerFromItem(dg.SelectedItem) as DataGridRow);
第二步:
得到单元格
                List<DataGridCell> tbs = GetChildObjects<DataGridCell>(row, "");
第三步:
设置单元格样式
                        dataGrid.CellStyle = this.Resources["dgCellStyle"] as Style;


问题二:
你需要自定义combobox的样式:
在设计视图里,选中combobox,然后右键,编辑模板/编辑其他模板。
根据自动生成的样式代码,调整成你需要的样子。
GetChildObjects是。。? combobox是datagrid里的列,也能套用combobox的模板吗
exception92 2018-01-15
  • 打赏
  • 举报
回复
GetVisualChild<DataGridCellsPresenter> 这个方法是一个自定义的泛型方法,但是它可能与 System.Windows.FrameworkElement.GetVisualChild(int) 这个方法同名了,导致编译器认为使用的是FrameworkElement.GetVisualChild(int) 这个方法。 调用的时候把名称改为完全限定的方法名称。
taojialin27 2018-01-12
  • 打赏
  • 举报
回复
                        DataGridRow dr = (DataGridRow)datag.ItemContainerGenerator.ContainerFromIndex(i);
                        DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(dr);

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧