更改 ItemsSource 时 GridView 列宽不会更新

发布于 2024-12-29 18:19:55 字数 1558 浏览 5 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ぃ弥猫深巷。 2025-01-05 18:19:55

更新 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; 
        } 
    } 
} 

Use this code after updating 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; 
        } 
    } 
} 
野生奥特曼 2025-01-05 18:19:55

我创建了以下类,并在应用程序中需要的地方使用它来代替 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);
    }
}

I have created the following class and used across the application whereever required in place of 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);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文