views:

163

answers:

3

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:

http://stackoverflow.com/questions/3472932/why-can-i-no-longer-bind-gradientstop-color-to-a-dependency-property-of-my-contro

Thanks everybody who helped,

Dalal

A: 

You can't databind to a GradientStop, see http://stackoverflow.com/questions/1513892/how-to-bind-gradientstop-colours-or-gradientstops-property-in-silverlight

Wallstreet Programmer
That's not necessarily true. Before I converted my project to a CustomControl from a UserControl (in order to get rid of the XAML file), the GradientStop binding was working perfectly, using the same code above. Perhaps it doesn't work in Silverlight, but works in WPF, I don't know.
Dalal
A: 

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"/>  
HCL
Oh yeah, oops. Thank you. I can now use my control as the ancestor type, but the binding still doesn't seem to be working. It's not because you can't databind to a GradientStop, because as I said in my comment to Wallstreet Programmer, it works in the UserControl version of my control. I might try putting the binding in Generic.xaml instead of in another file. But I don't want to do that. If you have any other solutions that would be great.
Dalal
You answered part of my initial question, so I went ahead and marked yours as the correct one.
Dalal
+1  A: 

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?

John Bowen
Thanks John. You're right. This seems to be a different problem than what I thought. I posted some specifics here: http://stackoverflow.com/questions/3472932/why-can-i-no-longer-bind-gradientstop-color-to-a-dependency-property-of-my-contro
Dalal