zoukankan      html  css  js  c++  java
  • 为WPF中的ContentControl设置背景色

    本文背景:在使用Prism框架的WPF中,直接为ContentControl设置Background不起作用。

    参考资料:ContentControl in ControlTemplate does not show Border nor Background

    1、问题原因

    ContentControl默认的Style不包含渲染Border和Background属性的方法,它仅仅含有一个ContentPresenter对象。

    2、解决方法

    为了给ContentControl添加背景色,需要为其添加包含所需元素的自定义模板,如下所示:

    <ContentControl regions:RegionManager.RegionName="ContentRegion" Margin="{Binding ContentMargin}"
                    HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
        <ContentControl.Template>
            <ControlTemplate TargetType="ContentControl">
                <Border Background="{Binding ContentBrush}">
                    <ContentPresenter Content="{TemplateBinding Content}"
                                      ContentTemplate="{TemplateBinding ContentTemplate}"
                                      Cursor="{TemplateBinding Cursor}"
                                      HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
                </Border>
            </ControlTemplate>
        </ContentControl.Template>
    </ContentControl>

    3、问题总结

    在上述代码中,为ContentControl的模板添加了Border,并为Border设置了Background,然后在Border中再添加ContentPresenter对象,这样便使得ContentPresenter渲染的控件能够具有指定的背景了,前面的问题得到解决。

  • 相关阅读:
    布局
    JS基础回顾_滚动条
    JS基础回顾_Dom
    JS语法_其他
    JS语法_类型
    一些免费的API
    CSS特效(一)
    博客园在Markdown中使用JS
    C# 聊一聊屏保的设置 第一步 注册表
    2019 力扣杯-全国高校春季编程大赛 最长重复子串
  • 原文地址:https://www.cnblogs.com/xhubobo/p/15398930.html
Copyright © 2011-2022 走看看