0

我有一個ItemsControl內另一個ItemsControl。這個ItemsControl包含一個銷售物品清單。UWP:隱藏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}}"> 

任何想法如何隱藏整個行?

感謝

+0

你可以使用列表框,而不是ItemsControl的。我害怕itemscontrol你除了刪除不需要的項目之外別無選擇。如果你可以使用列表框我可以爲你提供解決方案 – Archana

+0

是的,我可以使用ListBox ...你能幫我嗎? :) – Sam

+0

其實你可以用ItemsControl做。我找到了其他解決方案好吧,我會發布答案。 – Archana

回答

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

嗨LovetoCode,我試過你的解決方案,但「grid.Tag」爲空。任何想法? – Sam

+0

Opps。我會更新代碼。 – Archana

+0

嗨Lovetocode,謝謝你!有用!但是,當字段更新時(Profit變爲正值),它不會再次調用Grid_Loaded。任何想法? – Sam