user3478810
user3478810

Reputation: 45

Change comboboxitem style in runtime

i have a custom comboboxitem style

<Style x:Key="combo_item" TargetType="{x:Type ComboBoxItem}">

i need to add items to the combobox in run time (c# code) with this style i can add the items

ComboBoxItem tmp = new ComboBoxItem();
        tmp.Content = "data";
        combobox.Items.Add(tmp);

but i cant seem to figure out to to apply the style given that there is more than just this style so i cant do this

<Style x:Name="combo_item" TargetType="{x:Type ComboBoxItem}">

Upvotes: 0

Views: 853

Answers (2)

Mauricio Mora
Mauricio Mora

Reputation: 829

Did you try setting ItemContainerStyle property in the ComboBox to "combo_item"? Like this:

<Style x:Key="ComboBoxBaseStyle" TargetType="{x:Type ComboBox}">
    <Setter Property="ItemContainerStyle" Value="{StaticResource combo_Item}" />

or in code

yourComboBoxInstance.ItemContainerStyle = "combo_Item";

Upvotes: 0

J.H.
J.H.

Reputation: 4322

You need to find the style and then just set tmp.Style to it:

tmp.Style = this.FindResource("combo_item") as Style;

Upvotes: 2

Related Questions