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

问题描述

我有一个GridView在那里我设置的ItemsSource在code-后面。网格中的所有列在XAML定义,所有列宽度都自动。当我最初设置网格的ItemsSource,列宽设置正确。

现在,这取决于用户的操作,网格的的ItemsSource可被设定为一个新的EntityCollection。我注意到的是,列宽都保持着与previous的ItemsSource。也就是说,列宽似乎并不当一个新的ItemsSource设置为网格来自动调节自己。有什么办法在code-后面或XAML来强制设置列宽时使用的新的ItemsSource网格?我认为,这将是东西,在GridView会自动执行时,它的的ItemsSource复位。

 <的ScrollViewer VerticalScrollBarVisibility =自动>
    <&的ListView GT;
        < ListView.View>
            <&GridView的GT;
                < GridView.Columns>
                    < GridViewColumn WIDTH =自动标题=状态>
                        < GridViewColumn.CellTemplate>
                            <&DataTemplate的GT;
                                <图像宽度=16高度=16来源={绑定路径=阻塞}/>
                            < / DataTemplate中>
                        < /GridViewColumn.CellTemplate>
                    < / GridViewColumn>
                    < GridViewColumn WIDTH =自动标题=标题>
                        < GridViewColumn.CellTemplate>
                            <&DataTemplate的GT;
                                < TextBlock的TextTrimming =CharacterEllipsis文本={结合}/>
                            < / DataTemplate中>
                        < /GridViewColumn.CellTemplate>
                    < / GridViewColumn>
                < /GridView.Columns>
            < / GridView的>
        < /ListView.View>
    < /&的ListView GT;
< /&的ScrollViewer GT;


解决方案

更​​新后,使用此code的ItemsSource:

公共无效AutoSizeGridViewColumns(ListView控件ListView控件)
{
    GridView控件的GridView = listView.View为GridView控件;
    如果(GridView控件!= NULL)
    {
        的foreach(在gridView.Columns VAR列)
        {
            如果(double.IsNaN(column.Width))
                column.Width = column.ActualWidth;
            column.Width = double.NaN;
        }
    }
}

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>
解决方案

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

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

10-16 23:52