WPF 自定义控件依赖属性改变触发故事版,动画

针对自定义控件的特性,由于它的界面是在一个Themes/Generic.xaml文件中,并且在ControlTemplate中,所以,不能根据x:Name来访问其中的控件,在ControlTemplate中的资源和控件(建议把资源和控件,动画等都写到ControlTemplate中)的访问要通过重写OnApplyTemplate,方法GetTemplateChild来获得。

那么,许多特性就不能在xaml中编写了,包括绑定。

自定义控件,依赖属性如何绑定到Generic.xaml中的控件上,只能通过GetTemplateChild方法获取到该控件,然后在后台绑定。动画可以写到资源中,到时候获取然后Begin即可,但是不建议这么做,因为考虑到灵活性,动画的值如果跟业务相关那就不好控制了,所以建议在后台创建动画,虽然代码比较多,但是灵活。

在创建依赖属性时

public static DependencyProperty Register(
    string name,
    Type propertyType,
    Type ownerType,
    PropertyMetadata defaultMetadata
)

注意到第四个参数PropertyMetadata,该参数不仅能赋给一个默认值,还能够添加一个函数,该函数将在依赖属性值改变的时候执行,我们的动画Begin就在这里面,但是该方法是静态的又该怎么弄?

请看代码

public float CurrentValue
        {
            get { return (float)GetValue(CurrentValueProperty); }
            set { SetValue(CurrentValueProperty, value); }
        }

        // Using a DependencyProperty as the backing store for CurrentValue.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty CurrentValueProperty =
            DependencyProperty.Register("CurrentValue", typeof(float), typeof(Temp), new PropertyMetadata(50f, CurrentValueChange));

        public static void CurrentValueChange(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            (d as Temp).StoryboardPlay(e);
        }

        protected void StoryboardPlay(DependencyPropertyChangedEventArgs e)
        {
            Storyboard sb = new Storyboard();
            DoubleAnimation da = new DoubleAnimation();
            da.To = double.Parse(e.NewValue.ToString());
            da.Duration = new Duration(TimeSpan.Parse("0:0:1"));
            rect.BeginAnimation(Rectangle.HeightProperty, da);
        }

这样就完美解决问题了。

源码在这http://files.cnblogs.com/HelloMyWorld/%E8%87%AA%E5%AE%9A%E4%B9%89%E6%8E%A7%E4%BB%B6%E5%BC%80%E5%8F%91Sample.zip

转载于:https://www.cnblogs.com/HelloMyWorld/archive/2013/04/05/3001154.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
获取WPF自定义控件依赖属性的数据流程如下: 1. 定义依赖属性:在自定义控件的代码,定义一个依赖属性并注册该属性。例如: ``` public static readonly DependencyProperty MyPropertyProperty = DependencyProperty.Register( "MyProperty", typeof(string), typeof(MyControl), new PropertyMetadata("Default Value")); public string MyProperty { get { return (string)GetValue(MyPropertyProperty); } set { SetValue(MyPropertyProperty, value); } } ``` 2. 绑定依赖属性:在XAML,将自定义控件依赖属性绑定到其他控件或数据源。例如: ``` <local:MyControl MyProperty="{Binding MyData}" /> ``` 3. 获取依赖属性的值:当自定义控件被渲染时,WPF框架会自动调用依赖属性的get方法,从绑定的数据源获取属性的值。如果没有绑定任何数据源,则使用属性的默认值。例如: ``` string myPropertyValue = myControlInstance.MyProperty; ``` 4. 监听依赖属性的变化:如果需要在属性值发生变化时执行一些自定义逻辑,可以在自定义控件注册属性值变化的回调函数。例如: ``` public static readonly DependencyProperty MyPropertyProperty = DependencyProperty.Register( "MyProperty", typeof(string), typeof(MyControl), new PropertyMetadata("Default Value", OnMyPropertyChanged)); private static void OnMyPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { // Execute custom logic when MyProperty value changes } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值