DataGrid - 在ItemsSource上不更新的列宽度更改 [英] DataGrid-Column widths not updating on ItemsSource change

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

问题描述

XAML:

 < DataGrid.Columns> 
< DataGridTextColumn x:Name =colDisplayNameWidth =AutoIsReadOnly =TrueHeader =NameBinding ={Binding ssn.SSN_DISPLAY_NAME}>< / DataGridTextColumn>
< DataGridTextColumn x:Name =colValueWidth =AutoHeader =ValueBinding ={Binding ssv.SSV_VALUE}CellStyle ={StaticResource SingleClickEditing}>< / DataGridTextColumn>
< DataGridTextColumn x:Name =colDescriptionWidth =AutoIsReadOnly =TrueHeader =DescriptionBinding ={Binding ssn.SSN_DESCRIPTION}>< / DataGridTextColumn>
< DataGridTextColumn x:Name =colUnitWidth =AutoIsReadOnly =TrueHeader =Unit AbbreviationBinding ={Binding ssn.UNIT_TYPE.UNIT_NAME.UN_ABBREVIATION}>< / DataGridTextColumn> ;
< /DataGrid.Columns>

CS:

 code> 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})。
}

当列大小增加时,列宽缩小到正确,但不能正确收缩以减小柱尺寸。它不会在ItemsSource更新增加后减小列的大小。希望有道理任何帮助是赞赏。谢谢。

解决方案

对我没有任何意义,对不起,



我了解代码,但这部分:


当列大小增加时,列宽缩小到正确,但$ b $当柱尺寸减小时,b没有适当收缩。
不会在ItemSource更新的列之后递增


让我困惑



我理解为这样:


如果列内容的宽度增加,列的宽度增加,但是如果
的宽度减小,则列的宽度不会。


是对吗? p>

如果是这样,这个行为是正常的:如果需要的话,Wpf将调整datagrid的列宽设置为Auto,即:内容无法完全显示。所以当内容的宽度缩小时,列不会调整大小,因为内容仍然可以被完全看到。



我看到的唯一方法是强制wpf重新计算列'宽度将是强制他们全部为0,然后返回到自动的代码后面,一个或两个updateLayout()抛出,但这不是很好的编程: - /



编辑:
在您的代码中:

  foreach dg.Columns中的DataGridColumn c)
c.Width = 0;

//在这里更新您的DG的源

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

你可能需要一个dg.UpdateLayout()或两个在那里(更新和可能回到汽车)


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

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:

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

has me puzzled

I understood this like that:

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.

Is that right ?

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.

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

Edit: 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;

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

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

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