问题
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 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); }
}
回答1:
I believe I solved this problem by putting the following into my binding;
RelativeSource={RelativeSource AncestorType={x:Type Controls:ImageButton}}
回答2:
That binding is relative to the DataContext
, it should probably just be a TemplateBinding
as well.
来源:https://stackoverflow.com/questions/7198676/wpf-image-highlight-with-dropshadoweffect-cant-bind-color