Change ComboBoxitem color using xaml in WPF


You can reuse styles to customize appearance of components s in WPF . To change the color of Combo items ,you have to create a style for itemcontainer

This can be done as follows

ItemContainer Style

<Style x:Key="Combo+" TargetType="ComboBoxItem">
        <Setter Property="FocusVisualStyle" Value="{x:Null}" />
        <Setter Property="Background" Value="AliceBlue" />
        <Style.Triggers>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="Foreground" Value="Red"/>
                <Setter Property="Background" Value="GreenYellow"/>
            </Trigger>
        </Style.Triggers>
    </Style>

I have also another style for combo which set font, color etc . You can call the container style directly to Listbox or call from another style which is I am doing right here

Within another style

 <Style x:Key="Combo" TargetType="ComboBox">
        <Setter Property="FontSize" Value="18"/>
        <Setter Property="ItemContainerStyle" Value="{DynamicResource Combo+}"/>         
    </Style>

Within List [Direct way]

 <ComboBox ItemContainerStyle=""="{DynamicResource Combo+}" KeyUp="_TabPress"  TabIndex="10"   x:Name="lstb_base" Grid.Column="1" Grid.Row="10" Margin="0.2,31.6,0,2" Grid.RowSpan="2" Width="99" HorizontalAlignment="Left" >
 <ComboBox Style="{DynamicResource Combo}" KeyUp="_TabPress"  TabIndex="10"   x:Name="lstb_base" Grid.Column="1" Grid.Row="10" Margin="0.2,31.6,0,2" Grid.RowSpan="2" Width="99" HorizontalAlignment="Left" >

In the XAML

In my xaml I just calling the style using

   <ComboBox Style="{DynamicResource Combo}" KeyUp="_TabPress"  TabIndex="10"   x:Name="lstb_base" Grid.Column="1" Grid.Row="10" Margin="0.2,31.6,0,2" Grid.RowSpan="2" Width="99" HorizontalAlignment="Left" >

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.