Jason Rae
Jason Rae

Reputation: 2663

Style background color of ComboBox to match selected item background color

I have a combobox that has items with different background colors. When I select a colored item, the background of the combobox remains the default color. How can I bind the background of the combo box to change based on the background color of the selected item?

    <ComboBox.Style>
       <Style TargetType="ComboBox">
         <Style.Setters>
           <Setter Property="Background" Value="????"/>
         </Style.Setters>
       </Style>
   </ComboBox.Style>
   <ComboBox.ItemContainerStyle>
     <Style TargetType="ComboBoxItem">
       <Style.Setters>
         <Setter Property="Background" Value="{Binding Path=Color}"/>
       </Style.Setters>
     </Style>
   </ComboBox.ItemContainerStyle>

Upvotes: 0

Views: 5055

Answers (1)

Heena
Heena

Reputation: 8634

Try this..sorry if i am wrong to your requirement.

    <ComboBox Height="25" Width="200" >         
        <ComboBox.ItemContainerStyle>
            <Style TargetType="ComboBoxItem">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="ComboBoxItem">
                            <Border x:Name="Border" Background="{TemplateBinding Background }">
                                <TextBlock FontSize="25" FontFamily="Segoe UI Light">
                                            <ContentPresenter></ContentPresenter>
                                </TextBlock>
                            </Border>
                            <ControlTemplate.Triggers>
                                <Trigger Property="ComboBoxItem.IsSelected" Value="True">
                                    <Setter TargetName="Border" Property="Background" Value="{Binding Path=Background, RelativeSource={RelativeSource TemplatedParent}}"></Setter>
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </ComboBox.ItemContainerStyle>
        <ComboBoxItem Height="30" Background="Red">czczczc</ComboBoxItem>
        <ComboBoxItem Height="30"  Background="Green">czczczc</ComboBoxItem>
        <ComboBoxItem Height="30"  Background="Blue">czczczc</ComboBoxItem>
    </ComboBox>

and in case if you want change combobox dropdown then you can define its background like this

 Background="{Binding RelativeSource={RelativeSource Self}, Path=SelectedItem.Background}"

Upvotes: 1

Related Questions