C# 无法在DropDownButton中组织数据
总结 我有一个C# 无法在DropDownButton中组织数据,c#,wpf,xaml,mahapps.metro,C#,Wpf,Xaml,Mahapps.metro,总结 我有一个下拉按钮,包含特定国家的所有联盟。因此,基本上用户从组合框中选择国家,所选国家的联盟将添加到下拉按钮中。在这里之前没问题 我需要做的是:在下拉按钮中为国家组织联盟。想象一下,在下拉按钮中,我有一个组织: Italy Item1 Item2 Item3 England Item1 Item2 Spain Item1 Item2 ... etc ... 项目类型 如何查看项目是按国家组织的。在解释我为实现这个结
下拉按钮
,包含特定国家的所有联盟。因此,基本上用户从组合框中选择国家
,所选国家
的联盟
将添加到下拉按钮
中。在这里之前没问题
我需要做的是:在下拉按钮中为国家组织联盟
。想象一下,在下拉按钮中,我有一个组织:
Italy
Item1
Item2
Item3
England
Item1
Item2
Spain
Item1
Item2
... etc ...
项目类型
如何查看项目是按国家组织的。在解释我为实现这个结果做了什么之前,我必须说我使用的是一个CheckedListItem
类,它本质上是一个将复选框
绑定到下拉按钮
项的类。这个类非常简单:
public class CheckedListItem<T> : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private bool isChecked;
private T item;
public CheckedListItem() { }
public CheckedListItem(T item, bool isChecked = false)
{
this.item = item;
this.isChecked = isChecked;
}
public T Item
{
get { return item; }
set
{
item = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Item"));
}
}
public bool IsChecked
{
get { return isChecked; }
set
{
isChecked = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("IsChecked"));
}
}
}
解释问题
现在,为了在下拉按钮中实现分组,我首先在窗口中创建了一个CollectionViewSource
<CollectionViewSource Source="{Binding Leagues}" x:Key="GroupedData">
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="Item.Country" />
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
<DataTemplate x:Key="GroupHeader">
<TextBlock Text="{Binding Name}" Margin="10,0,0,0" Foreground="#989791"/>
</DataTemplate>
(请注意,我使用的是MahApp)
因此,本质上我为CheckedListItem
和ItemSource
添加了一个模板GroupedData
,您可以从上面的代码中看到如何绑定联盟的ObservableCollection
数据总体
当选择一个国家
时,将在下拉按钮
中添加可用于所选国家
的联盟
列表,用于实现此目的的代码:
private ObservableCollection<CheckedListItem<League>> _leagues = new ObservableCollection<CheckedListItem<League>>();
public ObservableCollection<CheckedListItem<League>> Leagues
{
get
{
return _leagues;
}
set
{
_leagues = value;
OnPropertyChanged();
}
}
本质上,我创建了一个League
的CheckedListItem
,该模型包含以下属性:
public class League
{
public string Name { get; set; }
public string Link { get; set; }
public string Country { get; set; }
}
应该显示的结果如下所示:
<CollectionViewSource Source="{Binding Leagues}" x:Key="GroupedData">
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="Item.Country" />
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
<DataTemplate x:Key="GroupHeader">
<TextBlock Text="{Binding Name}" Margin="10,0,0,0" Foreground="#989791"/>
</DataTemplate>
拿走了
但我得到的结果是:
如您所见,我没有使用Nation
名称进行标题分组。我在XAML或代码中也没有错误,所以我不知道为什么标题没有出现
我知道这个问题可能有点复杂,所以如果有任何问题或更多细节,请问我,我会尽力回答
演示解决方案:
我不想让第三方控件正常工作,而是改变工作的组合框来满足您的需要:
main window.xaml
<Controls:MetroWindow x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:Controls="http://metro.mahapps.com/winfx/xaml/controls"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
Title="MainWindow" Height="700" Width="525">
<Window.Resources>
<ResourceDictionary>
<CollectionViewSource Source="{Binding Leagues}" x:Key="GroupedData">
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="Item.Country" />
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
<DataTemplate x:Key="GroupHeader">
<TextBlock Text="{Binding Name}" Margin="10,0,0,0" Foreground="#989791"/>
</DataTemplate>
<Style TargetType="{x:Type GroupItem}" x:Key="containerStyle">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander IsExpanded="False" x:Name="ComboExpander" Header="{TemplateBinding Content}"
HeaderTemplate="{StaticResource GroupHeader}">
<ItemsPresenter />
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<DataTemplate x:Key="NormalItemTemplate">
<CheckBox IsChecked="{Binding IsChecked}" Content="{Binding Item.Name}" x:Name="Leagues" />
</DataTemplate>
<DataTemplate x:Key="HeaderTemplate">
<TextBlock Text="Campionati"/>
</DataTemplate>
<DataTemplate x:Key="CombinedTemplate">
<ContentPresenter x:Name="Presenter"
Content="{Binding}"
ContentTemplate="{StaticResource NormalItemTemplate}" />
<DataTemplate.Triggers>
<DataTrigger
Binding="{Binding RelativeSource={RelativeSource FindAncestor, ComboBoxItem, 1}}"
Value="{x:Null}">
<Setter TargetName="Presenter" Property="ContentTemplate"
Value="{StaticResource HeaderTemplate}" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</ResourceDictionary>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<ComboBox x:Name="LeagueMenuComboBox"
ItemsSource="{Binding Source={StaticResource GroupedData}}"
ItemTemplate="{StaticResource CombinedTemplate}"
HorizontalContentAlignment="Center">
<ComboBox.GroupStyle>
<GroupStyle ContainerStyle="{StaticResource containerStyle}"
HeaderTemplate="{StaticResource GroupHeader}">
</GroupStyle>
</ComboBox.GroupStyle>
</ComboBox>
</Grid>
</Controls:MetroWindow>
我这里没有MahApp,但在这种情况下,我通常会为各种元素添加不同的背景色。这样可以更容易地看到正在显示哪些模板和元素。@PieterWitvoet问题出在分组中不起作用的combinedTemplate
上。似乎是的副本。感谢您花了这么多时间。在接受你的回答之前,我只有几个问题:1。是否可以在组合框中独立于所选项目设置默认文本?2.是否可以更改GroupBox文本的颜色(我指的是国家名称的颜色)?谢谢。@AgainMe不客气,回答你的问题:1。这就是CombinedTemplate
的作用,它确保无论发生什么情况,默认文本都保持不变。2.您可以获得蓝色国家名称,将GroupHeader
内容设置为:
谢谢,我需要等待时间限制才能向您发送赏金。只有一件事:很遗憾,我没有在组合框中显示默认文本。@AgainMe默认文本是在HeaderTemplate
中设置的。请注意,DataTrigger绑定中的祖先现在是类型ComboBoxItem
(而不是DropDownButton
)。如果这没有帮助,请尝试用我发布的代码替换链接项目中的xaml文件。我理解为什么不工作。因此,当我启动应用程序时,组合框上没有文本,如果我选中某个项目,同样的情况下,只有当我在组合框上选择该项目时,文本才会出现。应用程序运行时,是否无法保留文本?我想设置selectedindex=0来解决这个问题,但也许您知道另一个解决方案。
public class League
{
public string Name { get; set; }
public string Link { get; set; }
public string Country { get; set; }
}
<Controls:MetroWindow x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:Controls="http://metro.mahapps.com/winfx/xaml/controls"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
Title="MainWindow" Height="700" Width="525">
<Window.Resources>
<ResourceDictionary>
<CollectionViewSource Source="{Binding Leagues}" x:Key="GroupedData">
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="Item.Country" />
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
<DataTemplate x:Key="GroupHeader">
<TextBlock Text="{Binding Name}" Margin="10,0,0,0" Foreground="#989791"/>
</DataTemplate>
<Style TargetType="{x:Type GroupItem}" x:Key="containerStyle">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander IsExpanded="False" x:Name="ComboExpander" Header="{TemplateBinding Content}"
HeaderTemplate="{StaticResource GroupHeader}">
<ItemsPresenter />
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<DataTemplate x:Key="NormalItemTemplate">
<CheckBox IsChecked="{Binding IsChecked}" Content="{Binding Item.Name}" x:Name="Leagues" />
</DataTemplate>
<DataTemplate x:Key="HeaderTemplate">
<TextBlock Text="Campionati"/>
</DataTemplate>
<DataTemplate x:Key="CombinedTemplate">
<ContentPresenter x:Name="Presenter"
Content="{Binding}"
ContentTemplate="{StaticResource NormalItemTemplate}" />
<DataTemplate.Triggers>
<DataTrigger
Binding="{Binding RelativeSource={RelativeSource FindAncestor, ComboBoxItem, 1}}"
Value="{x:Null}">
<Setter TargetName="Presenter" Property="ContentTemplate"
Value="{StaticResource HeaderTemplate}" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</ResourceDictionary>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<ComboBox x:Name="LeagueMenuComboBox"
ItemsSource="{Binding Source={StaticResource GroupedData}}"
ItemTemplate="{StaticResource CombinedTemplate}"
HorizontalContentAlignment="Center">
<ComboBox.GroupStyle>
<GroupStyle ContainerStyle="{StaticResource containerStyle}"
HeaderTemplate="{StaticResource GroupHeader}">
</GroupStyle>
</ComboBox.GroupStyle>
</ComboBox>
</Grid>
</Controls:MetroWindow>