Reputation: 759
I am using a DataGrid from the WpfToolkit. I have taken the Style resource dictionary for it and tweaked it a bit. What I am trying to accomplish is to make the header bold when a certain property on the data-bound object is True. The Column Header is not necessarily a TextBlock and has its Control Template redefined as seen below:
DataGridStyle:
<Style x:Key="ModificationsDataGridStyle" TargetType="{x:Type compCtrls:ModDataGrid}">
<Setter Property="ColumnHeaderStyle" Value="{StaticResource DatagridColumnHeaderCustomTemplateStyle}" />
</Style>
ColumnHeaderStyle:
<Style x:Key="DatagridColumnHeaderCustomTemplateStyle"
TargetType="{x:Type primitives:DataGridColumnHeader}">
<Setter Property="SnapsToDevicePixels" Value="True" />
<Setter Property="MinWidth" Value="0" />
<Setter Property="MinHeight" Value="28" />
<Setter Property="Foreground" Value="White" />
<Setter Property="FontWeight" Value="Normal" />
<Setter Property="Cursor" Value="Hand" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type primitives:DataGridColumnHeader}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Border x:Name="BackgroundBorder" BorderThickness="0,1,0,1"
Background="{StaticResource DataGridHeaderBackgroundBrush}"
BorderBrush="{StaticResource DataGridHeaderBorderBrush}"
Grid.ColumnSpan="2" />
<ContentPresenter x:Name="ContentPres" Margin="6,3,6,3" VerticalAlignment="Center" />
<Path x:Name="SortArrow" Visibility="Hidden" Data="M0,0 L1,0 0.5,1 z" Stretch="Fill"
Grid.Column="1" Width="10" Height="7" Fill="White" Margin="0,0,7,0"
VerticalAlignment="Center" RenderTransformOrigin="0.5,0.4" />
<Rectangle Width="1" Fill="#AAC377" HorizontalAlignment="Right" Grid.ColumnSpan="2" />
<Rectangle Width="1" Margin="0,0,1,0" Fill="#425B10"
HorizontalAlignment="Right" Grid.ColumnSpan="2" />
<Thumb x:Name="PART_LeftHeaderGripper" HorizontalAlignment="Left"
Style="{StaticResource ColumnHeaderGripperStyle}"/>
<Thumb x:Name="PART_RightHeaderGripper" HorizontalAlignment="Right"
Style="{StaticResource ColumnHeaderGripperStyle}"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="SortDirection" Value="{x:Null}">
<Setter TargetName="SortArrow" Property="Visibility" Value="Collapsed" />
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" TargetName="BackgroundBorder"
Value="{StaticResource DataGridHeaderMouseOverBackgroundBrush}" />
<Setter Property="BorderBrush" TargetName="BackgroundBorder"
Value="{StaticResource DataGridHeaderBorderBrush}" />
</Trigger>
<Trigger Property="SortDirection" Value="Ascending">
<Setter TargetName="SortArrow" Property="Visibility" Value="Visible" />
<Setter TargetName="SortArrow" Property="Fill" Value="Goldenrod" />
<Setter TargetName="SortArrow" Property="RenderTransform">
<Setter.Value>
<RotateTransform Angle="180" />
</Setter.Value>
</Setter>
</Trigger>
<Trigger Property="SortDirection" Value="Descending">
<Setter TargetName="SortArrow" Property="Visibility" Value="Visible" />
<Setter TargetName="SortArrow" Property="Fill" Value="Brown" />
</Trigger>
<Trigger Property="DisplayIndex" Value="0">
<Setter Property="Visibility" Value="Collapsed"
TargetName="PART_LeftHeaderGripper"></Setter>
</Trigger>
<DataTrigger Binding="{Binding IsRevisedSummableField}" Value="True">
<Setter TargetName="ContentPres" Property="Control.FontWeight" Value="Bold" />
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
I thought using a DataTrigger (bound to the given property) in the template would work. Either I am wrong or have not gone the right path in implementing this. I should also mention that the columns are Auto-Generated and once the columns are generated, I set the datacontext of each DataGridColumn as such:
protected override void OnAutoGeneratedColumns(EventArgs e)
{
var dataTable = pivotMod.DataTable;
foreach (DataGridColumn gridCol in Columns)
{
var colName = gridCol.Header.ToString();
DataColumn col = dataTable.Columns[colName];
// give the column headers a pretty name
gridCol.Header = col.Caption;
// set the datacontext of the gridcolumn to the modfield ...
ModFieldGUIWrapper modField = col.ExtendedProperties["ModField"] as ModFieldGUIWrapper;
gridCol.SetValue(FrameworkElement.DataContextProperty, modField);
}
base.OnAutoGeneratedColumns(e);
}
Any help is as always greatly appreciated.
Cheers, Sean
Upvotes: 0
Views: 2464
Reputation: 759
Found the reason why:
I should have set the DataGridColumn.Header to the data object that contains the property specified in the DataTrigger in order to function properly. Then, I just overrided the ToString() method of my data class to display the pretty name that I wanted.
protected override void OnAutoGeneratedColumns(EventArgs e)
{
var dataTable = pivotMod.DataTable;
foreach (DataGridColumn gridCol in Columns)
{
var colName = gridCol.Header.ToString();
DataColumn col = dataTable.Columns[colName];
// set the datacontext of the gridcolumn to the modfield ...
ModFieldGUIWrapper modField = col.ExtendedProperties["ModField"] as ModFieldGUIWrapper;
gridCol.SetValue(FrameworkElement.DataContextProperty, modField);
// set the header to the data object so that the datatrigger's binding works!!
gridCol.Header = modField;
}
base.OnAutoGeneratedColumns(e);
}
Thanks to Chris W. your comment set me on the right path! :)
Upvotes: 0