I am trying to create a simple proxy resource for dependency properties. I created my own control, it is a file collector, which is made from a text field (name: "TextBox_FilePath"
) and a button that opens a dialog box for an open file.
Since I'm doing reuse, I would like it to have the "SelectedFilePath"
property. Since the Text
property seems ideal for my control as a "SelectedFilePath"
property, I just would like to proxy this dependency property.
The first approach I did was the following:
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); } }
which worked, but threw an exception when trying to bind to this property. Then I worked:
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); } }
which works, but I have no idea why ?! Where have I pointed out, do I need the Text
property of a text field?
What am I missing just to proxy this dependency property?
EDIT: The AddOwner
doesnβt work either, it throws an Excetion saying that "the binding can only be applied to the dependency property." The code:
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); } }
What? I do not understand?
EDIT2: For everyone who has trouble understanding the answer, I made a little graphics