WPF DataGrid 列宽和重新排序

发布于 2024-08-02 01:39:12 字数 1585 浏览 7 评论 0原文

我有一个 DataGrid 定义为

<wpftoolkit:DataGrid
        x:Name="AccountsDataGrid"
        AutoGenerateColumns="False"
        ItemsSource="{Binding Path=Accounts}"
        ColumnReordered="DataGrid_ColumnReordered"
        SelectionUnit="FullRow"
        RowHeaderWidth="0"
        HorizontalAlignment="Stretch"
        VerticalAlignment="Stretch"
        >
        <wpftoolkit:DataGrid.Columns>
            <wpftoolkit:DataGridTextColumn Header="Account Id" Binding="{Binding Path=AccountId}" Width="Auto" />
            <wpftoolkit:DataGridTextColumn Header="Account Name" Binding="{Binding Path=AccountName}" Width="*" />
        </wpftoolkit:DataGrid.Columns>
    </wpftoolkit:DataGrid>

加载时看起来很棒。 第一列适合容纳内容和标题所需的最小宽度。 第二列延伸以填充 DataGrid 的其余宽度(因此我没有第三个填充列)。 但是,如果我尝试对列重新排序,则 AccountName 列的宽度无法调整为小于重新排序之前的宽度。 因此,我在 ColumnReordered 事件上添加了一个处理程序,认为我可以重置列宽度,但它似乎不起作用。 事实上,它将 AccountId 列缩小到几乎为零,并且 AccountName 列的大小仍然无法调整得更小。

private void DataGrid_ColumnReordered(object sender, Microsoft.Windows.Controls.DataGridColumnEventArgs e)
    {
        foreach (DataGridColumn column in AccountsDataGrid.Columns)
        {
            if (column.Equals(AccountsDataGrid.Columns.Last()))
            {
                column.Width = new DataGridLength(1.0, DataGridLengthUnitType.Star);
            }
            else
            {
                column.Width = new DataGridLength(1.0, DataGridLengthUnitType.Auto);
            }
        }
    }

有没有更好的方法来处理列宽以及为什么列重新排序会破坏列调整大小的能力

I have a DataGrid defined as

<wpftoolkit:DataGrid
        x:Name="AccountsDataGrid"
        AutoGenerateColumns="False"
        ItemsSource="{Binding Path=Accounts}"
        ColumnReordered="DataGrid_ColumnReordered"
        SelectionUnit="FullRow"
        RowHeaderWidth="0"
        HorizontalAlignment="Stretch"
        VerticalAlignment="Stretch"
        >
        <wpftoolkit:DataGrid.Columns>
            <wpftoolkit:DataGridTextColumn Header="Account Id" Binding="{Binding Path=AccountId}" Width="Auto" />
            <wpftoolkit:DataGridTextColumn Header="Account Name" Binding="{Binding Path=AccountName}" Width="*" />
        </wpftoolkit:DataGrid.Columns>
    </wpftoolkit:DataGrid>

which on load looks great. the first column fits to the minimum width necessary to fit both the content and the header. the second column stretches to fill the rest of the width of the DataGrid (so I don't have a 3rd filler column). But if I try to reorder the columns, the AccountName column cannot be resized to a width less than what its width was before the reorder. So I added a handler on the ColumnReordered event, figuring I can just reset the column widths, but it doesn't seem to work. In fact, it shrinks the AccountId column to almost nothing, and the AccountName column still cannot be resized smaller.

private void DataGrid_ColumnReordered(object sender, Microsoft.Windows.Controls.DataGridColumnEventArgs e)
    {
        foreach (DataGridColumn column in AccountsDataGrid.Columns)
        {
            if (column.Equals(AccountsDataGrid.Columns.Last()))
            {
                column.Width = new DataGridLength(1.0, DataGridLengthUnitType.Star);
            }
            else
            {
                column.Width = new DataGridLength(1.0, DataGridLengthUnitType.Auto);
            }
        }
    }

Is there a better way to handle the column widths and why does the column reordering break the column resizing ability

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

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

发布评论

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

评论(1

月寒剑心 2024-08-09 01:39:12

列在列集合中的位置没有改变。 只有 DisplayIndexes 发生变化。 尝试这个:

  private void DataGrid_ColumnReordered(object sender, DataGridColumnEventArgs e)
    {
        int lastColumnOrder = AccountsDataGrid.Columns.Count() - 1;
        foreach (DataGridColumn column in AccountsDataGrid.Columns)
        {
            if (column.DisplayIndex == lastColumnOrder)
            {
                column.Width = new DataGridLength(1.0, DataGridLengthUnitType.Star);
            }
            else
            {
                column.Width = new DataGridLength(1.0, DataGridLengthUnitType.Auto);
            }
        } 
    }

The columns are not changing position in the columns collection. Only the DisplayIndexes are changing. Try this:

  private void DataGrid_ColumnReordered(object sender, DataGridColumnEventArgs e)
    {
        int lastColumnOrder = AccountsDataGrid.Columns.Count() - 1;
        foreach (DataGridColumn column in AccountsDataGrid.Columns)
        {
            if (column.DisplayIndex == lastColumnOrder)
            {
                column.Width = new DataGridLength(1.0, DataGridLengthUnitType.Star);
            }
            else
            {
                column.Width = new DataGridLength(1.0, DataGridLengthUnitType.Auto);
            }
        } 
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文