让 DrawingImage 从包含 Button 继承颜色
Have DrawingImage inherit color from containing Button
我有几个具有共同样式的按钮。这个样式有一个ContentPresenter
,在按钮声明中填充了一个DrawingImage
。
当 DrawingImage
使用预定义的画笔时,这很好用。但是,我非常希望我的 DrawingImage
使用样式中使用的前景色,或者如果它在那里被覆盖,则在按钮上使用前景色。
我怎样才能做到这一点?
我现在的风格:
<UserControl.Resources>
<DrawingImage x:Key="SomeIcon">
<DrawingImage.Drawing>
<DrawingGroup>
<DrawingGroup>
<GeometryDrawing Brush="White" Geometry="PATH" />
<GeometryDrawing Brush="White" Geometry="OTHER_PATH" />
</DrawingGroup>
</DrawingGroup>
</DrawingImage.Drawing>
</DrawingImage>
<Style x:Key="MyButton" TargetType="{x:Type Button}">
<Setter Property="Foreground" Value="Blue" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<ContentPresenter/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>
<Grid>
<Button Style="{StaticResource MyButton">
<!-- I want this icon to be Blue as inherited from the style. -->
<Image Source="{StaticResource SomeIcon}"/>
</Button>
<Button Style="{StaticResource MyButton" Foreground="Red">
<!-- I want this icon be Red as overriden by the button -->
<Image Source="{StaticResource SomeIcon}"/>
</Button>
</Grid>
我只想要纯粹的 XAML 答案。实现相同目标的其他 XAML 路线也很受欢迎,例如使用 Path
而不是 DrawingImage
。
您可以将图标资源声明为 Geometry,并拥有一个带有如下路径的按钮样式:
<Window.Resources>
<Geometry x:Key="SomeIcon">M0,20 L20,0 40,20 20,40Z</Geometry>
<Style x:Key="PathButtonStyle" TargetType="Button">
<Setter Property="Foreground" Value="Blue"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<ControlTemplate TargetType="Button">
<Path Fill="{TemplateBinding Background}"
Stroke="{TemplateBinding Foreground}"
Data="{TemplateBinding Content}"
Stretch="None"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<Button Style="{StaticResource PathButtonStyle}"
Content="{StaticResource SomeIcon}"/>
</Grid>
我有几个具有共同样式的按钮。这个样式有一个ContentPresenter
,在按钮声明中填充了一个DrawingImage
。
当 DrawingImage
使用预定义的画笔时,这很好用。但是,我非常希望我的 DrawingImage
使用样式中使用的前景色,或者如果它在那里被覆盖,则在按钮上使用前景色。
我怎样才能做到这一点?
我现在的风格:
<UserControl.Resources>
<DrawingImage x:Key="SomeIcon">
<DrawingImage.Drawing>
<DrawingGroup>
<DrawingGroup>
<GeometryDrawing Brush="White" Geometry="PATH" />
<GeometryDrawing Brush="White" Geometry="OTHER_PATH" />
</DrawingGroup>
</DrawingGroup>
</DrawingImage.Drawing>
</DrawingImage>
<Style x:Key="MyButton" TargetType="{x:Type Button}">
<Setter Property="Foreground" Value="Blue" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<ContentPresenter/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>
<Grid>
<Button Style="{StaticResource MyButton">
<!-- I want this icon to be Blue as inherited from the style. -->
<Image Source="{StaticResource SomeIcon}"/>
</Button>
<Button Style="{StaticResource MyButton" Foreground="Red">
<!-- I want this icon be Red as overriden by the button -->
<Image Source="{StaticResource SomeIcon}"/>
</Button>
</Grid>
我只想要纯粹的 XAML 答案。实现相同目标的其他 XAML 路线也很受欢迎,例如使用 Path
而不是 DrawingImage
。
您可以将图标资源声明为 Geometry,并拥有一个带有如下路径的按钮样式:
<Window.Resources>
<Geometry x:Key="SomeIcon">M0,20 L20,0 40,20 20,40Z</Geometry>
<Style x:Key="PathButtonStyle" TargetType="Button">
<Setter Property="Foreground" Value="Blue"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<ControlTemplate TargetType="Button">
<Path Fill="{TemplateBinding Background}"
Stroke="{TemplateBinding Foreground}"
Data="{TemplateBinding Content}"
Stretch="None"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<Button Style="{StaticResource PathButtonStyle}"
Content="{StaticResource SomeIcon}"/>
</Grid>