【发布时间】:2015-10-08 16:11:54
【问题描述】:
我在 XAML 中定义了一个样式,该样式应用于 ListView 中的所有 ListViewItem。 ItemsSource 与后面代码中的一个属性进行数据绑定(不,此应用程序中几乎没有 MVVM)。样式具有条件数据触发器。样式如下所示:
<Window.Resources>
<local:IsNullConverter x:Key="isNullConverter"/>
<Style x:Name="buttonStyle" TargetType="{x:Type ListViewItem}">
<Setter Property="IsEnabled" Value="True"/>
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding Converter={StaticResource isNullConverter}, ElementName=lvItems, Path=SelectedItem}" Value="False"/>
<Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsSelected}" Value="False"/>
</MultiDataTrigger.Conditions>
<Setter Property="Effect">
<Setter.Value>
<BlurEffect Radius="2"></BlurEffect>
</Setter.Value>
</Setter>
</MultiDataTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
我有另一个样式可以在 ListView 中进行分组,如下所示:
<Style x:Key="ListViewItemContainerStyle" TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Expander IsExpanded="False">
<Expander.Header>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}" VerticalAlignment="Bottom"/>
</StackPanel>
</Expander.Header>
<ItemsPresenter/>
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
在后面的代码中,我给 ListView 一个 PropertyGroupDescription,如下所示:
CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(Cities);
PropertyGroupDescription groupDescription = new PropertyGroupDescription("Continent");
view.GroupDescriptions.Add(groupDescription);
我需要的是 MultiDataTrigger.Conditions 中的另一个条件,它将当前 ListViewItem 的组与 ListView.SelectedItem 的组进行比较。遗憾的是 WPF 中的 ListViews 没有 ListView.Groups 属性,但如果需要,我可以公开 CollectionView。我该怎么办?
【问题讨论】:
-
唯一的方法是拥有一个 MultiValueConverter,然后将您的集合视图和当前项目传递给它,然后在那里进行比较。
-
@Versatile 感谢您的快速回复。你能给我举个例子吗?
标签: c# wpf xaml data-binding