ComboBox stops reacting when a DynamicResource template is assigned to it

Juan Dent 236 Reputation points
2022-08-06T20:03:19.997+00:00

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"  
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
        xmlns:local="clr-namespace:TemplatesStylesAndThemes"  
        mc:Ignorable="d"  
        Title="ComboBoxesResources" Height="450" Width="800">  
    <Grid>  
        <ComboBox x:Name="comboBoxMaritalStatus" Grid.Column="0" HorizontalAlignment="Left" Margin="12,6,0,0" Grid.Row="0"   
                  VerticalAlignment="Top" Width="105" Height="25">  
  
        </ComboBox>  
    </Grid>  
</Window>  
  

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"  
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
             xmlns:local="clr-namespace:TemplatesStylesAndThemes"  
             StartupUri="ComboBoxesResources.xaml">  
    <Application.Resources>  
        <ControlTemplate x:Key="XYComboBoxTemplate" TargetType="ComboBox" >  
            <Border Name="RootElement" >  
                <Border.Background>  
                    <SolidColorBrush x:Name="BorderBrush" Color="LightCyan"/>  
                </Border.Background>  
  
            </Border>  
            <ControlTemplate.Triggers>  
                <Trigger Property="IsDropDownOpen" Value="True" >  
                    <Setter Property="Background" Value="Red"/>  
                </Trigger>  
            </ControlTemplate.Triggers>  
        </ControlTemplate>  
    </Application.Resources>  
</Application>  
  

What am I doing wrong?

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,671 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
764 questions
0 comments No comments
{count} votes

Accepted answer
  1. Hui Liu-MSFT 38,251 Reputation points Microsoft Vendor
    2022-08-08T07:18:12.91+00:00

    Hi,@Juan Dent . 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.
    229004-image.png
    228938-image.png

    Style:
    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.

     <MultiDataTrigger>  
                                    <MultiDataTrigger.Conditions>  
                                        <Condition Binding="{Binding IsDropDownOpen, RelativeSource={RelativeSource AncestorType={x:Type ComboBox}}}" Value="true"/>  
                                    </MultiDataTrigger.Conditions>  
                                    <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}"/>  
                                </MultiDataTrigger>  
    

    The complete code:
    229015-5.txt

    The result:
    228979-image.png


    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.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful