WPF Image 'highlight' with DropShadowEffect can't bind color
I have created a UserControl called ImageButton, and I am using a DropShadowEffect on MouseOver to show the button as 'active'. However, I cannot seem to bind the Color property of my DropShadowEffect. Could anyone suggest why this doesn't work?
XAML;
<ControlTemplate x:Key="ActiveEffectTemplate" TargetType="{x:Type开发者_如何学C Controls:ImageButton}">
<Image Name="image" Source="{TemplateBinding ImageSource}">
<Image.Effect>
<DropShadowEffect
Color="{Binding HighlightColour}"
BlurRadius="20"
ShadowDepth="0"
Opacity="1"
Direction="0"/>
</Image.Effect>
</Image>
</ControlTemplate>
Code behind;
public static readonly DependencyProperty HighlightColourProperty =
DependencyProperty.Register("HighlightColour", typeof(Color), typeof(ImageButton));
public Color HighlightColour
{
get { return (Color)GetValue(HighlightColourProperty); }
set { SetValue(HighlightColourProperty, value); }
}
I believe I solved this problem by putting the following into my binding;
RelativeSource={RelativeSource AncestorType={x:Type Controls:ImageButton}}
That binding is relative to the DataContext
, it should probably just be a TemplateBinding
as well.
精彩评论