0
我试图将标签存储在ComboBox中,并带有类别 - 类似this。显示控件时出现问题
但是,组合框项目出现,像这样:
这是我迄今所做的:
的App.xaml
<DataTemplate x:Key="groupStyle">
<Label FontWeight="Bold" Content="{Binding Name}"/>
</DataTemplate>
代码背后
ComboBox comboBox1 = new ComboBox();
GroupStyle style = new GroupStyle();
style.HeaderTemplate = (DataTemplate)this.FindResource("groupStyle");
comboBox1.GroupStyle.Add(style);
comboBox1.DisplayMemberPath = "Item";
ObservableCollection<CategoryItem<Label>> items = new ObservableCollection<CategoryItem<string>>();
Label label = new Label();
TextBlock block = new TextBlock();
block.Text = "Text";
label.Content = block;
items.Add(new CategoryItem<Label> { Category = "Category", Item = label });
CollectionViewSource cvs = new CollectionViewSource();
cvs.GroupDescriptions.Add(new PropertyGroupDescription("Category"));
cvs.Source = items;
Binding b = new Binding();
b.Source = cvs;
BindingOperations.SetBinding(comboBox1, ComboBox.ItemsSourceProperty, b);
public class CategoryItem<T>
{
public T Item { get; set; }
public string Category { get;
}
太感谢你了,它的工作原理:) – 2013-03-24 14:31:16
唯一的问题现在似乎当我将鼠标放在标签上时,它们周围没有蓝色边框,就像悬停在ComboBoxItem – 2013-03-24 14:32:30
上我想你应该定义一些在ControlTemplate中触发,但在这种情况下我不够好。我编辑了我的帖子,你可以从我提供的链接中获得一些想法。 – 2013-03-24 14:41:14