DataGrid-列宽度未随 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 更新后,它不会减少列的大小。希望这是有道理的。任何帮助表示赞赏。谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对我来说没有任何意义,抱歉。
我理解代码,但这部分:
让我感到困惑,
我是这样理解的:
是这样吗?
如果是这样,这种行为是正常的:如果需要,Wpf 只会将数据网格的列宽设置为“自动”,即:内容无法完全显示。因此,当内容的宽度缩小时,列的大小不会调整,因为内容仍然可以完全看到。
我能看到强制 wpf 重新计算列宽度的唯一方法是将它们全部强制为 0,然后在后面的代码中返回到 auto,并抛出一两个 updateLayout() ,但这不是很好的编程:-/
编辑:
基本上,在您后面的代码中:
您可能需要一个或两个 dg.UpdateLayout() (在更新并可能设置回自动之后)
did not make any sense for me, sorry.
I understand the code, but this part:
has me puzzled
I understood this like that:
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:
and you probably need a dg.UpdateLayout() or two somewhere in there (after the update and the setting back to auto probably)
为了方便大家,这里有一个基于 David 答案的扩展方法:
以及它的调用方式:
For everyone's convenience, here is an extension method based on David's answer:
And how it would be called:
屏幕上的闪烁更少,线条也更少,无需调用 updateLayout:
Much less flash on screen and less lines, no call to updateLayout necessary:
也许我可以帮忙。我遇到了同样的问题,我认为这是一个很好的解决方案。
Maybe I can help. I had the same problem and I found, I think, nice solution.