JuanDent-0991 avatar image
0 Votes"
JuanDent-0991 asked HuiLiu-MSFT commented

ComboBox stops reacting when a DynamicResource template is assigned to it

I have a simple combobox that works fine except when i assign a template to it.
The original combobox code is:

 <Window x:Class="TemplatesStylesAndThemes.ComboBoxesResources"
         Title="ComboBoxesResources" Height="450" Width="800">
         <ComboBox x:Name="comboBoxMaritalStatus" Grid.Column="0" HorizontalAlignment="Left" Margin="12,6,0,0" Grid.Row="0" 
                   VerticalAlignment="Top" Width="105" Height="25">

this works fine. If I add a template to it, the functionality of the combobox disappears ... That is when the ComboBox becomes:

 <ComboBox x:Name="comboBoxMaritalStatus" Template="{DynamicResource XYComboBoxTemplate}" HorizontalAlignment="Left" Margin="12,6,0,0"  
                   VerticalAlignment="Top" Width="105" Height="25">

the combobox does not react to mouse events at all - it just displays a rectangle with no "life"

The template is an Application.Resource and is defined in App.xaml as:

 <Application x:Class="TemplatesStylesAndThemes.App"
         <ControlTemplate x:Key="XYComboBoxTemplate" TargetType="ComboBox" >
             <Border Name="RootElement" >
                     <SolidColorBrush x:Name="BorderBrush" Color="LightCyan"/>
                 <Trigger Property="IsDropDownOpen" Value="True" >
                     <Setter Property="Background" Value="Red"/>

What am I doing wrong?

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

HuiLiu-MSFT avatar image
1 Vote"
HuiLiu-MSFT answered HuiLiu-MSFT commented

Hi,@JuanDent-0991. Welcome Microsoft Q&A.

To modify the background color of the ComboBox as needed, it is necessary to modify the ControlTemplate of the ComboBox .
You could refer to the following code to modify and use the default template of Combobox.

Get the default template of Combobox in xaml.

Find and modify the value of ComboBox.Static.Background in the newly created resource.

     <SolidColorBrush x:Key="ComboBox.Static.Background" Color="LightCyan"/>

Add the following code in the ControlTemplate.Triggers of the ComboBoxToggleButton style.

                                     <Condition Binding="{Binding IsDropDownOpen, RelativeSource={RelativeSource AncestorType={x:Type ComboBox}}}" Value="true"/>
                                 <Setter Property="Background" TargetName="templateRoot" Value="red"/>
                                 <Setter Property="BorderBrush" TargetName="templateRoot" Value="{StaticResource ComboBox.MouseOver.Editable.Border}"/>
                                 <Setter Property="Background" TargetName="splitBorder" Value="red"/>
                                 <Setter Property="BorderBrush" TargetName="splitBorder" Value="{StaticResource ComboBox.MouseOver.Editable.Button.Border}"/>

The complete code:

The result:

If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

image.png (68.1 KiB)
image.png (11.0 KiB)
5.txt (26.5 KiB)
image.png (3.3 KiB)
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi,@JuanDent-0991. Is there any update to the question? Did my answer solve your problem? If so, you could accept it as the answer. It's helpful for community members with related questions.

0 Votes 0 ·

If so, you could accept it as the answer

Where do i do this? I see no button!!

0 Votes 0 ·

Hi,@JuanDent-0991. You could click the " ✔ " next to an answer to accept it as an answer.

0 Votes 0 ·