我正在使用WpfToolkit中的DataGrid。我已经为它获取了Style资源字典并稍微调整了一下。我想要完成的是当数据绑定对象上的某个属性为True时使标题变为粗体。列标题不一定是TextBlock,并且其控制模板已重新定义,如下所示:
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>
我认为在模板中使用DataTrigger(绑定到给定属性)会起作用。无论是我错了还是没有走上正确的道路来实施这一点。我还应该提到列是自动生成,生成列后,我将每个DataGridColumn的datacontext设置为:
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);
}
任何帮助都会受到高度赞赏。
干杯, 肖恩
答案 0 :(得分:0)
找到原因:
我应该将DataGridColumn.Header设置为包含DataTrigger中指定的属性的数据对象,以便正常运行。然后,我刚刚覆盖了我的数据类的ToString()方法,以显示我想要的漂亮名称。
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);
}
感谢Chris W.你的评论让我走上了正确的道路! :)