Silverlight 将ImageBrush绑定到具有DependencyProperty的模板

Silverlight 将ImageBrush绑定到具有DependencyProperty的模板,silverlight,xaml,windows-phone-7,Silverlight,Xaml,Windows Phone 7,我正在尝试创建一个特殊的按钮,该按钮根据系统中的前景颜色为图像着色。解决方案似乎是使用图像作为不透明度遮罩来获得颜色,当我直接像这样设置图像时,它会起作用: <Grid> <Rectangle x:Name="ImageForeground" Height="48" Width="48" Fill="{StaticResource PhoneForegroundBrush}" > <Rectangle.OpacityMask>

我正在尝试创建一个特殊的按钮,该按钮根据系统中的前景颜色为图像着色。解决方案似乎是使用图像作为不透明度遮罩来获得颜色,当我直接像这样设置图像时,它会起作用:

<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>
<Grid>
  <Rectangle x:Name="ImageForeground" Height="48" Width="48" 
    Fill="{TemplateBinding Foreground}" >
    <Rectangle.OpacityMask>
      <ImageBrush Stretch="Fill" ImageSource="{TemplateBinding Image}"/>
    </Rectangle.OpacityMask>
  </Rectangle>
</Grid>
然后在XAML中,如下所示:

<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>
<Grid>
  <Rectangle x:Name="ImageForeground" Height="48" Width="48" 
    Fill="{TemplateBinding Foreground}" >
    <Rectangle.OpacityMask>
      <ImageBrush Stretch="Fill" ImageSource="{TemplateBinding Image}"/>
    </Rectangle.OpacityMask>
  </Rectangle>
</Grid>
ImageProperty还可以,因为我测试了如何将它绑定到图像,而不是像这样

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


有什么想法吗?我的直觉是我如何定义我的依赖属性,但我不知道如何前进。

我试图在一个基于您给出的XAML的测试项目中复制您的问题,并且不得不承认我没有遇到您描述的问题。如果您能提供
RButton
的代码,并提供一些关于如何使用控件的上下文,我可能会更幸运(或者缺少!)

今天有一篇很好的博客文章,你可能会觉得有用


更新:我刚刚读了这篇完美匹配的文章,它充分解释了问题并给出了解决方案。基本上,
ImageBrush
不是一个
FrameworkElement
,因此不能将
TemplateBinding
与之一起使用。

该元素不继承FrameworkElement,因此不能被模板绑定或数据绑定

我把它标为答案,虽然它不是完整的答案。为了让所有的工作,我必须做一些调整,因为我不能在模板绑定中使用转换器,叹息。。。无论如何下面是为我解决这一问题的代码,以及Derek指出的中的转换器

所以要将我的图像依赖属性绑定为模板中的图像笔刷,我必须这样做

<Grid>
  <Grid.Resources>
    <controls:ImageBrushConverter x:Key="brushConverter"/>
  </Grid.Resources>
  <Rectangle 
    x:Name="ImageForeground"
    Height="48"
    Width="48" 
    Fill="{TemplateBinding Foreground}" 
        DataContext="{TemplateBinding Image}"
        OpacityMask="{Binding Converter={StaticResource brushConverter}}">
  </Rectangle>
</Grid> 


不明显,但它确实起了作用,这对我来说解决了很多问题。感谢您的帮助

另一种方法,但不需要转换器

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

//或


或者,如果您是控件的作者,则在应用程序模板上,在模板中找到可视笔刷并设置其值

public override void OnApplyTemplate()
{
    base.OnApplyTemplate();

    VisualBrush oVBrushBack = (VisualBrush)this.Template.FindName("rectVisual", this);

    oVBrushBack.Visual = this.IconVisual;
}

您可以使用
相对资源TemplatedParent
绑定而不是TemplateBinding。这基本上是一样的,但在模板绑定不起作用的奇怪情况下,它会起作用

<ImageBrush
    ImageSource="{Binding RelativeSource={RelativeSource TemplatedParent},Path=Image}" />


感谢您添加您的解决方案,这正是我所需要的!这个答案并没有真正的帮助,因为Windows Phone Silverlight中没有“AncestorType”绑定。Silverlight中没有“VisualBrush”——这是一个WPF问题。是否有相关的来源?谁说DependencyObject的DependencyProperty不能成为模板绑定的目标?这是原始问题的最简单解决方案。
<ImageBrush
    ImageSource="{Binding RelativeSource={RelativeSource TemplatedParent},Path=Image}" />