[英]WPF Datagrid: How to change border thickness of a single cell programatically?
我是WPF的新手
我试图根据列和行索引更改单个单元格的边框。 到目前为止,我已经有了获取列和行索引的代码。
现在,我需要获取“那个单元格”并更改其边界。
这是我的代码,但是不起作用:
我从网上得到了这种方法:
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++)
{
Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
child = v as T;
if (child == null)
child = GetVisualChild<T>(v);
else
break;
} return child;
}
然后在这里我需要更改单元格属性。
int rows = 0;
int col = 0;
while (col < myDG.Columns.Count)
{
rows = 0;
while (rows < myDG.Items.Count)
{
DataGridRow row = (DataGridRow)myDG.ItemContainerGenerator.ContainerFromIndex(rows);
if (row != null)
{
DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(row);
DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(col);
cell.BorderThickness = new Thickness (2,2,2,2);
cell.BorderBrush= Brushes.Black;
rows++;
}
col++;
}
任何想法? 提前致谢
尝试避免WPF应用程序中的代码落后。 最好在您的数据上下文上将厚度属性设置为绑定属性。
那么您可以通过设置valueconverter将此值转换为该属性的有效值。
这是一个非常基本的示例: http : //wpftutorial.net/DataBindingOverview.html
因此,如果您不熟悉WPF,请尝试搜索有关MVVM的一些视频。
Laurent Bignon提出了一些出色的解释整个概念的方法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.