WPF Image 'highlight' with DropShadowEffect can't bind color

可紊 提交于 2019-12-25 02:16:08

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!