Reputation: 7360
There is "a lot of" space above and below the text lines in a Silverlight DataGrid
.
The default DataGridCell
instance that is generated by DataGridTextColumn
renders a TextBlock
with a Margin of 4 (worked out using Silverlight Spy).
I tried to create a custom DataGridCell
template and set the Margin and Padding values to zero there, but neither this nor setting a ContentTemplate
changed anything.
Do you have any idea how I can reduce the height of certain DataGridCell
's to a value next to 0?
Thanks in advance!
Upvotes: 1
Views: 318
Reputation: 7360
I just found the answer by myself:
The problem is the part of the DataGridTextColumn
class where the TextBlock that is placed inside each cell generated:
protected override FrameworkElement GenerateElement(DataGridCell cell, object dataItem)
{
TextBlock block = new TextBlock {
Margin = new Thickness(4.0),
VerticalAlignment = VerticalAlignment.Center
};
if (DependencyProperty.UnsetValue != base.ReadLocalValue(FontFamilyProperty))
{
block.FontFamily = this.FontFamily;
}
if (this._fontSize.HasValue)
{
block.FontSize = this._fontSize.Value;
}
if (this._fontStyle.HasValue)
{
block.FontStyle = this._fontStyle.Value;
}
if (this._fontWeight.HasValue)
{
block.FontWeight = this._fontWeight.Value;
}
if (this._foreground != null)
{
block.Foreground = this._foreground;
}
if ((this.Binding != null) || !DesignerProperties.IsInDesignTool)
{
block.SetBinding(TextBlock.TextProperty, this.Binding);
}
return block;
}
As you can see the Margin is statically set to 4.0. To get around this I created a wrapper class that derived from DataGridTextColumn
:
public class DataGridCustomTextColumn : DataGridTextColumn
{
protected override FrameworkElement GenerateElement(DataGridCell cell, object dataItem)
{
//Get the parent TextBlock
TextBlock block = (TextBlock)base.GenerateElement(cell, dataItem);
if (ElementStyle != null) //if an element style is given
{
//Apply each setter of the style to the generated block
ElementStyle.Setters.OfType<System.Windows.Setter>()
.ForEach((setter) => block.SetValue(setter.Property, setter.Value));
}
//Return styled block
return (FrameworkElement)objBlock;
}
}
Upvotes: 1