user1716674
user1716674

Reputation: 11

WPF ComboBox customization in windows8

I am trying to customize WPF ComboBox control on windows8. But looks like on win8 these customization doesn't have any effect on ComboBox, combobox has default look and feel.

e.g

< ComboBox Height="20" HorizontalAlignment="Left" Background="Red" BorderThickness="1" Name="comboBox1" VerticalAlignment="Top" Width="200" />

Here I am assiging Red background for combobox, but on win8 this doesn't have any effect.

Am I missing something here ?

Upvotes: 1

Views: 843

Answers (2)

Ajay
Ajay

Reputation: 17

I followed what Suny has suggested and still was not able to get Red background, and so I changed Background to Red in Popup and which helped me to get Red.

Upvotes: 0

Suny
Suny

Reputation: 1175

You may want to "Edit additional Template" to have full control on Combobox. It can be done by right clicking on the combobox(DesignView) in XAML (assuming you are using C# and XAML) and goto "Edit additional Template" and then goto to "Edit Genereated Item Container" and then goto "Edit a copy". This would create a style in "StandardStyles.xml"(by default) and then you can modify at your will. Worked for me. Hope it does for you as well.

In the style, look for Visual states and change the background color like this

                            <VisualState x:Name="Selected">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="InnerGrid">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="#333333"/>
                                    </ObjectAnimationUsingKeyFrames>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource ListBoxItemSelectedForegroundThemeBrush}"/>
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="SelectedUnfocused">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="InnerGrid">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="#333333"/>
                                    </ObjectAnimationUsingKeyFrames>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource ListBoxItemSelectedForegroundThemeBrush}"/>
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="SelectedDisabled">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="InnerGrid">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="#333333"/>
                                    </ObjectAnimationUsingKeyFrames>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource ListBoxItemSelectedDisabledForegroundThemeBrush}"/>
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="SelectedPointerOver">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="InnerGrid">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="#333333"/>
                                    </ObjectAnimationUsingKeyFrames>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource ListBoxItemSelectedForegroundThemeBrush}"/>
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="SelectedPressed">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="InnerGrid">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="#333333"/>
                                    </ObjectAnimationUsingKeyFrames>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource ListBoxItemSelectedForegroundThemeBrush}"/>
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>

#333333 is tha value i gave it.You can specify "Red" here. Should work.

Upvotes: 1

Related Questions