> 文章列表 > WPF入门教程(六)--依赖属性(2)--属性值优先级与继承

WPF入门教程(六)--依赖属性(2)--属性值优先级与继承

WPF入门教程(六)--依赖属性(2)--属性值优先级与继承

一、 依赖属性优先级

由于WPF 允许我们可以在多个地方设置依赖属性的值,所以我们就必须要用一个标准来保证值的优先级别。比如下面的例子中,我们在三个地方设置了按钮的背景颜色,那么哪一个设置才会是最终的结果呢?是Black、Red还是Azure呢?


<Window x:Class="WpfApp1.WindowDepend"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"Title="WindowDepend" Height="400" Width="400"><Grid><Button x:Name="myButton" Background="Azure"><Button.Style><Style TargetType="{x:Type Button}"><Setter Property="Background" Value="Black"/><Style.Triggers><Trigger Property="IsMouseOver" Value="True"><Setter Property="Background" Value="Red" /></Trigger></Style.Triggers></Style></Button.Style>Click</Button></Grid>
</Window>

通过前面的简单介绍,我们了解了简单的依赖属性,每次访问一个依赖属性,它内部会按照下面的顺序由高到底处理该值。详细见下图

这里写图片描述

上图的流程图偏理想化

二、 依赖属性的继承

属性值继承是WPF属性系统的一项功能。 属性值继承使元素树中的子元素可以从父元素那里获取特定属性的值,并继承该值,就好像它是在最近的父元素中的任意位置设置的一样。 父元素还可以通过属性值继承来获得其值,因此系统有可能一直递归到页面根元素。 属性值继承不是属性系统的默认行为;属性必须用特定的元数据设置来建立,以便使该属性能够对子元素启动属性值继承。

依赖属性继承的最初意愿是父元素的相关设置会自动传递给所有层次的子元素 ,即元素可以从其在树中的父级继承依赖项属性的值。这个我们在编程当中接触得比较多,如当我们修改窗体父容器控件的字体设置时,所有级别的子控件都将自动 使用该字体设置 (前提是该子控件未做自定义设置)。接下来,我们来做一个实际的例子。代码如下:

<Window x:Class="WpfApp1.WindowInherited"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"Title="WindowInherited" Height="400" Width="500" Loaded="Window_Loaded" ><Grid><Grid.RowDefinitions><RowDefinition Height="101*"/><RowDefinition Height="80"/><RowDefinition Height="80"/></Grid.RowDefinitions><StackPanel Grid.Row="0" ><Label Content="继承自Window的FontSize" /><TextBlock Name="textBlockInherited" Text="重写了继承,没有继承Window的 FontSize"FontSize="36" TextWrapping="WrapWithOverflow"/><StatusBar>没有继承自Window的FontSize,Statusbar</StatusBar></StackPanel><WrapPanel Grid.Row="1"><Label Content="窗体字体大小" /><ComboBox Name="drpWinFontSize"></ComboBox><Button Name="btnFontSize" Click="btnFontSize_Click">改变window字体</Button></WrapPanel><WrapPanel Grid.Row="2"><Label Content="文本字体大小" /><ComboBox Name="drpTxtFontSize"></ComboBox><Button Name="btnTextBlock" Click="btnTextBlock_Click">改变TextBlock字体 </Button></WrapPanel></Grid>
</Window>
using System;
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows; 
using System.Windows.Controls;
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media;
using System.Windows.Media.Imaging; 
using System.Windows.Shapes; 
namespace WpfApp1 
{/// <summary>/// WindowInherited.xaml 的交互逻辑/// </summary>public partial class WindowInherited : Window{public WindowInherited(){InitializeComponent();}private void btnFontSize_Click(object sender, RoutedEventArgs e){this.FontSize =Convert.ToInt32(drpWinFontSize.Text);}private void btnTextBlock_Click(object sender, RoutedEventArgs e){this.textBlockInherited.FontSize = Convert.ToInt32(drpTxtFontSize.Text);}private void Window_Loaded(object sender, RoutedEventArgs e){List<int> listFontSize = new List<int>();for (int i = 0; i <= 60; i++){listFontSize.Add(i + 4);}drpTxtFontSize.ItemsSource = listFontSize;drpWinFontSize.ItemsSource = listFontSize;}}
}

这里写图片描述

Window.FontSize 设置会影响所有的内部元素字体大小,这就是所谓的属性值继承,如上面代码中的第一个Label没有定义FontSize ,所以它继承了Window.FontSize的值。但一旦子元素提供了显式设置,这种继承就会被打断,如第二个TextBlock定义了自己的 FontSize,所以这个时候继承的值就不会再起作用了。
  这个时候你会发现一个很奇怪的问题:虽然StatusBar没有重写FontSize,同时它也是Window的子元素,但是它的字体大小却没 有变化,保持了系统默认值。那这是什么原因呢?作为初学者可能都很纳闷,官方不是说了原则是这样的,为什么会出现表里不一的情况呢?其实仔细研究才发现并不是所有的元素都支持属性值继承。还会存在一些意外的情况,那么总的来说是由于以下两个方面:
1、有些Dependency属性在用注册的时候时指定Inherits为不可继承,这样继承就会失效了。
2、有其他更优先级的设置设置了该值,在前面讲的的“依赖属性的优先级”你可以看到具体的优先级别。
属性值继承通过混合树操作。持有原始值的父对象和继承该值的子对象都必须是 FrameworkElement 或 FrameworkContentElement,且都必须属于某个逻辑树。 但是,对于支持属性继承的现有 WPF 属性,属性值的继承能够通过逻辑树中没有的中介对象永久存在。 这主要适用于以下情况:让模板元素使用在应用了模板的实例上设置的所有继承属性值,或者使用在更高级别的页级成分(因此在逻辑树中也位于更高位置)中设置的所有继承属性值。 为了使属性值的继承在这两种情况下保持一致,继承属性必须注册为附加属性

--可继承属性必须为依赖属性!!!