zoukankan      html  css  js  c++  java
  • WPF Unleashed Chapter 3:Important New Concepts in WPF Dependency Properties(Property Value Inheritance)

    声明: 本译文仅供学习讨论,禁止用于商业用途,否则后果自负


    Property Value Inheritance

            “属性值继承”这一术语(简称属性继承)并不是传统面向对象里的继承,而是指属性值会沿着元素树向下传递。List 3.4中就展示了一个简单的例子,它在List 3.1的基础山进行了改进,显示地设置了FontSize和FontStyle这两个dp的值。图3.4为运行结果

    LISTING 3.4 The About Dialog with Font Properties Set on the Root Window
          <Window xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation”
                Title=”About WPF Unleashed” SizeToContent=”WidthAndHeight”
                FontSize=”30” FontStyle=”Italic”
                Background=”OrangeRed”>
                <StackPanel>
                <Label FontWeight=”Bold” FontSize=”20” Foreground=”White”>
                      WPF Unleashed (Version 3.0)
                </Label>
                <Label>© 2006 SAMS Publishing</Label>
                <Label>Installed Chapters:</Label>
                <ListBox>
                      <ListBoxItem>Chapter 1</ListBoxItem>
                      <ListBoxItem>Chapter 2</ListBoxItem>
                </ListBox>
                <StackPanel Orientation=”Horizontal” HorizontalAlignment=”Center”>
                      <Button MinWidth=”75” Margin=”10”>Help</Button>
                      <Button MinWidth=”75” Margin=”10”>OK</Button>
                </StackPanel>
                <StatusBar>You have successfully registered this product.</StatusBar>
                </StackPanel>
          </Window>



            在大多数情况下,这两个设置会传递到元素树并被其子元素所继承。这种情况对位于逻辑树第三级的Button和ListBoxItem同样适用。List3.4中的第一个Label控件的FontSize没有变成父元素的值,这是因为Label的FontSize属性被显示将设置为20。对于值被设置成Italic的FontStyle来说,它会影响所有的Label,ListBoxItem,Button,因为这些元素都没有设置FontStyle。

            注意,虽然StatusBar控件也支持FontSize和FontStyle属性,但是它的文本并没有受到任何一个的影响。属性值继承行为在两种情况下是可以被忽略的: 
        1    并不是每一个DP都支持属性值继承(DP属性可以通过将FrameworkPropertyMetadataOptions.Inherits出递给DependencyProperty.Register方法来选择是否支持属性值继承) 
        2    属性值被优先级更高的资源设置,下一节会解释

            本例中StatusBar的文本没有继承父元素的值是由第二个原因引起的。一小部分控件,如StatusBar,Menu和ToolTip,他们内部将字体相关的属性设置成当前系统的设置。对于这种控件,用户可以通过控制面板(Control Panel)来改变其字体。结果会使人很困惑,这一类控件会吞噬屌任何来自元素树的值。以List3.4举例来说,现在将一个StatusBar控件添加一个Button控件,作为逻辑子元素。Button内文本的字体大小和字体样式都是默认值:分别是12和Normal。

    DIGGING DEEPER  
                                        Property Value Inheritance in Additional Places
            虽然属性值继承原本是为操作元素树而设计的,但也可以扩展到其他场景中。例如,虽然某些元素既不属于逻辑树,又不属于视觉树,但从XML的角度上看起来像是某个元素的子元素(因为XAML的属性元素语法的缘故)。在这种情况下,“父”元素的值是可以传递给这些元素的。这些“伪子元素”可以是元素的触发器,,属性值或者继承自Freezable对象。该主题的内容并没有规范到文档中,看上去有些随意,但在许多场景中都可以很好的运行。
      
              下一篇文章介绍Dependency Property的提供者
    如果您有WPF方面的问题,可以给我留言一起探讨,感谢大家浏览
  • 相关阅读:
    LeetCode 109 Convert Sorted List to Binary Search Tree
    LeetCode 108 Convert Sorted Array to Binary Search Tree
    LeetCode 107. Binary Tree Level Order Traversal II
    LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal
    LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal
    LeetCode 103 Binary Tree Zigzag Level Order Traversal
    LeetCode 102. Binary Tree Level Order Traversal
    LeetCode 104. Maximum Depth of Binary Tree
    接口和多态性
    C# 编码规范
  • 原文地址:https://www.cnblogs.com/stswordman/p/802764.html
Copyright © 2011-2022 走看看