【发布时间】:2015-05-08 22:45:41
【问题描述】:
我正在为 Windows Phone 8.1 开发应用程序。在那个应用程序中,我想将ObservableCollection<DisruptionDisplayElement> 的项目绑定到ListView。 DisruptionDisplayElement 有一个名为 bool IsFavorite 的属性。在 ListView 我想隐藏所有项目,其中IsFavorite 为假。
如果我通过使用ItemContainerStyle 来执行此操作并使用转换器将Visibility-Property 设置为折叠,则它不起作用。如果我以相同的方式定义 Backgroundcolor 进行测试,它就可以工作。
我还可以隐藏网格,ListViewItem 的所有内容都在其中,但在这种情况下,我仍然拥有 ListViewItem 的装饰,这需要大约 50 像素的空间。
这是我得到的:
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>
转换器:
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();
}
}
}
中断显示元素:
public class DisruptionDisplayElement
{
public string Message { get; set; }
public bool IsFavorite { get; set; }
}
代码背后:
namespace myApp
{
public sealed partial class MainPage
{
public MainPage()
{
InitializeComponent();
DataContext = new ViewModel;
}
}
}
我的“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" });
}
}
}
如果我将网格隐藏在里面,我该怎么做才能隐藏 ListViewItem 而不会浪费所有空间给空的 ListViewItems?
编辑: 高级代码提供
【问题讨论】:
标签: c# xaml listview winrt-xaml