我想創建一個簡單的依賴項屬性代理。我製作了一個自定義控件,它是一個文件選擇器,由文本框(名稱:"TextBox_FilePath"
)和一個顯示打開文件對話框的按鈕組成。創建一個依賴項屬性的代理
由於我正在製作可重複使用的控件,我希望它具有"SelectedFilePath"
屬性。由於Text
屬性似乎是完美的我的控制是"SelectedFilePath"
屬性,我只想代理這些依賴屬性。
我提出的第一種方法是:
public static readonly DependencyProperty SelectedFilePathProperty = TextBox.TextProperty;
public string SelectedFilePath
{
get { return (string) this.TextBox_FilePath.GetValue(SelectedFilePathProperty); }
set { this.TextBox_FilePath.SetValue(SelectedFilePathProperty, value); }
}
其工作,但扔試圖綁定到該屬性時異常。然後我脫掉了:
public static readonly DependencyProperty SelectedFilePathProperty =
DependencyProperty.Register("SelectedFilePath", typeof (string), typeof (FilePicker), new PropertyMetadata(default(string)));
public string SelectedFilePath
{
get { return (string) this.TextBox_FilePath.GetValue(SelectedFilePathProperty); }
set { this.TextBox_FilePath.SetValue(SelectedFilePathProperty, value); }
}
哪個工作,但我不知道爲什麼?!我在哪裏指定我想要文本框的text
屬性?
我錯過了什麼,只是代理出來的依賴屬性?
編輯: 與AddOwner
的解決方案不工作過,它拋出一個EXCETION說:「結合只能在一個依賴屬性應用」。代碼:
public static readonly DependencyProperty SelectedFilePathProperty =
TextBox.TextProperty.AddOwner(typeof(FilePicker));
public string SelectedFilePath
{
get { return (string)this.TextBox_FilePath.GetValue(SelectedFilePathProperty); }
set { this.TextBox_FilePath.SetValue(SelectedFilePathProperty, value); }
}
我不明白?
EDIT2: 對於大家理解別人的答案有問題,我做了a little graphic
請看看我的編輯:) – GameScripting
*正如我所說的*,它的名稱爲「文本」中註冊,可以因此只有綁定到一個所謂的財產'Text',你創建的'SelectedFilePath'屬性只是爲了方便你的命令式代碼的一個包裝,綁定永遠不會使用它。如果你想要一個不同的名字,請註冊你自己的房產 –
願你如此善良,並提供一個例子或提供任何參考如何做到這一點? – GameScripting