例如,在背景屬性中,我們可以這樣做
<cs:CustomControl.Background>
<SolidColorBrush Color="{Binding BackGround}"/>
</cs:CustomControl.Background>
SolidColorBrush是一個單獨的類,包含單獨的依賴屬性,它作業正常,背景屬性按預期更新。
我正在嘗試在我的CustomControl.
我已經創建了一個具有這樣的依賴屬性的子類
public class SubClass : DependencyObject
{
public double SubProperty1
{
get { return (double)GetValue(SubProperty1Property); }
set { SetValue(SubProperty1Property, value); }
}
// Using a DependencyProperty as the backing store for SubProperty1. This enables animation, styling, binding, etc...
public static readonly DependencyProperty SubProperty1Property =
DependencyProperty.Register("SubProperty1", typeof(double), typeof(SubClass), new PropertyMetadata(10.0d, OnSubProperty1Changed));
private static void OnSubProperty1Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
//never called;
}
public string SubProperty2
{
get { return (string)GetValue(SubProperty2Property); }
set { SetValue(SubProperty2Property, value); }
}
// Using a DependencyProperty as the backing store for SubProperty1. This enables animation, styling, binding, etc...
public static readonly DependencyProperty SubProperty2Property =
DependencyProperty.Register("SubProperty2", typeof(string), typeof(SubClass), new PropertyMetadata("test", OnSubProperty2Changed));
private static void OnSubProperty2Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
//never called;
}
}
在我的自定義工具的主類中,我創建了依賴屬性,如下所示:
public SubClass MainProperty
{
get { return (SubClass)GetValue(MainPropertyProperty); }
set { SetValue(MainPropertyProperty, value); }
}
// Using a DependencyProperty as the backing store for MainProperty. This enables animation, styling, binding, etc...
public static readonly DependencyProperty MainPropertyProperty =
DependencyProperty.Register("MainProperty", typeof(SubClass), typeof(CustomControl), new PropertyMetadata((SubClass)null, OnMainPropertyChanged));
private static void OnMainPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
// called when object created only and not called again when the property changed on the UI
}
然后在我的 WPF 視窗中,我執行了以下操作
<cs:CustomControl.MainProperty>
<cs:SubClass SubProperty1="{Binding Length}" SubProperty2="{Binding Title}"/>
</cs:CustomControl.MainProperty>
我的代碼編譯但依賴屬性沒有更新,并且視圖模型更新時沒有呼叫回呼函式
我怎樣才能使這項作業?我想念什么我希望有任何詳細代碼的想法提前謝謝
uj5u.com熱心網友回復:
在 Visual Studio 的輸出視窗中,您應該看到類似
System.Windows.Data 錯誤:2:找不到目標元素的管理 FrameworkElement 或 FrameworkContentElement。...
當你除錯你的應用程式時。這是因為您的控制元件及其 DependencyObject 屬性沒有形成邏輯樹。
為了構建邏輯樹,子類必須派生自Freezable
public class SubClass : Freezable
{
...
protected override Freezable CreateInstanceCore()
{
return new SubClass();
}
}
并且 CustomControl 必須將 SubClass 實體添加到其邏輯子集合中:
private static void OnMainPropertyChanged(
DependencyObject o, DependencyPropertyChangedEventArgs e)
{
if (e.OldValue != null)
{
((CustomControl)o).RemoveLogicalChild(e.OldValue);
}
if (e.NewValue != null)
{
((CustomControl)o).AddLogicalChild(e.NewValue);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/436207.html
