【问题标题】:WPF Image 'highlight' with DropShadowEffect can't bind color带有 DropShadowEffect 的 WPF 图像“突出显示”无法绑定颜色
【发布时间】:2011-08-26 00:09:22
【问题描述】:

我创建了一个名为 ImageButton 的用户控件,并且我在 MouseOver 上使用 DropShadowEffect 将按钮显示为“活动”。但是,我似乎无法绑定 DropShadowEffect 的 Color 属性。谁能建议为什么这不起作用?

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>

代码隐藏;

public static readonly DependencyProperty HighlightColourProperty =
        DependencyProperty.Register("HighlightColour", typeof(Color), typeof(ImageButton));

    public Color HighlightColour
    {
        get { return (Color)GetValue(HighlightColourProperty); }
        set { SetValue(HighlightColourProperty, value); }
    }

【问题讨论】:

    标签: c# wpf xaml data-binding effects


    【解决方案1】:

    我相信我通过将以下内容放入绑定中解决了这个问题;

    RelativeSource={RelativeSource AncestorType={x:Type Controls:ImageButton}} 
    

    【讨论】:

      【解决方案2】:

      该绑定与DataContext 相关,它也应该只是TemplateBinding

      【讨论】:

      • 也试过了,但没有运气,我只是在效果中得到了默认的“灰色”颜色。
      • @RJLohan:你会在任何地方设置颜色吗? Visual Studio的输出窗口是否有绑定错误?
      • 是的,在创建对象之后,无论是在 XAML 中还是在代码中 - 都给我默认的灰色。
      • Erm...不知何故我已经让它工作了。我什至不确定我改变了什么(我已经放弃并且正在研究这个控制的其他方面......),但是在我上次评论之前进行验证时,它似乎工作...... :-|
      • 好的,我找到了解决方案;我将此行添加到我的控件的公共默认构造函数中; DataContext = this;
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-11-14
      • 1970-01-01
      • 1970-01-01
      • 2011-04-14
      • 2015-09-21
      • 2012-04-25
      • 2014-11-13
      相关资源
      最近更新 更多