UWP:隐藏项目控制项目
UWP: Hide ItemsControl items
我在另一个 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}}">
知道如何隐藏整行吗?
谢谢
<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;
}
}
}
我在另一个 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}}">
知道如何隐藏整行吗?
谢谢
<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;
}
}
}