使用 DependencyProperty 将 ImageBrush 绑定到模板 [英] Bind an ImageBrush to a template with a DependencyProperty

查看:14
本文介绍了使用 DependencyProperty 将 ImageBrush 绑定到模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个特殊按钮,根据系统的前景色为图像着色.解决方案似乎是使用图像作为不透明蒙版来获取颜色,当我像这样直接设置图像时它可以工作:

I'm trying to create a special button that colors an image based on Foreground color from the system. The solution seems to be in using the image as opacity mask to get the color and it works when I set the image directly like this:

<Grid>
  <Rectangle x:Name="ImageForeground" Height="48" Width="48" 
    Fill="{StaticResource PhoneForegroundBrush}" >
    <Rectangle.OpacityMask>
      <ImageBrush Stretch="Fill" ImageSource="/icons/play.png"/>
    </Rectangle.OpacityMask>
  </Rectangle>
</Grid>

但是一旦我尝试使用 DependencyProperty 为图像 lite 制作模板,这个:

But as soon as I try template this with a DependencyProperty for the image lite this:

public static readonly DependencyProperty ImageProperty  =
  DependencyProperty.Register("Image", typeof(ImageSource), 
                              typeof(RButton), null);  

然后在 XAML 中像这样:

And then in the XAML like this:

<Grid>
  <Rectangle x:Name="ImageForeground" Height="48" Width="48" 
    Fill="{TemplateBinding Foreground}" >
    <Rectangle.OpacityMask>
      <ImageBrush Stretch="Fill" ImageSource="{TemplateBinding Image}"/>
    </Rectangle.OpacityMask>
  </Rectangle>
</Grid>

我收到一条错误消息:

object of type 'System.Windows.CustomDependencyProperty' 
  cannot be converted to type 'System.Windows.DependencyProperty'

ImageProperty 没问题,因为我测试将它绑定到图像,而不是像这样

The ImageProperty is ok, as I tested binding it to an image instead like this

<Image Source="{TemplateBinding Image}" Width="48" Height="48" />

有什么想法吗?我的直觉告诉我如何定义我的 DependecyProperty,但我不知道如何继续前进.

Any ideas? My hunch says its in how I define my DependecyProperty, but I don't know how to move forward.

推荐答案

ImageBrush 不是从 FrameworkElement 继承的,因此它不能是 TemplateBound 或 Data Bound.

The ImageBrush doesn't inherit from FrameworkElement so it can't be TemplateBound or Data Bound.

这篇关于使用 DependencyProperty 将 ImageBrush 绑定到模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆