【问题标题】:ContentControl BackGround,BorderBrush Wont be Changed in UwpContentControl 背景,BorderBrush 不会在 Uwp 中更改
【发布时间】:2020-04-01 08:18:59
【问题描述】:
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;

当我为 stackpanel 设置背景时,它可以工作,但它不适用于 contentcontrol.. 我需要内容控制背景。!!

【问题讨论】:

    标签: c# uwp


    【解决方案1】:

    正如@Joey 所说,ContentControl 内部依赖ContentPresenter 工作,但ContentControl 中设置的背景属性不会传递给内部ContentPresenter。所以我们需要覆盖默认样式。

    将这些代码添加到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>
    

    之后你可以为ContentControl设置背景颜色。

    谢谢。

    【讨论】:

    • 谢谢;我无法详细说明,因为我从未使用过 UWP,并且或多或少希望它与 WPF 一样工作。
    【解决方案2】:

    ContentControl 的控制模板是什么样的?我的猜测是它只是一个ContentPresenter,没有别的。尤其是没有TemplateBinding 与您要更改的属性有关的内容。控件属性不会神奇地产生效果,除非控件的模板包含它们。

    以下控件模板可能适用于您的情况:

    <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>
    

    【讨论】:

    • 看不懂,能简单解释一下吗?
    • @PremKumarShanmugam:这是相当基本的知识,WPF 和 UWP 中的模板和绑定是如何工作的。这很难简单地描述,因为它是一个相当重要和重要的概念。您可以阅读简介here。而且我相当肯定 ContentControl 的默认控件模板只是一个 ContentPresenter,没有可能具有模板绑定到 Background 或 BorderBrush 属性的边框。
    猜你喜欢
    • 2016-06-04
    • 1970-01-01
    • 1970-01-01
    • 2017-12-29
    • 2021-02-11
    • 2021-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多