I have a data grid where I need everything to have a fully scalable size. I have no issue with the column headers, they all scale properly. My issue is for the individual cells, they do not seem to respect their binding.
The binding for grid height seems to set the initial value fine, but once the grid is displayed it does not change the height if the variable it is bound to changes.
I had to subclass DataGridTextColumn to add some custom functionality. I have a method called CreateDataGridColumn that returns a reference to ExtendedDataGridTextColumn. These columns are then added to the data grid. The data binding itself works fine, the grid shows all of the correct data.
Here is some code:
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;
}
Then I have the following method fired on the SizeChanged event for the datagrid:
_customDataGrid.TitleAreaHeight = new GridLength(GlobalVariables.DesignerPreviewHeight * (_titleHeightPercentage / 100));
_customDataGrid.SetHeaderFontSize(GlobalVariables.DesignerPreviewHeight * (_headerHeightPercentage / 100));
_fontSize = GlobalVariables.DesignerPreviewHeight * (_gridTextHeightPercentage / 100);
The first two lines do what they are supposed to, change the height of the title area which is something I added to my data grids and change the header height. The update of the _fontSize variable though does not change the data grid cell text height.
Updated
As per suggestion I added a dependency property as such.
public static readonly DependencyProperty GridFontHeightProperty = DependencyProperty.Register("GridFontHeight", typeof(double), typeof(CustomDataGrid));
Then changed my binding code to this.
binding = new Binding();
binding.Path = new PropertyPath("GridFontHeight");
textBlockFNFactory.SetBinding(TextBlock.FontSizeProperty, binding);
Then in my size changed added this.
SetValue(GridFontHeightProperty, _fontSize);
But it does not work. In this scenario it doesn’t set the font height correctly to begin with, it just uses the default font height for the data grid.
First of all, no you can’t bind to a private variable. My guess is, that your variable _fontSize is a
private double, right? (See how i have to guess? ;))You can bind to a public property or to a dependency property, which in your case fits well. So create a new dependency Property called FontSize and bind to that.
If for some reason you can’t use a dependency property, you can still bind to normal CLR properties using INotifyPropertyChanged which should look something like