我正在尝试创建一个特殊的按钮,根据系统的前景色为图像着色。解决方案似乎是使用图像作为不透明蒙版来获取颜色,当我像这样直接设置图像时它会起作用:
<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" />
有任何想法吗?我的直觉表明我如何定义 DependencyProperty,但我不知道如何继续。