question

JuanDent-0991 avatar image
0 Votes"
JuanDent-0991 asked JuanDent-0991 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"
         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-wpfdotnet-wpf-xaml
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 JuanDent-0991 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.
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.



image.png (68.1 KiB)
image.png (11.0 KiB)
5.txt (26.5 KiB)
image.png (3.3 KiB)
· 2
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 ·