如何从 Generic.xaml 中定义的资源字典绑定到自定义控件的依赖属性?
编辑:我重新表述了整个问题。
大家好,
我有一个带有依赖属性的自定义控件。在 Generic.xaml 文件中,我有一个资源字典。它是外部字典中的资源字典,定义如下:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyNamespace">
<!-- This is the dictionary-->
<ResourceDictionary x:Name="TheDictionaryImTalkingAbout" . . . >
.
.
.
</ResourceDictionary>
.
.
.
</ResourceDictionary>
在这个资源字典 TheDictionaryImTalkingAbout 中,我想绑定到控件的依赖属性。我尝试了以下 XAML:
<Object x:Key="MyObject" SomeProperty="{Binding MyDependencyProperty, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type MyNamespace:MyControl}}}"/>
绑定没有返回错误,但是它不起作用。谁能告诉我如何从 Generic.xaml 内的资源字典中绑定到我的父控件?
编辑:此绑定确实有效,但仅适用于某些属性。我无法将 GradientStop Color 绑定到颜色类型的依赖属性。当这是一个用户控件时它曾经可以工作,但现在我创建了一个自定义控件,它不再工作了。我不知道为什么,但如果您感兴趣,我在这里问这个问题:
EDIT: I rephrased the entire question.
Hello everybody,
I have a custom control with dependency properties. In the Generic.xaml file I have a resource dictionary. It's a resource dictionary within the outer dictionary, defined like so:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyNamespace">
<!-- This is the dictionary-->
<ResourceDictionary x:Name="TheDictionaryImTalkingAbout" . . . >
.
.
.
</ResourceDictionary>
.
.
.
</ResourceDictionary>
In this resource dictionary, TheDictionaryImTalkingAbout, I want to bind to a dependency property of my control. I tried the following XAML :
<Object x:Key="MyObject" SomeProperty="{Binding MyDependencyProperty, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type MyNamespace:MyControl}}}"/>
The binding returns no errors, however, it doesn't work. Can anyone tell me how I'm supposed to bind to my parent control from within a resource dictionary that's within Generic.xaml?
EDIT: This binding DOES work, but only for certain properties. I am unable to bind GradientStop Color to a dependency property of type color. It USED to work when this was a UserControl, but it doesn't work anymore now that I created a custom control. I don't know why, but if you're interested, I asked this question here:
Why can I no longer bind GradientStop Color to a Dependency Property of my control?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
ResourceDictionary 中的位置与relativesource findancestor 绑定的解析无关。源在成为可视化树的一部分后在运行时解析。您发布的 XAML 中没有任何内容可用于诊断您遇到的问题。
无关:是什么导致您选择在另一个 ResourceDictionary 中声明一个 ResourceDictionary?
Location in a ResourceDictionary has nothing to do with resolution of a RelativeSource FindAncestor Binding. The Source is resolved at runtime after it becomes part of a Visual Tree. There is nothing in the XAML you have posted that could be used to diagnose the problem you are having.
Unrelated: What led you choose to declare a ResourceDictionary inside another ResourceDictionary?
我看到了华尔街程序员的回答。因此,我不知道最终绑定是否有效。但是您在绑定中看到的问题是,您必须声明 UserControl 所在的命名空间,然后在绑定中使用它。
在 xaml 顶部添加命名空间声明。如果命名空间是“WindowsApplication”,那么这将如下所示:
然后在绑定中写入
I have seen the answer from Wallstreet Programmer. Therefore, I don't know if in the end, the binding will work. But the problem that you see with your binding is, that you must declare the namespace where your UserControl is and then use this in the binding.
Add a namespace-declaration on top of your xaml. If the namespace is "WindowsApplication" then this will look like follows:
Then in the binding, write
您无法对 GradientStop 进行数据绑定,请参阅如何在 Silverlight 中绑定 GradientStop Colors 或 GradientStops 属性?
You can't databind to a GradientStop, see How to bind GradientStop Colours or GradientStops Property in Silverlight?
如果有人感兴趣,这就是我从 Generic.xaml 内部绑定到自定义控件的依赖属性的方式:
来自 Generic.xaml 的一部分:
如果您收到此消息:
关键是使用 TemplateBinding 并在属性前添加类型名称前缀(就像我所做的那样:'local:MyControl.MyFoo')
If anyone is interested, this is how I bind to my dependency property from inside my Generic.xaml for a custom control:
A part from Generic.xaml:
If you receive this message:
The key is to use TemplateBinding and prefix your property with the type name (like I did: 'local:MyControl.MyFoo')