`

WPF - Setting foreground color of Entire window

阅读更多

You might as well as I would suppose that if we just set the foreground of a window to something different from its original color, then the whole controls from the window might change as well?
well this is not true, consider this example. 

<Window Foreground="Red">
   <Label Content="Test"/>
   <Label Content="Test"/>
   <CheckBox Content="Checkbox"/>
</Window>

 

 

the reason as pointed by Pavlo Glazkov is as such ...

Pavlo Glazkov 写道
This is because such controls as Label and CheckBox override the Foreground property in their styles.

 

Below is an example a typical logical tree of elements that shows how the value specified on the Window level travels down the tree:

 

Window (Red [Local]) 
  -> Grid (Red [Inherited]) 
     -> ListBox (Red [Inherited]) 
        -> ListBoxItem (Red [Inherited]) 
           -> StackPanel (Red [Inherited]) 
              -> Label (Black [Style])
                 -> TextBlock (Black [Inherited])
              -> TextBlock (Red [Inherited])

 

In square brackets the source of the value is shown.

As you can see the inheritance breaks on theLabelitself because it has theForegroundproperty set in its default style:

 

 

 

<Style x:Key="{x:Type Label}"
       TargetType="{x:Type Label}">
    <Setter Property="Foreground"
            Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
    ...
</Style>

 

 

 

As a workaround for this we can use the following trick. Define the default style for such controls (as Label) in the application (in App.xaml or in the Window inself). And in that default style override the Foreground property to set a relative source binding to the nearest ancestor of the control that still has the desired value:

 

 

<StyleTargetType="{x:Type Label}"><SetterProperty="Foreground"Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type FrameworkElement}}, Path=(TextElement.Foreground)}"/></Style><StyleTargetType="{x:Type CheckBox}"><SetterProperty="Foreground"Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type FrameworkElement}}, Path=(TextElement.Foreground)}"/></Style>

 

After that our tree will look like this:

 

 

 

Window(Red[Local])->Grid(Red[Inherited])->ListBox(Red[Inherited])->ListBoxItem(Red[Inherited])->StackPanel(Red[Inherited])->Label(Red[Binding to StackPanel.(TextElement.Foreground)])->TextBlock(Red[Inherited])->TextBlock(Red[Inherited])

 

 

As you can see, our binding restores the inheritance.

Such styles need to be defined for each element that overrides the Foregroundproperty in its style. As @Duane suggested, to not duplicate the binding in each style the BasedOn capability can be used:

 

 

 

<Style x:Key="ForegroundInheritanceFixStyle"TargetType="Control"><SetterProperty="Foreground"Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type FrameworkElement}}, Path=(TextElement.Foreground)}"/></Style><StyleTargetType="{x:Type Label}"BasedOn="{StaticResource ForegroundInheritanceFixStyle}"></Style><StyleTargetType="{x:Type CheckBox}"BasedOn="{StaticResource ForegroundInheritanceFixStyle}"></Style>

 

 

And because of this inheritance relationship, I found there is a problem when I tried to change the default settings for some data control, the change is not affected, it was because the container element is no modified for its fore/ground, background color.  

so, e.g.

 

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:dx="clr-namespace:DevExpress.Xpf.Core;assembly=DevExpress.Xpf.Core.v12.1"
    xmlns:vm="clr-namespace:RiskLayoutExample.ViewModels"
    xmlns:dxg="clr-namespace:DevExpress.Xpf.Grid;assembly=DevExpress.Xpf.Grid.v12.1"
    xmlns:dxdo="http://schemas.devexpress.com/winfx/2008/xaml/docking"
    >
  <DataTemplate DataType="{x:Type vm:RiskDataViewModel}">
    <dx:BackgroundPanel>
      <DataGrid
          HorizontalAlignment="Stretch"
          VerticalAlignment="Stretch"
          ItemsSource="{Binding RiskData}"
                  >
        <DataGrid.Resources>
                      <Style TargetType="TextBlock"  x:Key="{x:Type TextBlock}" >
                      </Style>
                      <Style TargetType="TextBoxBase" x:Key="{x:Type TextBoxBase}" 
                             >
                      </Style>
                      <Style TargetType="TextBox"  x:Key="{x:Type TextBox}" />
                      <Style TargetType="DataGridCell" x:Key="{x:Type DataGridCell}">
                      </Style>
                      <!-- .. -->
        </DataGrid.Resources>

        <DataGrid.Columns>
          <DataGridTextColumn
              Header="Book"
              Binding="{Binding Book}"
              IsReadOnly="True"
                          >
          </DataGridTextColumn>
          <!-- ... -->
        </DataGrid.Columns>
      </DataGrid>
    </dx:BackgroundPanel>
  </DataTemplate>
</ResourceDictionary>

 

 

the text color of the DataGridCell does not change, so I hvae to change the foreground and background of the dxd:BackgroundPanel

So I have to supply the color of the containing element. 

    <dx:BackgroundPanel
      Foreground="White"
      Background="Black">
   </dx:BackgroundPanel>

 

 

References:

Setting Foreground Color of Entire Window

分享到:
评论

相关推荐

    gong-wpf-dragdrop, GongSolutions.WPF.DragDrop 库是WPF的拖动'n'拖放框架.zip

    gong-wpf-dragdrop, GongSolutions.WPF.DragDrop 库是WPF的拖动'n'拖放框架 简介GongSolutions.WPF.DragDrop 库是一个易于使用的拖拉'n'拖放框架。特性使用 MVVM: 拖放逻辑可以放在ViewModel中。 代码不需要放在in中...

    通用WPF主题控件包rubyer-wpf-master

    通用WPF主题控件包rubyer-wpf-master是一款专为Windows Presentation Foundation (WPF) 应用程序设计的开源UI框架。它提供了丰富的主题和控件,旨在帮助开发者快速构建美观且用户友好的应用程序界面。在2.0.0版本中...

    C#开发WPF-Silverlight动画及游戏系列教程-深蓝色右手 4

    C#开发WPF-Silverlight动画及游戏系列教程-深蓝色右手 C#开发WPF-Silverlight动画及游戏系列教程-深蓝色右手 C#开发WPF-Silverlight动画及游戏系列教程-深蓝色右手

    WPF-Samples-master_WPF基本sample_

    WPF的基本空间历程,使用.net core3.0.1版本

    基于WPF的图形化编程控件和环境WPF-Blockly-master

    【标题】"基于WPF的图形化编程控件和环境WPF-Blockly-master" 提供了一个创新的编程体验,它将传统的代码编写转变为图形化的流程图形式,使得编程变得更加直观和易于理解。WPF(Windows Presentation Foundation)是...

    bootstrap-wpf-style-master 样式

    Bootstrap-WPF 样式是一种将流行的前端框架 Bootstrap 的设计风格应用于 WPF(Windows Presentation Foundation)应用程序的方法。Bootstrap 是一个广泛使用的开源工具包,主要用于构建响应式、移动设备优先的网页...

    AI-wpf-controls一个Wpf控件库

    在本文中,我们将深入探讨"AI-wpf-controls",这是一个专为Windows Presentation Foundation(WPF)框架设计的控件库。这个独特的库整合了多个知名控件库的优点,包括MahApps.Metro、Material-Design、HandyControl...

    WPF-Diagram-Designer:WPF图表设计器源代码

    通过深入研究WPF-Diagram-Designer的源代码(如WPF-Diagram-Designer-master文件夹中的内容),开发者不仅可以学习到如何在WPF中构建复杂的图形界面,还可以了解到图形编辑器的设计原理和实现细节,对于提升图形应用...

    WPF-MaterialDesign-master.zip_WPF_WPF非常好的界面_包括多种漂亮的皮肤_漂亮的控件_配色

    在本项目"WPF-MaterialDesign-master.zip"中,重点在于利用**Material Design**这一设计语言来增强WPF应用的视觉效果。Material Design是Google推出的一种设计规范,其灵感来源于现实世界中的纸张和墨水,强调层次感...

    Wpf-glTF-testing.zip

    在“Wpf-glTF-testing.zip”压缩包中,我们有一个基于WPF(Windows Presentation Foundation)的简化glTF文件查看器项目。 WPF是.NET Framework的一部分,是一个用于构建Windows桌面应用程序的框架。它提供了丰富的...

    wpf---StatusBar

    “wpf---StatusBar”这个标题表明我们将探讨的是WPF(Windows Presentation Foundation)框架中的StatusBar组件。WPF是.NET Framework的一部分,用于构建桌面应用程序,它提供了丰富的用户界面(UI)功能。StatusBar...

    WPF-强大的图表.zip

    **WPF - 强大的图表技术** Windows Presentation Foundation (WPF) 是Microsoft开发的一个用于构建桌面应用程序的框架,它提供了丰富的图形系统,包括对2D和3D图形的强大支持。在WPF中,开发人员可以利用各种图表...

    wpf-datagrid-access DB

    在这个“wpf-datagrid-access DB”主题中,我们将深入探讨如何利用WPF Datagrid与Microsoft Access数据库进行交互,实现数据的读取、更新和保存。 1. **WPF Datagrid简介** - Datagrid是WPF中的一个数据展示控件,...

    C#开发WPF-Silverlight动画及游戏系列教程-深蓝色右手1

    C#开发WPF-Silverlight动画及游戏系列教程-深蓝色右手 C#开发WPF-Silverlight动画及游戏系列教程-深蓝色右手 C#开发WPF-Silverlight动画及游戏系列教程-深蓝色右手

    WPF-ScreenData.zip

    超酷WPF数据可视化,全套源程序,非常适合做数据可视化的程序员使用,WPF,XAML。 找了好久的资源,附有很多很详细的插图,是大数据时代不可缺少的可视化学习数据,仅供大家学习使用请勿用做商业用途。与大家一起...

    WPF-进阶视频教程(共113集)-045Window类.mp4

    WPF-进阶视频教程(共113集)-045Window类.mp4

    WPF--Effect特效

    标题中的"WPF--Effect特效"指的是Windows Presentation Foundation(WPF)中的Effect特效。WPF是.NET Framework的一个重要组件,用于构建丰富的桌面应用程序。Effect特效在WPF中是用来改变UI元素外观的一...

    Prism-Samples-Wpf-master11-15.zip

    Prism-Samples-Wpf-master11-15的VS2017版本实现,下载手动重新安装一下nuget包即可,方便大家学习

    WPF-MvvmLight

    **WPF-MvvmLight** 是一个面向初学者的教程资源,旨在帮助他们理解并实践如何在Windows Presentation Foundation(WPF)应用中使用MvvmLight框架。MvvmLight,全称Model-View-ViewModel Light,是由GalaSoft开发的一...

    practical-wpf-charts-graphics-master.rar

    该资源"practical-wpf-charts-graphics-master.rar"包含了这本书的源代码,为读者提供了丰富的实践案例和深入理解WPF图表及图形编程的宝贵材料。 WPF(Windows Presentation Foundation)是.NET Framework的一部分...

Global site tag (gtag.js) - Google Analytics