WPF-如何将LayoutTransform应用于特定的DataGridColumnHeader [英] WPF- How to apply LayoutTransform to a specific DataGridColumnHeader
问题描述
我有一个 DataGrid
其 ItemsSource
被绑定到一个 System.Data.DataTable
。这个 DataTable
在运行时被填充了一些文本列和一些布尔列。如预期的那样,文本列显示为 DataGridTextColumn
,布尔值显示为 DataGridCheckBoxColumn
。到目前为止没有问题。现在我想要复选框列的列标题显示为垂直。所以我定义了一个这样的样式:
I have a DataGrid
whose ItemsSource
is bound to a System.Data.DataTable
. This DataTable
is filled at runtime with some text columns and some boolean columns. As expected, the text columns are displayed as DataGridTextColumn
and the boolean ones are displayed as DataGridCheckBoxColumn
. No problem so far. Now I want the column headers of the checkbox columns to be displayed as vertical. So I defined a style like this:
<Style x:Key="ColumnHeaderStyle" TargetType="{x:Type DataGridColumnHeader}">
<Style.Triggers>
<!--<Trigger Property=??? Value=???>
<Setter Property="LayoutTransform">
<Setter.Value>
<RotateTransform Angle="270"/>
</Setter.Value>
</Setter>
</Trigger>-->
</Style.Triggers>
</Style>
问题是 - 在代码中注释了,我不知道应该是什么样的触发器用于样式,以便仅应用于 DataGridCheckBoxColumn
而不是其他类型的列。任何想法?
The problem is -as commented in the code, I don't know what kind of trigger I should use for the style in order to be applied only to the DataGridCheckBoxColumn
s and not other types of columns. Any ideas?
推荐答案
要满足两个条件:
- 时间:在ItemSource设置后翻转标题
- 过滤器:样式应仅应用于CheckBoxColumns
您可以诉诸附加行为:
DataGridColumnsBehavior.cs
public static class DataGridColumnsBehavior
{
public static readonly DependencyProperty
FlipHeaderProperty =
DependencyProperty.RegisterAttached("FlipHeader",
typeof(bool), typeof(DataGridColumnsBehavior),
new PropertyMetadata(FlipHeaderChanged));
public static bool GetFlipHeader(DependencyObject obj)
{
return (bool)obj.GetValue(FlipHeaderProperty);
}
public static void SetFlipHeader(DependencyObject obj, bool value)
{
obj.SetValue(FlipHeaderProperty, value);
}
private static void FlipHeaderChanged(DependencyObject d,
DependencyPropertyChangedEventArgs args)
{
var grid = d as DataGrid;
var flip = (bool)grid.GetValue(FlipHeaderProperty);
if (grid == null
|| grid.Columns.Count == 0
|| flip == false) return;
foreach (var column in grid.Columns)
{
if (column.GetType() == typeof(DataGridCheckBoxColumn))
column.HeaderStyle =
(Style)grid.FindResource("CheckBoxColumnHeaderStyle");
}
}
}
XAML
XAML
<DataGrid ItemsSource="{Binding Collection}"
funk:DataGridColumnsBehavior.FlipHeader="{Binding Flip}">
<DataGrid.Resources>
<Style x:Key="CheckBoxColumnHeaderStyle"
TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="LayoutTransform">
<Setter.Value>
<RotateTransform Angle="270"/>
</Setter.Value>
</Setter>
</Style>
</DataGrid.Resources>
</DataGrid>
时间由Flip属性控制。每次更改Collection时,将Flip设置为false,然后再次设置为true。列被迭代,DataGridCheckBoxColumns的标题被翻转。注意实现的方式将Flip设置为false不执行任何操作,因此标题不会被翻转。
The timing is controlled by a Flip property. Every time Collection is changed, set Flip to false and then to true again. The Columns are iterated and the Headers of the DataGridCheckBoxColumns are flipped. Note the way this is implemented setting Flip to false does nothing, so the Headers aren't flipped back.
编辑
只是了解一个较短的方法,使用 AutoGeneratingColumn事件:
Just learned about a shorter method, using the AutoGeneratingColumn event:
<DataGrid ItemsSource="{Binding Collection}"
AutoGeneratingColumn="dataGrid_AutoGeneratingColumn">
<DataGrid.Resources>
<Style x:Key="CheckBoxColumnHeaderStyle"
TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="LayoutTransform">
<Setter.Value>
<RotateTransform Angle="270"/>
</Setter.Value>
</Setter>
</Style>
</DataGrid.Resources>
</DataGrid>
部分类中的处理程序
private void dataGrid_AutoGeneratingColumn(object sender,
DataGridAutoGeneratingColumnEventArgs e)
{
DataGrid grid = sender as DataGrid;
// Only DataGridCheckBoxColumns
if (e.PropertyType == typeof(bool))
e.Column.HeaderStyle =
(Style)grid.FindResource("CheckBoxColumnHeaderStyle");
}
这篇关于WPF-如何将LayoutTransform应用于特定的DataGridColumnHeader的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!