【发布时间】:2011-10-05 12:57:43
【问题描述】:
我有一个数据网格,我需要其中的所有内容都具有完全可扩展的大小。我对列标题没有任何问题,它们都可以正确缩放。我的问题是单个细胞,它们似乎不尊重它们的结合。
网格高度的绑定似乎可以很好地设置初始值,但是一旦显示网格,如果它绑定的变量发生变化,它不会改变高度。
我必须继承 DataGridTextColumn 来添加一些自定义功能。我有一个名为 CreateDataGridColumn 的方法,它返回对 ExtendedDataGridTextColumn 的引用。然后将这些列添加到数据网格中。数据绑定本身工作正常,网格显示所有正确的数据。
这是一些代码:
private ExtendedDataGridTextColumn CreateDataGridColumn(EntityBase dataColumn, FormatConditionGroup formatConditionGroup)
{
ExtendedDataGridTextColumn newColumn = new ExtendedDataGridTextColumn(dataColumn);
DataTemplate dataTemplate = new DataTemplate();
String textBlockName = "Text" + dataColumn.EntityId;
String columnTag = dataColumn.GetPropertyValue("Tag");
// Create the TextBlock that will display the cell contents
FrameworkElementFactory textBlockFNFactory;
textBlockFNFactory = new FrameworkElementFactory(typeof(TextBlock));
_gridTextHeightPercentage = dataColumn.GetPropertyDouble("GridFontSize", Constants.DefaultFontHeightPercent) / 2.8;
_fontSize = GlobalVariables.DesignerPreviewHeight * (_gridTextHeightPercentage / 100);
Binding binding = new Binding();
binding.Source = _fontSize;
textBlockFNFactory.SetBinding(TextBlock.FontSizeProperty, binding);
// Do a whole bunch of stuff here
// Create a border so that the label background does not obscure the grid lines
FrameworkElementFactory borderFNFactory;
borderFNFactory = new FrameworkElementFactory(typeof(Border));
borderFNFactory.AppendChild(textBlockFNFactory);
// Add type to data template
dataTemplate.VisualTree = borderFNFactory;
newColumn.CellTemplate = dataTemplate;
return newColumn;
}
然后我在数据网格的 SizeChanged 事件上触发了以下方法:
_customDataGrid.TitleAreaHeight = new GridLength(GlobalVariables.DesignerPreviewHeight * (_titleHeightPercentage / 100));
_customDataGrid.SetHeaderFontSize(GlobalVariables.DesignerPreviewHeight * (_headerHeightPercentage / 100));
_fontSize = GlobalVariables.DesignerPreviewHeight * (_gridTextHeightPercentage / 100);
前两行做了他们应该做的,改变标题区域的高度,这是我添加到我的数据网格的东西,并改变了标题高度。 _fontSize 变量的更新虽然不会改变数据网格单元格的文本高度。
更新
根据建议,我添加了一个依赖属性。
public static readonly DependencyProperty GridFontHeightProperty = DependencyProperty.Register("GridFontHeight", typeof(double), typeof(CustomDataGrid));
然后把我的绑定代码改成这个。
binding = new Binding();
binding.Path = new PropertyPath("GridFontHeight");
textBlockFNFactory.SetBinding(TextBlock.FontSizeProperty, binding);
然后在我的尺寸改变了添加这个。
SetValue(GridFontHeightProperty, _fontSize);
但它不起作用。在这种情况下,它没有正确设置字体高度,它只是使用数据网格的默认字体高度。
【问题讨论】:
-
我以为我在展示绑定。我创建一个绑定对象,设置源,然后以编程方式调用 SetBinding。