zoukankan      html  css  js  c++  java
  • WPF依赖属性-依赖属性的传递(继承)

     依赖属性的传递,在XAML逻辑树上, 内部的XAML元素,关联了外围XAML元素同名依赖属性值 ;

    <Window x:Class="Custom_DPInherited.DPInherited"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
          xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
          mc:Ignorable="d" 
          d:DesignHeight="300" d:DesignWidth="300"
          FontSize="18"
          Title="依赖属性的继承">
        <StackPanel >
            <Label Content="继承自Window的FontSize" />
            <Label Content="显式设置FontSize" 
                   TextElement.FontSize="36"/>
            <StatusBar>Statusbar没有继承自Window的FontSize</StatusBar>
        </StackPanel>
    </Window>
    

    在这里插入图片描述
     在上面XAML代码中。Window.FontSize设置会影响所有内部子元素字体大小,这就是依赖属性的值传递。如第一个Label没有定义FontSize,所以它继承了Window.FontSize值。但一旦子元素提供了显式设置,这种继承就会被打断,所以Window.FontSize值对于第二个Label不再起作用。
     但是,并不是所有元素都支持属性值继承的,如StatusBar、Tooptip和Menu控件。另外,StatusBar等控件截获了从父元素继承来的属性,使得该属性也不会影响StatusBar控件的子元素。例如,如果我们在StatusBar中添加一个Button。那么这个Button的FontSize属性也不会发生改变,其值为默认值

    获取外围依赖属性值
    定义自定义依赖属性时,可通过AddOwer方法可以使依赖属性,使用外围元素的依赖属性值。具体的实现代码如下所示

    public class CustomStackPanel : StackPanel
        {
            public static readonly DependencyProperty MinDateProperty;
    
            static CustomStackPanel()
            {
                MinDateProperty = DependencyProperty.Register("MinDate", typeof(DateTime), typeof(CustomStackPanel), new FrameworkPropertyMetadata(DateTime.MinValue, FrameworkPropertyMetadataOptions.Inherits));
            }
    
            public DateTime MinDate
            {
                get { return (DateTime)GetValue(MinDateProperty); }
                set { SetValue(MinDateProperty, value); }
            }
        }
    
        public class CustomButton :Button
        {
            private static readonly DependencyProperty MinDateProperty;
    
            static CustomButton()
            {
                // AddOwner方法指定依赖属性的所有者,从而实现依赖属性的传递,即CustomStackPanel的MinDate属性可以传递给CustomButton控件。
                // 注意FrameworkPropertyMetadataOptions的值为Inherits
                MinDateProperty = CustomStackPanel.MinDateProperty.AddOwner(typeof(CustomButton), new FrameworkPropertyMetadata(DateTime.MinValue, FrameworkPropertyMetadataOptions.Inherits));
            }
    
            public DateTime MinDate
            {
                get { return (DateTime)GetValue(MinDateProperty); }
                set { SetValue(MinDateProperty, value); }
            }
        }
    
    <Window x:Class="Custom_DPInherited.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:local="clr-namespace:Custom_DPInherited"
            xmlns:sys="clr-namespace:System;assembly=mscorlib"
            Title="实现自定义依赖属性的值传递" Height="350" Width="525">
        <Grid>
            <local:CustomStackPanel x:Name="customStackPanle" MinDate="{x:Static sys:DateTime.Now}">
                <!--CustomStackPanel的依赖属性-->
                <ContentPresenter Content="{Binding Path=MinDate, ElementName=customStackPanle}"/>
                <local:CustomButton Content="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=MinDate}" Height="25"/>
            </local:CustomStackPanel>
        </Grid>
    </Window>
    

    在这里插入图片描述

  • 相关阅读:
    VIM于换行EOL的思考
    wp7给图片和按钮添加背景图片
    wp7打开加速度传感器
    java值传递、引用传递
    keychron k1长时间不使用蓝牙,蓝牙连接不上解决方案
    IE浏览器切换64位和32位
    文件和注册表的重定向解决方法
    Windows C++遍历所有进程的命令行
    c++ fstream open 失败原因
    解析wrk压测工具
  • 原文地址:https://www.cnblogs.com/MonoHZ/p/14944142.html
Copyright © 2011-2022 走看看