如何向数据绑定的 WPF 组合框添加分隔符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18154167/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:24:53  来源:igfitidea点击:

How to add a separator to a WPF combobox that is databound?

c#wpfbindingcomboboxseparator

提问by Theodosius Von Richthofen

Is there any way I can add a seprator into a WPF combobox that is databound? ie in my xaml, the combobox's ItemsSource="{Binding TheList}". The list is an observable collection of objects, one of i want separated from the rest. This list is also generated from sql, so its not hard-coded or anything. I wouldn't want the seaprator to be selectable, either. Thanks!

有什么方法可以将分隔符添加到数据绑定的 WPF 组合框中?即在我的 xaml 中,组合框的 ItemsSource="{Binding TheList}"。该列表是一个可观察的对象集合,我想将其中一个与其他对象分开。这个列表也是从 sql 生成的,所以它不是硬编码或任何东西。我也不希望分离器是可选的。谢谢!

回答by bouvierr

Check thissolution. It uses a Styleto change the Templateof certain ComboBoxItemobjects

检查解决方案。它使用 aStyle来改变Template某些ComboBoxItem对象的

回答by rodolfoprado

you need to use a ComboBox.ItemTemplate to draw your itens

您需要使用 ComboBox.ItemTemplate 来绘制您的项目

http://www.silverlightshow.net/items/Silverlight-ComboBox.aspx

http://www.silverlightshow.net/items/Silverlight-ComboBox.aspx

回答by nrod

Although my ComboxBox(at the moment) is not DataBoundI achieved the concept of a separator by adding a bottom border to an element. In this example two lines before and two lines after the separator.

虽然我的ComboxBox目前)不是DataBound我通过向元素添加底部边框来实现分隔符的概念。在此示例中,分隔符前两行和分隔符后两行。

<ComboBox x:Name="Cbx" SelectionChanged="Cbx_SelectionChanged">
  <ComboBoxItem Content="select one..." Foreground="DarkGray" IsSelected="True" /
  <ComboBoxItem Content="ABC" />
  <ComboBoxItem Content="DEF" />
  <ComboBoxItem Content="GHI" BorderBrush="Black" BorderThickness="0,0,0,2" />
  <ComboBoxItem Content="KLM" />
  <ComboBoxItem Content="NOP" />
</ComboBox>

回答by TorATB

If you want to do the same in C# code:

如果你想在 C# 代码中做同样的事情:

Cbx.Items.Add("ABC");
Cbx.Items.Add("DEF");
ComboBoxItem item = new ComboBoxItem();
item.Content = "GHI";
item.BorderBrush = Brushes.Black;
item.BorderThickness = new Thickness(0, 0, 0, 2);
Cbx.Items.Add(item);
Cbx.Items.Add("KLM");
Cbx.Items.Add("NOP");