views:

63

answers:

2

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

A: 

Can you please add a little code, like the XAML for your CustomControl so we can clearly see where the binding fails.

{enjoy}

Micael Bergeron
I edited my post to include a link to a test project for a custom control where the binding doesn't work, and I also included a link to a test user control where the binding DOES work. What's going on here? Hope you get a chance to check it out.
Dalal
+3  A: 

In your test project at http://megaupload.com/?d=0006XVYD , You seem to have DynamicResources where you need static, and forward references where not required.

In Dictionary1.xaml, change the contents to:

<!-- Color Scheme Gradient: Used in the label background and border brush.
                                SUBTLE gradient from the colorScheme color
                                to a slightly darker shade -->
    <GradientStopCollection x:Key="colorSchemeGradient">
        <GradientStop Color="{Binding Path=Scheme, RelativeSource={RelativeSource FindAncestor, AncestorType=local:CustomControl1}}" Offset="0"/>
        <GradientStop Color="#000000" Offset="3"/>
    </GradientStopCollection>

    <LinearGradientBrush x:Key="colorBrush" GradientStops="{StaticResource colorSchemeGradient}"/>

    <TextBlock x:Key="text1" Text="{Binding Path=Name, RelativeSource={RelativeSource FindAncestor, AncestorType=local:CustomControl1}}" Foreground="Blue"/>
    <TextBlock x:Key="text2" Text="This text doesn't show!  Grr..." Foreground="{StaticResource colorBrush}"/>

I also changed the text1 and text2 references in Generic.xaml.

Note the StaticResource rather than DynamicResource in a few places, as well as the reorder to remove forward references. If you read http://msdn.microsoft.com/en-us/library/ms750613.aspx , you may note that dynamicresource lookup behavior is a good bit different than static.

Note that switching to static does NOT keep the control from responding to changes to the Scheme property - the lookup of the resources is static, not the value of properties in those resources.

Philip Rieck
I changed all my DynamicResources to StaticResources and it works now, thanks.
Dalal