使用 DependencyProperty 将 ImageBrush 绑定到模板
我正在尝试创建一个特殊的按钮,根据系统的前景色为图像着色。解决方案似乎是使用图像作为不透明蒙版来获取颜色,当我像这样直接设置图像时它会起作用:
<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 进行模板化:
public static readonly DependencyProperty ImageProperty =
DependencyProperty.Register("Image", typeof(ImageSource),
typeof(RButton), null);
然后在 XAML 中像这样:
<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 没问题,因为我测试了将其绑定到图像,而不是像这样
<Image Source="{TemplateBinding Image}" Width="48" Height="48" />
有什么想法吗?我的直觉表明我如何定义 DependecyProperty,但我不知道如何继续前进。
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>
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);
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>
I get an error saying:
object of type 'System.Windows.CustomDependencyProperty'
cannot be converted to type 'System.Windows.DependencyProperty'
The ImageProperty is ok, as I tested binding it to an image instead like this
<Image Source="{TemplateBinding Image}" Width="48" Height="48" />
Any ideas? My hunch says its in how I define my DependecyProperty, but I don't know how to move forward.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
ImageBrush 不继承自 FrameworkElement,因此它不能是 TemplateBound 或 Data Bound。
The ImageBrush doesn't inherit from FrameworkElement so it can't be TemplateBound or Data Bound.
我尝试根据您提供的 XAML 在测试项目中复制您的问题,并且不得不承认我没有遇到您所描述的问题。如果您可以提供 RButton 的代码以及有关如何使用该控件的一些上下文,我可能会更幸运(或缺乏!)。
有一篇很棒的博客文章 有关 Silverlight 中的依赖属性WP7 今天发布,您可能会觉得有用。
更新:我刚刚阅读了这篇完美匹配的 文章 解释了问题充分并给出了解决方案。基本上,
ImageBrush
不是FrameworkElement
,因此您不能将TemplateBinding
与它一起使用。I tried to replicate your problem in a test project based on the XAML you've given and have to admit that I haven't run into the problem that you describe. If you could provide the code for the
RButton
and some context as to how the control is used I might have more luck (or lack of!).There was a great blog post All about Dependency Properties in Silverlight for WP7 posted today that you might find useful.
UPDATE: I just read this perfectly matched article that explains the problem fully and gives a solution. Basically,
ImageBrush
isn't aFrameworkElement
, so you can't use theTemplateBinding
with it.我将其标记为答案,尽管它不是完整的答案。为了使其全部正常工作,我必须进行一些调整,因为我无法在 TemplateBinding 中使用转换器,叹息......无论如何。这是为我解决此问题的代码以及 文章中的转换器< /a> 德里克指出的。
因此,要将我的 Image DependencyProperty 绑定为模板中的图像画笔,我必须执行此操作...
并不明显,但它确实解决了问题,这为我解决了很多问题。感谢您的帮助
I marked It as answer although it wasn't the full answer. To get it all working I had to do some tweaking as I can't use a Converter in a TemplateBinding, sigh... anyway. Here is the code that solved it for me together with the converter in the article that Derek pointed out.
So to bind my Image DependencyProperty as an image brush in my Template I have to do this...
Not obvious but it did the trick, this has solved a lot for me. Thanks for the help
您可以使用
RelativeSource TemplatedParent
绑定代替 TemplateBinding。这基本上是同一件事,但它会在 TemplateBinding 不起作用的奇怪情况下起作用。You can use a
RelativeSource TemplatedParent
binding instead of TemplateBinding. This is basically the same thing, but it will work in those odd cases where the TemplateBinding does not.另一种方法,但不需要转换器
//或者
Another approach but without need of converter
//Or
或者,如果您是控件的作者,则在 OnApplyTemplate 中找到模板中的视觉画笔并设置其值。
Or if you are author of control then OnApplyTemplate, find the visual brush in template and set its value.