gpt4 book ai didi

WPF 工具包数据网格 : how to get ColumnHeader width to be the same as GridColumn width

转载 作者:行者123 更新时间:2023-12-04 21:48:50 35 4
gpt4 key购买 nike

对于我的 WPF Toolkit DataGrid,我使用以下自定义列标题样式:

<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>

header 的 DataContext 在代码中设置,并且使用 DataGrid 的初始宽度正确显示 Name 和 Data 字符串属性,如下所示:
---------------
|Name |
| Data|
---------------

但是,当我调整列大小时,标题显示不会重排,而是保持不变:
--------------------
|Name |
| Data |
--------------------

正如我所期望的那样:
--------------------
|Name |
| Data|
--------------------

我需要做什么才能获得上述所需的行为?

同样,标题内容似乎也没有在垂直方向上拉伸(stretch)。

更新:
添加
    <Setter Property="VerticalAlignment">
<Setter.Value>Bottom</Setter.Value>
</Setter>

样式似乎正确地将标题与底部对齐。不幸的是,以时尚的方式将 Horizo​​ntalAlignment 属性设置为“Stretch”似乎并没有达到我想要的效果。

Repro 的详细信息:
下面是演示该行为的代码片段。

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; }
}
}

使用简单的输入、输出和关系类:

公共(public)类输入
{
公共(public)输入()
{
关系 = 新的 ObservableCollection();
}
public string Name { get; set; }
public string Data { get; set; }

public ObservableCollection<Relationship> Relationships { get; set; }

}

公共(public)类输出
{
公共(public)输出(){}
public string Name { get; set; }
public string Data { get; set; }

}

公共(public)类关系
{
公共(public)关系() { }
公共(public)字符串公式 { 获取;放; }
}

复制步骤:
  • 启动应用程序
  • 观察列标题“O 1data 1”、“O 2data 2”和“O 3data 3”
  • 通过向右拖动列分隔符使第一列变宽
  • 观察“名称”文本 block (在本例中为“O 1”)和“数据”文本 block (“数据 1”)之间的距离没有变化,即“数据”文本 block 没有“停靠”到右侧列标题的边缘。
  • 最佳答案

    我建议更换您的 StackPanelGrid :

    <Style x:Name="ColumnStyle" x:Key="ColumnHeaderStyle" TargetType="my:DataGridColumnHeader">
    <Setter Property="ContentTemplate">
    <Setter.Value>
    <DataTemplate>
    <Grid>
    <Grid.RowDefinitions>
    <RowDefinition Height="Auto" />
    <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
    <ColumnDefinition Width="Auto" />
    <ColumnDefinition Width="*" />
    <ColumnDefinition Width="Auto" />
    </Grid.ColumnDefinitions>
    <TextBlock Grid.Row="0" Grid.Column="0" Text="{Binding Name}" Background="LightGreen" />
    <TextBlock Grid.Row="1" Grid.Column="2" Text="{Binding Data}" Background="LightBlue" />
    </Grid>
    </DataTemplate>
    </Setter.Value>
    </Setter>
    </Style>

    如果这不起作用,您可能需要编辑 ControlTemplate对于 DataGridColumnHeader .我不确定默认模板是什么样的,但如果 ContentPresenter不位于可拉伸(stretch)容器内(如 Grid ),是否拉伸(stretch) ContentPresenter 内的内容无关紧要,它不会拉伸(stretch)。但有一件事我很确定, StackPanels即使你告诉他们也不要拉伸(stretch),所以一定要试试 Grid在您的 DataTemplate第一的。

    更新(固定)

    好的,我挖出了默认的 ControlTemplate对于 DataGridColumnHeader .事实证明确实使用了 Grid ,所以我认为这不是问题。

    关键可能是 ContentPresenter :
    <ContentPresenter
    SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
    VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
    HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" />

    您可能只需要设置 HorizontalContentAlignment="Stretch"在您的 DataGridColumnHeader 上风格。

    关于WPF 工具包数据网格 : how to get ColumnHeader width to be the same as GridColumn width,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2143037/

    35 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com