Search code examples
c#wpfxamlcomboboxwpf-style

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}">

Solution

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

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