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"开发者_JS百科; . . . >
.
.
.
</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?
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?
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:
xmlns:local="clr-namespace:WindowsApplication"
Then in the binding, write
<GradientStop Color="{Binding Scheme, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:MyControl}}}" Offset="0"/>
You can't databind to a GradientStop, see How to bind GradientStop Colours or GradientStops Property in Silverlight?
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:
<ContentControl x:Key="MyFooDepProp"
Content="{TemplateBinding local:MyControl.MyFoo}">
<!-- ... -->
</ContentControl>
<!-- ... -->
<Style TargetType="ContentControl">
<Setter Property="Content" Value="{TemplateBinding local:MyControl.MyFoo}" />
<!-- ... -->
If you receive this message:
'MyFoo' member is not valid because it does not have a qualifying type name
The key is to use TemplateBinding and prefix your property with the type name (like I did: 'local:MyControl.MyFoo')
精彩评论