我想创建一个简单的依赖项属性代理。我制作了一个自定义控件,它是一个文件选择器,由文本框(名称:"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