zoukankan      html  css  js  c++  java
  • WPF学习之路(三) 属性与依赖

     类型是DependencyProperty的属性是依赖属性

     依赖属性不同于普通的.Net属性,类似于一个计算过程,根据依赖的值得到最终值。

    为什么引入依赖属性:

    MSDN原文 One of the primary architectural philosophies used in building WPF was a preference for properties over methods or events.

    WPF的设计思想是侧重属性胜于方法和事件。

    实例  

     依赖属性对资源引用的支持

    设置Button的Background为金色

    APP.xaml

    <Application.Resources>
      <SolidColorBrush x:Key="MyBrush" Color="Gold" />
    </Application.Resources>

     MainWindow.xaml

    <Button Grid.Row="0" Grid.Column="1" Name="btn1" Margin="5" Background="{DynamicResource MyBrush}">Golden Button</Button>

     依赖属性对样式的支持

    App.xaml

    <Application.Resources>
      <Style x:Key="GreenButtonStyle">
        <Setter Property="Control.Background" Value="Green" />
      </Style>
    </Application.Resources>

    MainWindow.xaml

     <Button Grid.Row="0" Grid.Column="3" Name="btn2" Margin="5" Style="{StaticResource GreenButtonStyle}">Green Button</Button>

    依赖属性对动画的支持

    <Button Grid.Row="1" Grid.Column="1" Name="btn3" Margin="5">
                    <Button.Background>
                        <SolidColorBrush x:Name="AnimBrush" />
                    </Button.Background>
                    <Button.Triggers>
                        <EventTrigger RoutedEvent="Button.Loaded">
                            <BeginStoryboard>
                                <Storyboard>
                                    <ColorAnimation Storyboard.TargetName="AnimBrush"
                                                    Storyboard.TargetProperty="(SolidColorBrush.Color)"
                                                    From="Red" To="Green" Duration="0:0:5" 
                                                    AutoReverse="True" RepeatBehavior="Forever" />
                                </Storyboard>
                            </BeginStoryboard>
                        </EventTrigger>
                    </Button.Triggers>
                    Animation Button</Button>

    依赖属性对数据绑定的支持

    新建数据类型

        class BindingData
        {
            public BindingData() { }
    
            private string colorName = "Red";
    
            public string ColorName
            {
                get { return this.colorName; }
                set { this.colorName = value; }
            }
        }

    App.xaml

    <Application x:Class="Alex_WPFAPPDemo01.App"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:local="clr-namespace:Alex_WPFAPPDemo01"
                 StartupUri="MainWindow.xaml">
        <Application.Resources>
            <local:BindingData x:Key="DataSource" />
        </Application.Resources>
    </Application>

    MainWindow.xaml

     <Button Grid.Row="1" Grid.Column="3" Name="btn4" Margin="5" Background="{Binding Source={StaticResource ResourceKey=DataSource}, Path=ColorName}">
      Is bound to red</Button>

    依赖属性对属性值继承的支持

     MainWindows.xaml.cs

         private double fontSize = 0;
    
            public MainWindow()
            {
                InitializeComponent();
                fontSize = this.FontSize;
            }
    
            private void SetFontSize(object sender, RoutedEventArgs e)
            {
                this.FontSize = 16;
            }
    
            private void SetButtonFont(object sender, RoutedEventArgs e)
            {
                this.btn6.FontSize = 8;
            }
    
            private void ResetFontSize(object sender, RoutedEventArgs e)
            {
                this.FontSize = fontSize;
                this.btn6.FontSize = fontSize;
            }

    MainWindows.xaml

    <Button Grid.Row="2" Grid.Column="1" Name="btn5" Margin="5" Click="SetFontSize">Set the window font</Button>
    <Button Grid.Row="2" Grid.Column="2" Name="btn6" Margin="5" Click="SetButtonFont">Set the button font</Button>
    <Button Grid.Row="2" Grid.Column="3" Name="btn7" Margin="5" Click="ResetFontSize">Reset the font</Button>

     依赖属性设置的优先级

     优先级按从高到低排序:

      属性系统强制转换

      动画

      本地值

      模板属性

      隐式样式

      样式触发器

      模板触发器

      样式

      继承

      默认值

    附加属性

    在WPF里最典型的附加属性就是各种布局中的属性,Grid.Row DockPanel.Dock等,方便处理布局的问题。

    附加属性实质是依赖属性,与普通的依赖属性相比有以下不同

    注册方式不同,通过GetSet实现属性封装,没有普通的.Net属性

    依赖属性的安全性

    https://msdn.microsoft.com/zh-cn/library/cc903923(v=vs.95).aspx

    To be continue...

  • 相关阅读:
    Mysql Got a packet bigger than 'max_allowed_packet' bytes
    Git之IDEA集成Git更新项目Update Type选项解释
    IDEA获取GIT仓库时更新类型update type的选择
    git merge和git rebase的区别
    git merge和git merge --no-ff的区别
    Git中fetch和pull命令的区别
    git官网下载太慢解决方法
    IDEA执行Thread.activeCount() = 2的问题
    k8s 常见错误汇总
    Axure9破解
  • 原文地址:https://www.cnblogs.com/alex09/p/4429358.html
Copyright © 2011-2022 走看看