我想在 ControlTemplate 内使用 GeometryDrawing 作为按钮图标,并且我希望它的Brush
属性绑定到按钮的Foreground
属性,但它不起作用。另一方面,如果我明确设置画笔,它就可以工作。
这是我的(非工作)代码:
<Style x:Key="SimpleButtonStyle" TargetType="{x:Type Button}">
<Setter Property="Background" Value="LightGray"/>
<Setter Property="Foreground" Value="Black"/>
<Setter Property="BorderThickness" Value="2"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border x:Name="border"
BorderBrush="{TemplateBinding Foreground}"
BorderThickness="{TemplateBinding BorderThickness}"
Background="{TemplateBinding Background}"
SnapsToDevicePixels="true">
<StackPanel Orientation="Vertical">
<Image x:Name="buttonImage" Height="20" Width="20">
<Image.Source>
<DrawingImage>
<DrawingImage.Drawing>
<!-- line below does not work -->
<GeometryDrawing Brush="{TemplateBinding Foreground}">
<GeometryDrawing.Geometry>
<EllipseGeometry RadiusX="15" RadiusY="15"/>
</GeometryDrawing.Geometry>
</GeometryDrawing>
</DrawingImage.Drawing>
</DrawingImage>
</Image.Source>
</Image>
<ContentPresenter x:Name="contentPresenter"/>
</StackPanel>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>