问题
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.
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.
How I can fix it?
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. However, that binding fails, and so nothing works. If you can get that binding working, you're awesome.
Why does the binding work here and not in the custom control?
回答1:
In your test project, 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.
来源:https://stackoverflow.com/questions/3472932/why-can-i-no-longer-bind-gradientstop-color-to-a-dependency-property-of-my-contr