无法使用控件的依赖属性
我的自定义控件名为“FileSelectDialog”,具有依赖属性:
public static readonly DependencyProperty FilePathProperty =
DependencyProperty.Register("FilePath", typeof(string), typeof(FileSelectDialog));
public string FilePath
{
get { return (string)GetValue(FilePathProperty); }
set { SetValue(FilePathProperty, value); }
}
然后我尝试像这样绑定到此依赖属性:
<controls:FileSelectDialog FilePath="{Binding FolderName}"/>
但没有任何反应,控件中没有显示初始文本,没有更新的文本保存到“FolderName”属性!我在输出窗口中遇到这样的错误:
System.Windows.Data Error: 40 : BindingExpression path error: 'FolderName' property not found on 'object' ''FileSelectDialog' (Name='FolderSelector')'. BindingExpression:Path=FolderName; DataItem='FileSelectDialog' (Name='FolderSelector'); target element is 'FileSelectDialog' (Name='FolderSelector'); target property is 'FilePath' (type 'String')
因此,据我了解,控件尝试在自身上查找属性“FolderName”,而它必须在父控件 DataContext 中查找它。例如,当我使用简单的文本框时:
<TextBox Text="{Binding Path=FolderName}"/>
一切正常。
I have my custom control named "FileSelectDialog" with Dependency Property:
public static readonly DependencyProperty FilePathProperty =
DependencyProperty.Register("FilePath", typeof(string), typeof(FileSelectDialog));
public string FilePath
{
get { return (string)GetValue(FilePathProperty); }
set { SetValue(FilePathProperty, value); }
}
Then I'm trying to bind to this dependency property like this:
<controls:FileSelectDialog FilePath="{Binding FolderName}"/>
But nothing happing, no initial text shown in my control, no updated text's saving to 'FolderName' property! I got such error in Output window:
System.Windows.Data Error: 40 : BindingExpression path error: 'FolderName' property not found on 'object' ''FileSelectDialog' (Name='FolderSelector')'. BindingExpression:Path=FolderName; DataItem='FileSelectDialog' (Name='FolderSelector'); target element is 'FileSelectDialog' (Name='FolderSelector'); target property is 'FilePath' (type 'String')
So, as far as I understand, control try to find property 'FolderName on itself, while it must look for it in parents control DataContext. For example, when I use simple textbox:
<TextBox Text="{Binding Path=FolderName}"/>
All is working fine.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对我来说似乎是一个基本的 DataContext 问题
您如何设置 FileSelectDialog 控件的 DataContext ?似乎您在代码中将 dataContext 设置为“Me”/“this”,或者在 xaml 中将 dataContext 设置为“RelativeSource Self”或类似的内容。
Seems a basic DataContext issue to me
How did you set the DataContext of your FileSelectDialog Control ? seems you set the dataContext in code as 'Me'/'this' or in xaml with 'RelativeSource Self' or something like this.
我知道您正在自定义控件中公开此属性,但是您是否使用依赖项属性中设置的值更新自定义控件中的某些控件?
您可能需要附加回调并在自定义控件的某些控件中显示 DP 中设置的值。
就像:
如果已经这样做了,那么第二个问题就是绑定中显示的错误。为此,请尝试将控件的 DataContext 设置为具有 source 属性的对象。
I understand you are exposing this property in a custom control, but are you updating some of the control in your customcontrol with the value set in your dependency property?
You may need to attach a callback and show the value set in your DP in some control in your customcontrol.
Like:
If already doing this, then the second problem is the error shown in Binding. For this try setting the DataContext of your control to the object which has the source property.
你必须witre属性改变回调函数
You have to witre the property changed call back function