我想在控件模板內使用GeometryDrawing作爲按鈕圖標,並且我想將其Brush
屬性綁定到按鈕的Foreground
屬性,但它不起作用。另一方面,如果我明確地設置了畫筆,它就可以工作。TemplateBinding在按鈕樣式中的DrawingImage中不工作
這裏是我的(非工作)代碼:
<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>
那麼,對於一些奇怪的原因,'刷= 「{Binding Foreground,RelativeSource = {RelativeSource TemplatedParent}}」'工作。在其他答案中這是相當有據可查的。至少有人能詳細說明這一點嗎?我很樂意接受一些有用的信息...... – heltonbiker