简体   繁体   English

ListViewItem的绑定可见性

[英]Binding Visiblity of ListViewItem

I'm developing an App for Windows Phone 8.1. 我正在为Windows Phone 8.1开发一个应用程序。 In that App I want to bind the Items of an ObservableCollection<DisruptionDisplayElement> to a ListView . 在该应用程序中,我想将ObservableCollection<DisruptionDisplayElement>的项目绑定到ListView DisruptionDisplayElement has a property named bool IsFavorite . DisruptionDisplayElement具有一个名为bool IsFavorite的属性。 In the ListView I want to hide all items, where IsFavorite is false. 在ListView中,我要隐藏IsFavorite为false的所有项目。 If I do it by using ItemContainerStyle and set the Visibility -Property to collapsed by using a Converter, it is not working. 如果我使用ItemContainerStyle做到这一点,并使用Converter将Visibility -Property设置为折叠,则它将无法正常工作。 If I define the Backgroundcolor the same way for testing, it works. 如果我以相同的方式定义Backgroundcolor进行测试,那么它将起作用。 I can also hide the Grid, where everything of the ListViewItem is in, but in that case I still have the the decoration of the ListViewItem , that takes round about 50 pixels of space. 我还可以隐藏Grid,其中包含ListViewItem所有内容,但是在那种情况下,我仍然具有ListViewItem的修饰,它需要大约50个像素的空间。

Here is what I've got: 这是我所拥有的:

XAML: XAML:

<Page
    x:Class="myApp.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:myApp"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:converter="using:myApp.Converter"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
    >
    <Page.Resources>            
        <converter:BoolToVisibilityConverter x:Key="BoolToVisibilityConv"/>
    </Page.Resources>
    <Grid>
        <Hub Header="{Binding CityName}" 
             SectionsInViewChanged="Hub_SectionsInViewChanged" 
             Grid.Row="1"
             >
             <HubSection Header="My Lines" Name="hubFavorites">
                 <DataTemplate>
                     <Grid Margin="0,-25,0,0">
                         <ListView 
                             ItemsSource="{Binding DisruptionDisplayList}"
                             Grid.Row="1"
                             >
                             <ListView.ItemContainerStyle>
                         <Style TargetType="ListViewItem">
                                     <!-- This seems not to work -->
                                     <Setter Property="Visibility" Value="{Binding IsFavorite, Converter={StaticResource BoolToVisibilityConv}}"/>
                                     <!-- For testing -->
                                     <Setter Property="Background" Value="Aqua"/>
                                 </Style>
                             </ListView.ItemContainerStyle>
                             <ListView.ItemTemplate>
                                 <DataTemplate>
                                     <!-- The Visibility-Property is just for testing as described -->
                                     <Grid 
                                         Margin="0,0,0,10" 
                                         Visibility="{Binding IsFavorite, Converter={StaticResource BoolToVisibilityConv}}"
                                         >
                                         <!-- Content here -->
                                         <TextBlock Text="{Binding Message}"/>
                                      </Grid>
                                 </DataTemplate>
                              </ListView.ItemTemplate>
                         </ListView>
                     </Grid>
                </DataTemplate>
            </HubSection>
        </Hub>
    </Grid>
</Page>

The Converter: 转换器:

namespace myApp.Converter
{
    public class BoolToVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string culture)
        {
            return (bool) value ? Visibility.Visible : Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, string culture)
        {
            throw new NotImplementedException();
        }
    }
}

DisruptionDisplayElement: DisruptionDisplayElement:

public class DisruptionDisplayElement
{
    public string Message { get; set; }

    public bool IsFavorite { get; set; }
}

Code Behind: 背后的代码:

namespace myApp
{
    public sealed partial class MainPage
    {
        public MainPage()
        {
            InitializeComponent();

            DataContext = new ViewModel;
        }
    }
}

My "ViewModel"-Class: 我的“ ViewModel”类:

namespace myApp
{
    public class ViewModel
    {
        public ObserverableCollection<DisruptionDisplayElement> DisruptionDisplayList {get;set;}

        public ViewModel()
        {
            DisruptionDisplayList = new ObservableCollection<DisruptionDisplayElement>();
            DisruptionDisplayList.Add(new DisruptionDisplayElement() { IsFavorite = true, Message = "Message 1"});
            DisruptionDisplayList.Add(new DisruptionDisplayElement() { IsFavorite = false, Message = "Message 2" });
            DisruptionDisplayList.Add(new DisruptionDisplayElement() { IsFavorite = true, Message = "Message 3" });
        }
    }
}

What can I do to hide the ListViewItem without wasting all the space for emtpy ListViewItems if I hide the grid inside? 如果我将网格隐藏在里面,该怎么办才能隐藏ListViewItem而不浪费Emtpy ListViewItems的所有空间?

Edit: Advanced the Code providing 编辑:高级代码提供

Without a good, minimal , complete code example , it's not possible to provide an actual code example that shows the correct technique in your exact scenario. 如果没有一个好的, 最小的完整的代码示例 ,就无法提供一个实际的代码示例来显示您的确切方案中的正确技术。

However, the basic answer is that you should be using ICollectionView to filter the visual presentation based on some property. 但是,基本的答案是您应该使用ICollectionView来基于某些属性来过滤视觉表示。

There are a variety of ways to go about doing this. 有多种方法可以执行此操作。 One of the simplest is to apply the filtering to the default view for your data source. 最简单的方法之一就是将过滤应用于数据源的默认视图。 Such a view always exists, and if you are only binding your data source to a single visual object, or you want all visual objects that present that data source to be filtered in the same way, then getting and modifying this view is the correct approach. 这样的视图始终存在,并且如果您仅将数据源绑定到单个视觉对象,或者您希望所有以相同方式过滤表示该数据源的视觉对象,那么获取和修改此视图是正确的方法。

As an example: 举个例子:

ICollectionView view = CollectionViewSource.GetDefaultView(DisruptionDisplayList);

view.Filter = item => ((MyClass)item).IsFavorite;

You would configure this view in your code-behind at the appropriate time, eg when the user indicates through whatever input mechanism you've provided that they want to show only the favorite items. 您可以在适当的时候在代码后方配置此视图,例如,当用户通过您提供的任何输入机制指示他们仅想显示喜欢的项目时,就进行配置。

Note that this approach avoids altogether trying to use the DataTemplate as the mechanism for showing or hiding the items. 请注意,这种方法避免了完全尝试使用DataTemplate作为显示或隐藏项目的机制。 Instead, the items are filtered before they ever reach the ListView object. 而是在项到达ListView对象之前对其进行过滤。

if you are looking for UWP, just set MinHeight Property to 0 (zero): 如果要查找UWP,只需将MinHeight属性设置为0(零)即可:

<ListView.ItemContainerStyle>
     <Style TargetType="ListViewItem">
          <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
          <Setter Property="MinHeight" Value="0" />

     </Style>
</ListView.ItemContainerStyle>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM