[英]Setting a dependency property of a control in the custom control
我在解決方案中繼承了一個基礎設施網格。 現在,我想在我的自定義控件代碼中而不是在xaml中設置在基礎設施網格控件中定義的依賴項屬性。 我如何完成設置依賴項屬性?
我能夠在我的自定義控件中設置支持CLR屬性,但是正如預期的那樣,它沒有觸發預期的gridcontrol UI更改,因此被迫在我的自定義控件中設置依賴項屬性。
我究竟該如何完成?
通常,使用CLR屬性'wrappers'這樣聲明DependencyProperty
:
public static readonly DependencyProperty ItemsProperty = DependencyProperty.Register(
"Items", typeof(ObservableCollection<string>), typeof(YourControlType));
public ObservableCollection<string> Items
{
get { return (ObservableCollection<string>)GetValue(ItemsProperty); }
set { SetValue(ItemsProperty, value); }
}
如果您繼承的Grid
像這樣聲明了它們的DependencyProperties
,那么您應該能夠像這樣直接使用CLR包裝的屬性:
Items = new ObservableCollection<string>();
Items.Add("Something");
如果他們沒有這樣做,那么您可以在包裝的屬性中看到如何訪問它們:
anInstanceOfYourControlType.SetValue(ItemsProperty, "some value");
當然,他們有機會聲明內部屬性,在這種情況下,您將無法訪問它們,但是您應該能夠從他們的在線文檔中找到它們。
您可以使用DependencyProperty SetValue設置Dependency屬性的本地值。
dataGridObject.SetValue(DataGrid.ItemsSourceProperty, value);
但是,如果您的DP綁定了某些CLR屬性,並且您也想對其進行更新,請使用SetCurrentValue方法。
dataGridObject.SetCurrentValue(DataGrid.ItemsSourceProperty, value);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.