DataGrid-Column 宽度不会在 ItemsSource 更改时更新 [英] DataGrid-Column widths not updating on ItemsSource change

查看:31
本文介绍了DataGrid-Column 宽度不会在 ItemsSource 更改时更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

XAML:

<DataGrid.Columns>
    <DataGridTextColumn x:Name="colDisplayName" Width="Auto" IsReadOnly="True" Header="Name" Binding="{Binding ssn.SSN_DISPLAY_NAME}"></DataGridTextColumn>
    <DataGridTextColumn x:Name="colValue" Width="Auto" Header="Value" Binding="{Binding ssv.SSV_VALUE}" CellStyle="{StaticResource SingleClickEditing}"></DataGridTextColumn>
    <DataGridTextColumn x:Name="colDescription" Width="Auto" IsReadOnly="True" Header="Description" Binding="{Binding ssn.SSN_DESCRIPTION}"></DataGridTextColumn>
    <DataGridTextColumn x:Name="colUnit" Width="Auto" IsReadOnly="True" Header="Unit Abbreviation" Binding="{Binding ssn.UNIT_TYPE.UNIT_NAME.UN_ABBREVIATION}"></DataGridTextColumn>
    </DataGrid.Columns>

CS:

private void tvSystemConfiguration_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
       {
           dgSystemSettings.ItemsSource =
               ((tvSystemConfiguration.SelectedItem as SYSTEM_SETTINGS_GROUP).SYSTEM_SETTINGS_NAMEs.Join
               (ssdc.SYSTEM_SETTINGS_VALUEs, x => x.SSN_ID, y => y.SSV_SSN_ID, (x, y) => new DataGridItem{ ssn = x, ssv = y })).ToList();
       }

当增加列大小时,列宽会收缩以正确适应,但在减少列大小时不会正确收缩以适应.在 ItemsSource 更新后,它不会减小列的大小.希望这是有道理的.任何帮助表示赞赏.谢谢.

Column widths shrink to fit correctly when increasing in column size but do not properly shrink to fit when decreasing in column size. It will not decrase the size of the column on an ItemsSource update after it has increased. Hope that makes sense. Any help is appreciated. Thank you.

推荐答案

对我来说没有任何意义,抱歉.

did not make any sense for me, sorry.

我理解代码,但这部分:

I understand the code, but this part:

当增加列大小时,列宽会缩小以正确适应,但确实如此减小列大小时无法正确收缩以适应.它不会在 ItemsSource 更新后减小列的大小

Column widths shrink to fit correctly when increasing in column size but do not properly shrink to fit when decreasing in column size. It will not decrase the size of the column on an ItemsSource update after it has increased

让我迷惑了

我是这样理解的:

如果一列内容的宽度增加,则该列的宽度也会增加,但如果内容的宽度减小,列的宽度不减小.

if the width of a column's content increases, the column's width increases, but if the content's width decreases, the column's width does not.

是吗?

如果是这样,这种行为是正常的:如果需要,Wpf 只会将数据网格的列宽调整为自动,即:内容无法完全显示.所以当内容的宽度缩小时,列不会调整大小,因为内容仍然可以完整地看到.

if so, this behaviour is normal: Wpf will just resize a datagrid's column width set to Auto if needed, i.e: the content cannot be displayed entirely. So when the content's width shrinks, the column does not resize as the content can still been seen entirely.

我能看到强制 wpf 重新计算列的宽度的唯一方法是将它们全部强制为 0,然后在后面的代码中返回 auto,并抛出一两个 updateLayout(),但这不是非常好的编程:-/

the only way I can see to force wpf to recalculate the columns' widths would be to force them all to 0 and then back to auto in the code behind, with one or two updateLayout() thrown in, but this is not very nice programming :-/

基本上,在你后面的代码中:

basically, in your code behind:

foreach (DataGridColumn c in dg.Columns)
    c.Width = 0;

// Update your DG's source here

foreach (DataGridColumn c in dg.Columns)
    c.Width = DataGridLength.Auto;

你可能需要一个或两个 dg.UpdateLayout() 在那里的某个地方(在更新和设置回自动之后)

and you probably need a dg.UpdateLayout() or two somewhere in there (after the update and the setting back to auto probably)

这篇关于DataGrid-Column 宽度不会在 ItemsSource 更改时更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆