我所做的是左图所示的效果,我有一个包含两组数据的组合框,我想要的是当我在组合框中选择一个项目时,显示的文本将看起来像右图所示(添加一些选择项前的前缀),例如我在A组中选择了项目' 00 ' ,然后显示的文本将是' A_00 ',如何根据我的代码实现这个效果?
在 GroupComboBoxResDic.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:helper="clr-namespace:WpfApplication3">
<!-- Main style for ComboBox -->
<Style x:Key="MyComboBox" TargetType="{x:Type ComboBox}">
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<WrapPanel IsItemsHost="True" Orientation="Horizontal" Width="150" Height="Auto" />
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
<Setter Property="ItemTemplate">
<Setter.Value>
<DataTemplate>
<TextBlock Text="{Binding Item, Converter={StaticResource terminalToDisplayNameConverter}, Mode=OneWay}" Width="40"/>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
<!-- Style for ComboBoxItem -->
<Style TargetType="{x:Type ComboBoxItem}">
<Setter Property="Width" Value="50" />
</Style>
<!-- Style for ItemContainerStyle -->
<Style x:Key="ComboBoxItemContainerStyle" TargetType="{x:Type ComboBoxItem}">
<Setter Property="IsEnabled" Value="{Binding Available}" />
</Style>
<!-- DataTemplate for HeaderTemplate -->
<DataTemplate x:Key="MyHeaderTemplate">
<Border BorderBrush="Black" BorderThickness="2">
<TextBlock Text="{Binding Name}" HorizontalAlignment="Stretch" Background="YellowGreen" />
</Border>
</DataTemplate>
</ResourceDictionary>
XAML 文件
<Window x:Class="WpfApplication3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" xmlns:my="clr-namespace:WpfApplication3">
<Window.Resources>
<ResourceDictionary Source="GroupComboBoxResDic.xaml" />
</Window.Resources>
<Grid>
<ComboBox x:Name="MyComboBox1"
Style="{StaticResource MyComboBox}"
IsSynchronizedWithCurrentItem="False"
ItemContainerStyle="{StaticResource ComboBoxItemContainerStyle}"
ItemsSource="{Binding InputsList}" Margin="293,50,104,234">
<ComboBox.GroupStyle>
<GroupStyle HeaderTemplate="{StaticResource MyHeaderTemplate}" />
</ComboBox.GroupStyle>
</ComboBox>
</Grid>
</Window>
代码隐藏文件
public enum CategoryGroup
{
GroupA = 0,
GroupB,
}
public class CategoryItemString
{
public string Item { get; set; }
public bool Available { get; set; }
public CategoryGroup Group { get; set; }
public string Category
{
get
{
switch (Group)
{
case CategoryGroup.GroupA:
return "Group A";
case CategoryGroup.GroupB:
return "Group B";
}
return string.Empty;
}
}
public override bool Equals(object obj)
{
if (obj == null) return false;
CategoryItemString rht = obj as CategoryItemString;
if (rht == null) return false;
return rht.Item.Equals(this.Item) &&
rht.Group == this.Group;
}
}
public class VM : INotifyPropertyChanged
{
private ListCollectionView lcv;
public VM()
{
List<CategoryItemString> items = new List<CategoryItemString>();
for (int i = 0; i < 18; ++i)
{
items.Add(new CategoryItemString { Item = string.Format("{0:D2}", i), Available = (true), Group = CategoryGroup.GroupA });
}
for (int i = 0; i < 4; ++i)
{
items.Add(new CategoryItemString { Item = string.Format("{0:D2}", i), Available = (true), Group = CategoryGroup.GroupB });
}
//Need the list to be ordered by the category or you might get repeating categories
lcv = new ListCollectionView(items.OrderBy(w => w.Category).ToList());
//Create a group description
lcv.GroupDescriptions.Add(new PropertyGroupDescription("Category"));
}
public ListCollectionView InputsList
{
get { return lcv; }
set
{
if (lcv != value)
{
lcv = value;
OnPropertyChanged("InputsList");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string prop)
{
if (this.PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(prop));
}
}
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
VM vm = new VM();
this.MyComboBox1.DataContext = vm;
}
}