0
点赞
收藏
分享

微信扫一扫

wpf自定义控件绑定依赖属性

自定义控件绑定属性需要提前注册这个属性,同时注册对应的回调函数

例如,若要添加信号值属性

  1. 在自定义控件中添加保存数据的属性

public double SignalValue
{
get { return (int)GetValue(SignalValueProperty); }
set { SetValue(SignalValueProperty, value); }
}

  1. 在自定义控件中注册依赖属性

//依赖属性注册
//(string name, Type propertyType, Type ownerType, PropertyMetadata typeMetadata)
//参数分别对应依赖属性名,依赖属性数据类型,自定义控件类型,回调函数
public static readonly DependencyProperty SignalValueProperty = DependencyProperty.Register("SignalValue", typeof(int), typeof(PowerControl), new UIPropertyMetadata(1, ChangeSignal));

  1. 添加回调函数,在函数中执行对应的逻辑

 private static void ChangeSignal(DependencyObject obj, DependencyPropertyChangedEventArgs r)
{
//获取到具体的对象
PowerControl control = (PowerControl)obj;
control.SetClip(control.SignalValue);
}

  1. 然后调用组件

<component:PowerControl SignalValue="{Binding signal}"/>

留待后查,同时方便他人



举报

相关推荐

0 条评论