您可以使用模板來獲取看看你是後restyle一個ComboBox。
下面是一個簡單的例子:
XAML
<ComboBox Width="200" Height="30" ItemsSource="{Binding ItemList}">
<ComboBox.ItemContainerStyle>
<Style>
<Setter Property="UIElement.IsEnabled" Value="{Binding IsSelectable}" />
</Style>
</ComboBox.ItemContainerStyle>
<ComboBox.ItemTemplate>
<DataTemplate >
<StackPanel Orientation="Horizontal">
<Image Name="ImageControl" Source="{Binding ImagePath}" Width="10" Height="10" />
<Label Content="{Binding Name}" >
<Label.Style>
<Style TargetType="Label">
<Style.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Foreground" Value="Black" />
<Setter Property="FontWeight" Value="Bold" />
</Trigger>
</Style.Triggers>
</Style>
</Label.Style>
</Label>
</StackPanel>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding IsSelectable}" Value="False">
<Setter TargetName="ImageControl" Property="Visibility" Value="Collapsed" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
C#
// A class to hold my data for the combobox
public class Item
{
public string Name { get; set; }
public bool IsSelectable { get; set; }
public string ImagePath { get; set; }
}
// In your datacontext
public ObservableCollection<Item> ItemList { get; set; }
public ComboBoxFun()
{
ItemList = new ObservableCollection<Item>()
{
new Item() { [email protected]"/images/up.png", Name="Item Group 1", IsSelectable=false},
new Item() { [email protected]"/images/up.png", Name="Item 1", IsSelectable=true},
new Item() { [email protected]"/images/up.png", Name="Item 2", IsSelectable=true},
new Item() { [email protected]"/images/up.png", Name="Item 3", IsSelectable=true},
new Item() { [email protected]"/images/up.png", Name="Item Group 2", IsSelectable=false},
new Item() { [email protected]"/images/up.png", Name="Item 1", IsSelectable=true},
new Item() { [email protected]"/images/up.png", Name="Item 2", IsSelectable=true},
new Item() { [email protected]"/images/up.png", Name="Item 3", IsSelectable=true}
};
唯一棘手的部分是,當你關閉一個項目將被造型爲禁用,所以你必須設置不可選項目的樣式,以使它們顯得正常。
這裏是我的結果:
您可以選擇任何非組項目,它像一個正常的組合框。 至於你的按鈕,你可以根據選定的項目數據簡單地控制它們的可見性。
希望這會有所幫助。
爲了實現分組,你可以額外的屬性添加到您的項目,指示組級別和使用數據觸發衝擊姓名標籤的邊緣。 – Duane
分離器怎麼樣? – jur
我可能會用另一個StackPanel包裝ItemTemplate,並在當前內容後面引出一行,然後使用現有的觸發器來確定它的可見性。或者可以用邊框包裹ItemTemplate的當前內容,並用觸發器控制BorderThickness(即「0,0,0,0」未觸發和「0,0,0,1」觸發)。有很多不同的方式來做到這一點。 –