The summary of my problem: I had created a UserControl where I bound my dependency property "Scheme" to the Color property of a GradientStop, and it worked perfectly. Now, after converting my UserControl to a custom control, this binding no longer works, and I don't know why.
EDIT: Replication of Custom Control Problem: http://www.megaupload.com/?d=0006XVYD
EDIT: An Equivalent User Control where Binding Works: http://www.megaupload.com/?d=W13GTD4E
This is how I declared my resource dictionaries for my UserControl in the UserControl1.xaml file.
In UserControl1.xaml
<!-- Resources -->
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Colors.xaml"/>
<ResourceDictionary Source="Styles.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
Within Colors.xaml, I have the following:
In Colors.xaml
<GradientStopCollection x:Key="colorSchemeGradient">
<GradientStop Color="{Binding Scheme, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" Offset="0"/>
<GradientStop Color="#000000" Offset="3"/>
</GradientStopCollection>
<LinearGradientBrush x:Key="colorBrush" StartPoint="0,0" EndPoint="0,1" GradientStops="{DynamicResource colorSchemeGradient}"/>
The dependency property Scheme's binding to the color property of the GradientStop WORKS PERFECTLY, exactly how I want it to. The LinearGradientBrush uses the Scheme color to create a gradient that goes from the color of scheme to a slightly darker shade of it. The UserControl has a rectangle in it that uses the colorBrush as its background. It all works perfectly, and I love it.
Now, circumstances are making me convert this UserControl to a CustomControl. I'm doing this by taking the XAML file of my UserControl and pretty much copying all its content into Generic.xaml in the control template. After trying different things out, this has seemed to work pretty well for declaring my resource dictionaries in Generic.xaml:
In Generic.xaml
<Style TargetType="{x:Type local:MyCustomControl}">
<!-- Style Resources -->
<Style.Resources>
<!-- Resource Dictionaries -->
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/MyCustomControl;component/themes/Colors.xaml"/>
<ResourceDictionary Source="/MyCustomControl;component/themes/Styles.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Style.Resources>
.
.
.
</Style>
Now, in my Colors.xaml file, I have the following:
In Colors.xaml for my custom control
<GradientStopCollection x:Key="colorSchemeGradient">
<GradientStop Color="{Binding Path=Scheme, RelativeSource={RelativeSource FindAncestor, AncestorType=l:MyCustomControl}}" Offset="0"/>
<GradientStop Color="#000000" Offset="3"/>
</GradientStopCollection>
<!-- FOR DEBUG PURPOSES -->
<TextBlock x:Key="whoaText" Text="{Binding Path=Title, RelativeSource={RelativeSource FindAncestor, AncestorType=l:MyCustomControl}}"/>
<LinearGradientBrush x:Key="colorBrush" StartPoint="0,0" EndPoint="0,1" GradientStops="{DynamicResource colorSchemeGradient}"/>
Now, for some very frustrating reason I cannot understand, the binding to the GradientStop doesn't work anymore. There are no problems in how I have organized my resources, because that debug textblock I made binds to the Title dependency property perfectly. The GradientStop color does not bind to the Scheme dependency property and it is annoying me.
Can anyone tell me what's going on and how I can fix it?
EDIT: I have replicated my problem in a test project: http://www.megaupload.com/?d=0006XVYD
In Dictionary1.xaml look at the textblock with the x:key "text2". The colorBrush uses gradient stops from colorSchemeGradient, which uses a binding to Scheme. But that binding fails and so nothing works. If you can get that binding working, you're awesome.
EDIT: Here's an equivalent user control where the binding works: http://www.megaupload.com/?d=W13GTD4E
Why does the binding work here and not in the custom control?
Thank you VERY much,
Dalal