WPF依赖属性和附加属性

文章详细介绍了WindowsPresentationFoundation(WPF)中的依赖属性和附加属性的使用,包括Version和IsMax属性的声明、绑定和触发器应用。展示了如何通过DependencyProperty实现动画、样式和数据绑定。
摘要由CSDN通过智能技术生成

一、依赖属性

 public class MyWindow:Window
    {
        //propdp
        /// <summary>
        /// 1.效果实现方法在style中使用trigger
        /// 2.callback
        /// </summary>
        #region Version 依赖属性(用途:用于绑定)
        public string Version
        {
            get { return (string)GetValue(VersionProperty); }
            set { SetValue(VersionProperty, value); }
        }

        // Using a DependencyProperty as the backing store for IsHighLighted.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty VersionProperty =
            DependencyProperty.Register("Version", typeof(string), typeof(MyWindow), new PropertyMetadata("", Callback));

        private static void Callback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var win = d as MyWindow;
            //做些什么...
        }
        #endregion

        #region IsMax 只读依赖属性(用途:结合属性触发器 (Trigger) 来实现样式的切换)
        public bool IsMax => (bool)GetValue(IsMaxProperty);
        public static readonly DependencyProperty IsMaxProperty;
        public static readonly DependencyPropertyKey IsMaxPropertyKey;

        static MyWindow()
        {
            IsMaxPropertyKey = DependencyProperty.RegisterReadOnly("IsMax", typeof(bool), typeof(MyWindow), new PropertyMetadata(false));
            IsMaxProperty = IsMaxPropertyKey.DependencyProperty;
        }
        #endregion
    }

 

local:MyWindow x:Class="WindowChromeDemo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WindowChromeDemo"        
        mc:Ignorable="d"
        Title="Title"
        Version="1.0"
        Height="450" Width="800">

    <Grid Background="White">
        
    </Grid>

</local:MyWindow>

二、附加属性

public class AttachHelper
    {


        //propa

        public static string GetVersion(DependencyObject obj)
        {
            return (string)obj.GetValue(VersionProperty);
        }

        public static void SetVersion(DependencyObject obj, string value)
        {
            obj.SetValue(VersionProperty, value);
        }

        // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty VersionProperty =
            DependencyProperty.RegisterAttached("Version", typeof(string), typeof(AttachHelper), new PropertyMetadata(""));



    }
 <TextBlock local:AttachHelper.Version="1.0"/>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值