Tag: templatebinding

当目标是ImageBrush.ImageSource时,TemplateBinding失败

为什么在这种特定情况下, TemplateBinding似乎失败了? 拿一个基本的扩展按钮: public class IconButton : Button { public ImageSource Icon { get { return (ImageSource)GetValue(IconProperty); } set { SetValue(IconProperty, value); } } public static readonly DependencyProperty ImageProperty = DependencyProperty.Register(“Icon”, typeof(ImageSource), typeof(IconButton), new PropertyMetadata(null)); public IconButton() { DefaultStyleKey = typeof(IconButton); } } 控件模板使用OpacityMask显示图标: 这无声地失败 – 控件显示为实心矩形。 如果我使用常规图像而不是ImageBrush ,则绑定成功: 如果我硬编码图像源路径,它也可以正常工作: 那么,为什么TemplateBinding在ImageBrush失败? 更新 通过演绎(并感谢克里斯的答案),可能的因素是: ImageBrushinheritance自DependencyObject而不是FrameworkElement TemplateBinding不像普通绑定那样支持隐式类型转换(即string-to-ImageSource) […]