Anders
Anders

Reputation: 12560

Add a StackPanel to a ComboBoxItem Programmatically

I have a windows program built on WPF. I am making it more customizable for the end user by utilizing an ini file. Currently I have a ComboBox with several items, and a StackPanel between sections of the ComboBox:

enter image description here

Here is the XAML:

<ComboBox Grid.ColumnSpan="2" Grid.Column="0" HorizontalAlignment="Stretch" Margin="75,0,5,5" Grid.Row="1" VerticalAlignment="Top">
    <ComboBoxItem IsEnabled="False">
        <StackPanel>
            <TextBlock Text="50% Margin" />
            <Rectangle />
        </StackPanel>
    </ComboBoxItem>
    <ComboBoxItem Content="Windows" Uid="0" />
    <ComboBoxItem Content="Gutters" Uid="0" />
    <ComboBoxItem Content="Insulation" Uid="0" />
    <ComboBoxItem Content="Doors" Uid="0" />
    <ComboBoxItem IsEnabled="False">
        <StackPanel>
            <TextBlock Text="50% Margin, No Overage" />
            <Rectangle />
        </StackPanel>
    </ComboBoxItem>
    <ComboBoxItem Content="Kitchens" Uid="1" />
    <ComboBoxItem Content="Bathrooms" Uid="1" />
    <ComboBoxItem Content="Additions" Uid="1" />
    <ComboBoxItem Content="Basements" Uid="1" />
    <ComboBoxItem IsEnabled="False">
        <StackPanel>
            <TextBlock Text="35% Margin" />
            <Rectangle />
        </StackPanel>
    </ComboBoxItem>
    <ComboBoxItem Content="Siding" Uid="2" />
    <ComboBoxItem Content="Roofing" Uid="2" />
</ComboBox>

How do I add the StackPanel inside of the ComboBox item programatically? Unfortunately for me, ComboBoxItem does not have a children property which would make this really easy.

Upvotes: 2

Views: 782

Answers (1)

Pragmateek
Pragmateek

Reputation: 13374

ComboBoxItem has a Content property that should do what you want.

In general if you want to know to which property the XAML content of an element is mapped to, walk up the inheritance hierarchy and look for the [ContentPropertyAttribute] attribute.

ComboBoxItem inherits from ListBoxItem which itself inherits from ContentControl which is "tagged" with [ContentPropertyAttribute("Content")]. :)

Upvotes: 2

Related Questions