WPF DataGrid 窗口调整大小不会调整 DataGridColumns 的大小

发布于 2024-08-30 13:29:21 字数 1502 浏览 2 评论 0原文

我的应用程序中有一个 WPF DataGrid(来自 WPFToolkit 包),如下所示。

<Controls:DataGrid>
            <Controls:DataGrid.Columns>
                <Controls:DataGridTextColumn Width="1*" Binding="{Binding Path=Column1}" Header="Column 1" />
                <Controls:DataGridTextColumn Width="1*" Binding="{Binding Path=Column2}" Header="Column 2" />
                <Controls:DataGridTextColumn Width="1*" Binding="{Binding Path=Column3}" Header="Column 3" />
    </Controls:DataGrid.Columns>
</Controls:DataGrid>

列宽应该自动调整,以便所有三列都填充网格的宽度,因此我在每一列上设置 Width="1*" 。我在使用这种方法时遇到了两个问题。

  1. 当 DataGrid 的 ItemsSourcenull 或空列表时,列的大小不会适应网格的宽度,而是具有大约 20 像素的固定宽度。请参见下图: http://img169.imageshack.us/img169/3139/initialcolumnwidth.png
  2. 当我最大化应用程序窗口时,列不会调整其大小,而是保持其初始大小。请参见下图: http://img88.imageshack.us/img88/9362/columnwidthaftermaximiz.png
  3. 当我用鼠标调整应用程序窗口大小时,列不会调整大小。

我能够通过从 DataGrid 派生子类并重写 DataGrid 的 OnRenderSizeChanged 方法来解决问题 #3,如下所示。

protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
{
    base.OnRenderSizeChanged(sizeInfo);
    foreach (var column in Columns)
    {
        var tmp = column.GetValue(DataGridColumn.WidthProperty);
        column.ClearValue(DataGridColumn.WidthProperty);
        column.SetValue(DataGridColumn.WidthProperty, tmp);
    }
}

不幸的是,这并不能解决问题#1 和#2。我怎样才能摆脱它们?

I have a WPF DataGrid (from the WPFToolkit package) like the following in my application.

<Controls:DataGrid>
            <Controls:DataGrid.Columns>
                <Controls:DataGridTextColumn Width="1*" Binding="{Binding Path=Column1}" Header="Column 1" />
                <Controls:DataGridTextColumn Width="1*" Binding="{Binding Path=Column2}" Header="Column 2" />
                <Controls:DataGridTextColumn Width="1*" Binding="{Binding Path=Column3}" Header="Column 3" />
    </Controls:DataGrid.Columns>
</Controls:DataGrid>

The column width should be automatically adjusted such that all three columns fill the width of the grid, so I set Width="1*" on every column. I encountered two problems with this approach.

  1. When the ItemsSource of the DataGrid is null or an empty List, the columns won't size to fit the width of the grid but have a fixed width of about 20 pixel. Please see the following picture: http://img169.imageshack.us/img169/3139/initialcolumnwidth.png
  2. When I maximize the application window, the columns won't adapt their size but keep their initial size. See the following picture: http://img88.imageshack.us/img88/9362/columnwidthaftermaximiz.png
  3. When I resize the application window with the mouse, the columns won't resize.

I was able to solve problem #3 by deriving a sub class from DataGrid and override the DataGrid's OnRenderSizeChanged method as follows.

protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
{
    base.OnRenderSizeChanged(sizeInfo);
    foreach (var column in Columns)
    {
        var tmp = column.GetValue(DataGridColumn.WidthProperty);
        column.ClearValue(DataGridColumn.WidthProperty);
        column.SetValue(DataGridColumn.WidthProperty, tmp);
    }
}

Unfortunately this does not solve problems #1 and #2. How can I get rid of them?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

星光不落少年眉 2024-09-06 13:29:21

我和你有完全相同的问题,但我没有听 RenderSizeChanged,而是听了 SizeChanged 事件,它解释了你的所有情况。

dataGrid.SizeChanged += (sender_, arg_) =>
        {
          foreach (var dataGridColumn in dataGrid.Columns)
          {
            DataGridLength dataGridLength = dataGridColumn.Width;
            dataGridColumn.ClearValue(DataGridColumn.WidthProperty);
            dataGridColumn.Width = dataGridLength;
          }
        };

I have exactly the same problem as you but instead of listening to RenderSizeChanged, I listened to the SizeChanged event and it accounts for all of your situations.

dataGrid.SizeChanged += (sender_, arg_) =>
        {
          foreach (var dataGridColumn in dataGrid.Columns)
          {
            DataGridLength dataGridLength = dataGridColumn.Width;
            dataGridColumn.ClearValue(DataGridColumn.WidthProperty);
            dataGridColumn.Width = dataGridLength;
          }
        };
本王不退位尔等都是臣 2024-09-06 13:29:21

我也遇到了你所说的同样的问题。我在互联网上搜索了一下,大概找到了解决方案。该解决方案实际上对我来说很有效。
从 C# 代码设置 * 值。
这是代码片段,

dg.Coulmns[0].Width = new DataGridLength(1.0, DatGridLengthUnitType.Star);

它将把第 0 列的宽度设置为 *。
不要忘记从 XAML 页面中删除 Width="1**"。

享受大师。

I have also faced the same problem as you stated. I searched a bit in the internet, and probably find a solution. The solution actually works for me properly.
Set the * value from C# code.
Here is the code snippet,

dg.Coulmns[0].Width = new DataGridLength(1.0, DatGridLengthUnitType.Star);

It will set the 0th column's width to *.
Don't forget to remove the Width="1**" from the XAML page.

Enjoy Guru.

甜味拾荒者 2024-09-06 13:29:21

以下覆盖修复了宽度变大(包括最大化)的尺寸问题。但仍然不是恢复大小的问题。

protected override void OnChildDesiredSizeChanged(UIElement e)
{
    base.OnChildDesiredSizeChanged(e);

    if (e.FindChild<DataGrid>() != null)
    {
        foreach (var column in dataGridMain.Columns)
        {
            var tmp = column.GetValue(DataGridColumn.WidthProperty);
            column.ClearValue(DataGridColumn.WidthProperty);
            column.SetValue(DataGridColumn.WidthProperty, tmp);
        }
    }
}

PS 这个问题只在我为 DataGrid 定义 GroupStyle 模板时出现,所以它看起来像是一个错误。

The following override fixes the size problem for the width growing bigger including maximize. But still not the restore size problem.

protected override void OnChildDesiredSizeChanged(UIElement e)
{
    base.OnChildDesiredSizeChanged(e);

    if (e.FindChild<DataGrid>() != null)
    {
        foreach (var column in dataGridMain.Columns)
        {
            var tmp = column.GetValue(DataGridColumn.WidthProperty);
            column.ClearValue(DataGridColumn.WidthProperty);
            column.SetValue(DataGridColumn.WidthProperty, tmp);
        }
    }
}

PS this problem only arose for me when I defined a GroupStyle template for the DataGrid, so it seems like a bug.

╰ゝ天使的微笑 2024-09-06 13:29:21

所有 DataGridTextColumn 的默认 MinWidth 属性设置为 20。因此,如果您想在从最大调整到最小大小后控制宽度,则必须自己指定此值。我的建议是:

    <DataGridTextColumn Header="Perfect
column" Binding="{Binding SomeMember}" MinWidth="120" Width="120*"/>    

All DataGridTextColumn has a default MinWidth prperty set to 20. So, if you want to control width after resizing from max to min you have to specify this value yourself. My proposal is:

    <DataGridTextColumn Header="Perfect
column" Binding="{Binding SomeMember}" MinWidth="120" Width="120*"/>    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文