隐藏GridViewItem并在GridView中重新定位项目

时间:2016-02-16 01:25:28

标签: c# asp.net xaml gridview win-universal-app

enter image description here

正如你在上面所看到的,我需要它重新调整自己。显然这些项目仍然存在,有没有办法完全忽略它们?

我有一个ObservableCollection:

 public static volatile ObservableCollection<MyVideo> MyVideoModels = new ObservableCollection<MyVideo>();

这充满了MyVideo对象。

将它绑定到我的GridView,如下所示:

public VideosFoundView()
        {
            this.InitializeComponent();
            this.AddVideoFolderGridView.ItemsSource = VideosFoundView.MyVideoModels;
        }

我用于GridView的DataTemplate。

<GridView Grid.Row="2" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" x:Name="AddVideoFolderGridView">
            <GridView.ItemTemplate>
                <DataTemplate>
                    <Border BorderThickness="5">
                        <Image Source="{Binding FullImageLocationOnDisk}"  MaxHeight="300" MaxWidth="200" DoubleTapped="gridViewItem_DoubleTapped" Loaded="gridViewItem_Loaded" Loading="gridViewItem_Loading">
                        </Image>
                    </Border>
                </DataTemplate>
            </GridView.ItemTemplate>
        </GridView>

我在这个GridView上面有一个静态ToggleSwitch,如下所示:

 <Grid Grid.Row="1">
                <Grid>
                    <TextBlock Text="Ingore Image Not Found"/>
                    <ToggleSwitch x:Name="ToggleSwitchIgnoreImages" Toggled="ignoreImages_Toggled"/>
                </Grid>
            </Grid>

其中:

 private void ignoreImages_Toggled(object sender, RoutedEventArgs e)
        {
            ToggleSwitch tSwitch = (ToggleSwitch)(sender as ToggleSwitch);
            if (tSwitch.IsOn)
            {
                for(int i = 0; i < VideosFoundView.MyVideoModels.Count; i++)
                {
                    if(VideosFoundView.MyVideoModels[i].FullImageLocationOnDisk == "ms-appx:///Assets/image-not-found.gif")
                    {
                        var gridViewItem = (GridViewItem)this.AddVideoFolderGridView.ContainerFromIndex(i);
                        gridViewItem.Visibility = Visibility.Collapsed;
                    }
                }
            }
            else
            {
                for (int i = 0; i < VideosFoundView.MyVideoModels.Count; i++)
                {
                    //VideosFoundView.MyVideoModels[i].Visibility = "Auto";         
                    var gridViewItem = (GridViewItem)this.AddVideoFolderGridView.ContainerFromIndex(i);
                    gridViewItem.Visibility = Visibility.Visible;
                }
            }
        }

然而问题是这些项目仍在我的GridView上占用空间,而其他项目也没有相应地重新定位。

2 个答案:

答案 0 :(得分:1)

GridView折叠时,GridViewItem上的项目仍占用空间。可能是因为ItemTemplate没有刷新,所以空间仍然保留。

作为一种解决方法,项目WinRTXamlToolkit中有一个用于UWP的Toolkit的WrapPanel端口。

您可以从NuGet获取。

然后在你的页面中添加这个前缀:

xmlns:toolkit="using:WinRTXamlToolkit.Controls"

现在您可以像以前一样使用<toolkit:WrapPanel Orientation="Horizontal" ></toolkit:WrapPanel>

例如:

<GridView x:Name="AddVideoFolderGridView">
    <GridView.ItemsPanel>
        <ItemsPanelTemplate>
            <toolkit:WrapPanel Orientation="Horizontal"  >
            </toolkit:WrapPanel>
        </ItemsPanelTemplate>
    </GridView.ItemsPanel>
    <GridView.ItemTemplate>
        <DataTemplate>
            <Border BorderThickness="5">
                <Image Source="{Binding FullImageLocationOnDisk}"  MaxHeight="300" MaxWidth="200" DoubleTapped="gridViewItem_DoubleTapped" Loaded="gridViewItem_Loaded" Loading="gridViewItem_Loading">
                </Image>
            </Border>
        </DataTemplate>
    </GridView.ItemTemplate>
</GridView>

答案 1 :(得分:1)

此处的解决方案:Not showing items with Visibility=Collapsed in Windows 8.1 GridView

tldr: 编辑GridView的模板,并将ItemsPanelTemplate中的ItemsWrapGrid替换为您可以在此处找到的WrapPanel http://codepaste.net/8gr5go