ContentControl BackGround,BorderBrush Wont be Changed in Uwp

ContentControl contentControl = new ContentControl();
StackPanel stackPanel = new StackPanel();
stackPanel.Height = 50;
stackPanel.Width = 100;
//stackPanel.Background = new SolidColorBrush(Colors.Green);
contentControl.Background = new SolidColorBrush(Colors.Red);
contentControl.BorderBrush = new SolidColorBrush(Colors.BlanchedAlmond);
contentControl.BorderThickness = new Thickness(5);
contentControl.Content = stackPanel;

When i set background for stackpanel it works,but it doesnt work for contentcontrol.. i need contentcontrol background.!!

2 answers

  • answered 2020-03-31 09:00 Joey

    What does the control template look like for your ContentControl? My guess would be that it's just a single ContentPresenter and nothing else. Especially nothing that has a TemplateBinding to the properties you're trying to change. Control properties don't magically have an effect, unless the control's template incorporates them.

    Something like the following control template would probably work in your case:

    <ControlTemplate TargetType="ContentControl">
      <Border BorderBrush="{TemplateBinding BorderBrush}"
              BorderThickness="{TemplateBinding BorderThickness}"
              Background="{TemplateBinding Background}">
        <ContentPresenter ContentTemplate="{TemplateBinding ContentTemplate}"
                          Content="{TemplateBinding Content}"
                          Margin="{TemplateBinding Padding}"/>
      </Border>
    </ControlTemplate>
    

  • answered 2020-04-01 00:18 Richard Zhang - MSFT

    As @Joey said, ContentControl internally relies on ContentPresenter to work, but the Background property set in ContentControl is not passed to the internal ContentPresenter. So we need to override the default style.

    Add these code to App.xaml:

    <Application.Resources>
        <Style TargetType="ContentControl">
            <Setter Property="HorizontalContentAlignment" Value="Left" />
            <Setter Property="VerticalContentAlignment" Value="Top" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ContentControl">
                        <ContentPresenter Content="{TemplateBinding Content}"
                                          ContentTemplate="{TemplateBinding ContentTemplate}"
                                          Margin="{TemplateBinding Padding}"
                                          Background="{TemplateBinding Background}"
                                          ContentTransitions="{TemplateBinding ContentTransitions}"
                                          HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                          VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Application.Resources>
    

    After that you can set the background color for ContentControl.

    Thanks.