更改 ItemsSource 时 GridView 列宽不会更新
我有一个 GridView,我在代码隐藏中设置 ItemsSource。网格中的所有列均在 XAML 中定义,并且所有列宽度均为“自动”。当我最初设置网格的 ItemsSource 时,列宽设置正确。
现在,根据用户的操作,网格的 ItemsSource 可能会设置为新的 EntityCollection。我注意到列宽与之前的 ItemsSource 保持一致。也就是说,当为网格设置新的 ItemsSource 时,列宽似乎不会自动调整。在代码隐藏或 XAML 中是否有任何方法可以强制 Grid 在设置列宽度时使用新的 ItemsSource?我认为这将是 GridView 在重置 ItemsSource 时自动执行的操作。
<ScrollViewer VerticalScrollBarVisibility="Auto">
<ListView>
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn Width="Auto" Header="Status">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Image Width="16" Height="16" Source="{Binding Path=Blocking}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Width="Auto" Header="Title">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock TextTrimming="CharacterEllipsis" Text="{Binding}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
</ScrollViewer>
I have a GridView where I'm setting the ItemsSource in code-behind. All columns in the grid are defined in XAML, and all column widths are "Auto". When I initially set ItemsSource of the grid, the column widths are set correctly.
Now, depending on the user's actions, the ItemsSource of the grid may be set to a new EntityCollection. What I have noticed is that the column widths remain as they were with the previous ItemsSource. That is, the column widths don't seem to adjust themselves automatically when a new ItemsSource is set for the Grid. Is there any way in code-behind or XAML to force the Grid to use the new ItemsSource when setting the column widths? I would think that this would be something that the GridView would do automatically when it's ItemsSource is reset.
<ScrollViewer VerticalScrollBarVisibility="Auto">
<ListView>
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn Width="Auto" Header="Status">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Image Width="16" Height="16" Source="{Binding Path=Blocking}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Width="Auto" Header="Title">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock TextTrimming="CharacterEllipsis" Text="{Binding}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
</ScrollViewer>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
更新 ItemsSource 后使用此代码:
Use this code after updating ItemsSource:
我创建了以下类,并在应用程序中需要的地方使用它来代替 GridView
I have created the following class and used across the application whereever required in place of GridView