[英]GridView column width does not update when changing ItemsSource
I have a GridView where I'm setting the ItemsSource in code-behind. 我有一个GridView,我在代码隐藏中设置ItemsSource。 All columns in the grid are defined in XAML, and all column widths are "Auto".
网格中的所有列都在XAML中定义,所有列宽都是“自动”。 When I initially set ItemsSource of the grid, the column widths are set correctly.
当我最初设置网格的ItemsSource时,列宽正确设置。
Now, depending on the user's actions, the ItemsSource of the grid may be set to a new EntityCollection. 现在,根据用户的操作,可以将网格的ItemsSource设置为新的EntityCollection。 What I have noticed is that the column widths remain as they were with the previous ItemsSource.
我注意到的是,列宽仍然与之前的ItemsSource一样。 That is, the column widths don't seem to adjust themselves automatically when a new ItemsSource is set for the Grid.
也就是说,当为Grid设置新的ItemsSource时,列宽似乎不会自动调整。 Is there any way in code-behind or XAML to force the Grid to use the new ItemsSource when setting the column widths?
代码隐藏或XAML中是否有任何方法可以在设置列宽时强制Grid使用新的ItemsSource? I would think that this would be something that the GridView would do automatically when it's ItemsSource is reset.
我认为这将是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>
Use this code after updating ItemsSource: 更新ItemsSource后使用此代码:
public void AutoSizeGridViewColumns(ListView listView)
{
GridView gridView = listView.View as GridView;
if (gridView != null)
{
foreach (var column in gridView.Columns)
{
if (double.IsNaN(column.Width))
column.Width = column.ActualWidth;
column.Width = double.NaN;
}
}
}
I have created the following class and used across the application whereever required in place of GridView 我创建了以下类,并在应用程序中使用,代替GridView
/// <summary>
/// Represents a view mode that displays data items in columns for a System.Windows.Controls.ListView control with auto sized columns based on the column content
/// </summary>
public class AutoSizedGridView : GridView
{
protected override void PrepareItem(ListViewItem item)
{
foreach (GridViewColumn column in Columns)
{
//setting NaN for the column width automatically determines the required width enough to hold the content completely.
//if column width was set to NaN already, set it ActualWidth temporarily and set to NaN. This raises the property change event and re computes the width.
if (double.IsNaN(column.Width)) column.Width = column.ActualWidth;
column.Width = double.NaN;
}
base.PrepareItem(item);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.