自定義控件綁定屬性需要提前注冊這個屬性,同時注冊對應的回調函數
例如,若要添加信號值屬性
- 在自定義控件中添加保存數據的屬性
public double SignalValue
{
get { return (int)GetValue(SignalValueProperty); }
set { SetValue(SignalValueProperty, value); }
}
- 在自定義控件中注冊依賴屬性
//依賴屬性注冊
//(string name, Type propertyType, Type ownerType, PropertyMetadata typeMetadata)
//參數分別對應依賴屬性名,依賴屬性數據類型,自定義控件類型,回調函數
public static readonly DependencyProperty SignalValueProperty = DependencyProperty.Register("SignalValue", typeof(int), typeof(PowerControl), new UIPropertyMetadata(1, ChangeSignal));
- 添加回調函數,在函數中執行對應的邏輯
private static void ChangeSignal(DependencyObject obj, DependencyPropertyChangedEventArgs r)
{
//獲取到具體的對象
PowerControl control = (PowerControl)obj;
control.SetClip(control.SignalValue);
}
- 然后調用組件
<component:PowerControl SignalValue="{Binding signal}"/>