<Style x:Name="ColumnStyle" x:Key="ColumnHeaderStyle" TargetType="my:DataGridColumnHeader">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<StackPanel Orientation="Vertical" HorizontalAlignment="Stretch" Background="LightYellow">
<TextBlock Text="{Binding Name}" HorizontalAlignment="Stretch" TextAlignment="Left" Background="LightGreen" />
<TextBlock Text="{Binding Data}" HorizontalAlignment="Stretch" TextAlignment="Right" Background="LightBlue" />
</StackPanel>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
头部的DataContext在代码中设置,使用DataGrid的初始宽度正确显示了Name和Data字符串属性:
---------------
|Name |
| Data|
---------------
然而,当我调整列的大小时,标题显示没有重新排版,而是保持不变:
--------------------
|Name |
| Data |
--------------------
我原本期望它看起来是这样的:
--------------------
|Name |
| Data|
--------------------
我需要做些什么才能获得上述所需的行为?
同样,标题内容似乎也无法在垂直方向上拉伸。
更新:添加
<Setter Property="VerticalAlignment">
<Setter.Value>Bottom</Setter.Value>
</Setter>
在样式中设置HorizontalAlignment属性为'Stretch'似乎可以将标题正确对齐到底部。不幸的是,这并没有达到我想要的效果。
复现详细信息: 以下是演示该行为的代码片段。
Window1.xaml:
<Window x:Class="GridTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300" xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit">
<Window.Resources>
<Style x:Name="ColumnStyle" x:Key="ColumnHeaderStyle" TargetType="my:DataGridColumnHeader">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<DockPanel>
<TextBlock DockPanel.Dock="Left" Text="{Binding Name}" />
<TextBlock DockPanel.Dock="Right" Text="{Binding Data}" />
</DockPanel>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Name="RowHeaderStyle" x:Key="RowHeaderStyle" TargetType="my:DataGridRowHeader">
<Setter Property="Content" Value="{Binding}" />
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Content.Name, RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type my:DataGridRowHeader}}}"
VerticalAlignment="Center"/>
<TextBlock Padding="5">|</TextBlock>
<TextBlock Text="{Binding Path=Content.Data, RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type my:DataGridRowHeader}}}"
VerticalAlignment="Center"/>
</StackPanel>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<my:DataGrid Name="dg"
ColumnHeaderStyle="{StaticResource ColumnHeaderStyle}"
RowHeaderStyle="{StaticResource RowHeaderStyle}"
HeadersVisibility="All">
</my:DataGrid>
</Grid>
</Window>
以及 Window1.xaml.cs 中的代码后台
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Data;
using Microsoft.Windows.Controls;
using SLModel;
namespace GridTest
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(Window1_Loaded);
}
void Window1_Loaded(object sender, RoutedEventArgs e)
{
Inputs = new List<Input>();
Outputs = new List<Output>();
Input i1 = new Input() { Name = "I 1", Data = "data 1" };
Input i2 = new Input() { Name = "I 2", Data = "data 2" };
Input i3 = new Input() { Name = "I 3", Data = "data 3" };
Inputs.Add(i1); Inputs.Add(i2);
Output o1 = new Output() { Name = "O 1", Data = "data 1" };
Output o2 = new Output() { Name = "O 2", Data = "data 2" };
Output o3 = new Output() { Name = "O 3", Data = "data 3" };
Outputs.Add(o1); Outputs.Add(o2); Outputs.Add(o3);
Relationship r1 = new Relationship() { Formula = "F1" };
Relationship r2 = new Relationship() { Formula = "F2" };
Relationship r3 = new Relationship() { Formula = "F3" };
Relationship r4 = new Relationship() { Formula = "F4" };
Relationship r5 = new Relationship() { Formula = "F5" };
Relationship r6 = new Relationship() { Formula = "F6" };
i1.Relationships.Add(r1);
i1.Relationships.Add(r2);
i2.Relationships.Add(r3);
i2.Relationships.Add(r4);
i3.Relationships.Add(r5);
i3.Relationships.Add(r6);
CreateColumn(o1, 0);
CreateColumn(o2, 1);
CreateColumn(o3, 2);
dg.Items.Add(i1);
dg.Items.Add(i2);
dg.Items.Add(i3);
dg.ColumnWidth = DataGridLength.SizeToHeader;
}
private void CreateColumn(Output output, int index)
{
Binding textBinding = new Binding();
textBinding.Path = new PropertyPath(string.Format("Relationships[{0}].Formula", index));
textBinding.Mode = BindingMode.TwoWay;
DataGridTextColumn tc = new DataGridTextColumn();
tc.Binding = textBinding;
dg.Columns.Add(tc);
tc.Header = output;
}
private List<Output> Outputs { get; set; }
private List<Input> Inputs { get; set; }
}
}
使用简单的类Input、Output和Relationship,如下所示:
public class Input { public Input() { Relationships = new ObservableCollection(); }
public string Name { get; set; }
public string Data { get; set; }
public ObservableCollection<Relationship> Relationships { get; set; }
公共类 Output { 公共 Output() { } }
public string Name { get; set; }
public string Data { get; set; }
public class Relationship { public Relationship() { } public string Formula { get; set; } }
复现步骤:
打开应用程序
观察列标题“O 1data 1”,“O 2data 2”和“O 3data 3”
通过拖动列分隔符将第一列变宽
观察“Name” TextBlock(在本例中为“O 1”)与“Data” TextBlock(“data 1”)之间的距离是否发生变化,即“Data” TextBlock 是否已“停靠”到列标题的右侧边缘。
ControlTemplate
。请查看我的答案更新。 - devuxer