UWP:隐藏ItemsControl项目

时间:2016-05-13 08:15:47

标签: win-universal-app itemscontrol windows-10-universal listbox-control

我在另一个ItemsControl中有一个ItemsControl。此ItemsControl包含已售商品的列表。

我想显示那些低于成本(负利润)的商品。 但是我有问题折叠项目(行)。

<ItemsControl>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Grid>
                ...
                ...
                ...
                <ItemsControl ItemsSource="{Binding SoldItems}">
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <Grid>  <!--If I hide the grid, it will create empty space.-->
                                ...
                                ...
                                ...
                                <TextBlock Text="{Binding Profit}"></TextBlock>
                            </Grid>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
            </Grid>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

如果我试图隐藏网格,它会显示空行。它看起来很奇怪,因为那里有空隙。

<Grid Visibility="{Binding Profit, Mode=OneWay, Converter={StaticResource ProfitVisibilityConverter}}">

知道如何隐藏整行吗?

由于

1 个答案:

答案 0 :(得分:1)

<ListView x:Name="MyListView">
    <ListView.ItemTemplate>
        <DataTemplate >
            <Grid Visibility="{Binding visible}"  Tag="{Binding ElementName=MyListView}" Loaded="Grid_Loaded"  >
                <TextBlock Text="{Binding Name}"/>
            </Grid>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

private void Grid_Loaded(object sender, RoutedEventArgs e)
{
    Grid grid = sender as Grid;
    // For items control
    // ContentPresenter item = (ContentPresenter)(grid.Tag as ItemsControl).ContainerFromItem(grid.DataContext);
    ListViewItem item = (ListViewItem)(grid.Tag as ItemsControl).ContainerFromItem(grid.DataContext);
    // Or you can directly access listview
    // ListViewItem item = (ListViewItem)(MyListView as ItemsControl).ContainerFromItem(grid.DataContext);
    if ((grid.DataContext as Test).visible == Visibility.Collapsed)
    {
        if (item != null)
        {
            Binding binding = new Binding();
            binding.Mode = BindingMode.TwoWay;
            binding.Source = (grid.DataContext as your model class)
            binding.Path = new PropertyPath("givevisibilityproperty");

            // Attach the binding to the target.
            item.SetBinding(ListViewItem.VisibilityProperty, binding);

            // (item ).Visibility = Visibility.Collapsed;
        }
    }
}