WPF路径几何样式以不同的颜色 [英] WPF Path Geometry Style in different colors
问题描述
我使用路径几何组来构建替换WPF中的Button的几何:
I use a Path Geometry Group to build a geometry that replaces a Button in WPF:
<UserControl.Resources>
<ControlTemplate x:Key="bT" TargetType="Button">
<Path StrokeThickness="1.5" >
<Path.Data>
<GeometryGroup>
<EllipseGeometry Center="7,7" RadiusX="7" RadiusY="7" />
<LineGeometry StartPoint="4,4" EndPoint="10,10" />
<LineGeometry StartPoint="10,4" EndPoint="4,10" />
</GeometryGroup>
</Path.Data>
<Path.Style>
<Style TargetType="{x:Type Path}">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="False">
<Setter Property="Stroke" Value="LightGray" />
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Stroke" Value="Red" />
</Trigger>
</Style.Triggers>
</Style>
</Path.Style>
</Path>
</ControlTemplate>
</UserControl.Resources>
按钮的实现方式
<Button Template="{StaticResource ResourceKey=bT}" />
现在,我的问题是我无法以不同的颜色设置几何形状的样式.例如,我希望EllipseGeometry的行为不同于LineGeometries的行为.有办法吗?
Now my problem is that i can not style my geometries in different colors. For example i want a diffrent behaviour for my EllipseGeometry than for my LineGeometries. Is there a way to to this?
推荐答案
您可能在ControlTemplate中有几个命名的路径,将 IsMouseOver
触发器移动到 ControlTemplate.Triggers
,然后在设置器中使用 TargetName
.还要注意,ControlTemplate中的所有路径都有默认的样式.
You could have several named Paths in your ControlTemplate, move the IsMouseOver
Trigger to ControlTemplate.Triggers
and use TargetName
in the Setters. Note also that there is a default Style for all Paths in the ControlTemplate.
<ControlTemplate TargetType="Button" x:Key="bT">
<ControlTemplate.Resources>
<Style TargetType="Path">
<Setter Property="Fill" Value="Transparent"/>
<Setter Property="Stroke" Value="LightGray"/>
<Setter Property="StrokeThickness" Value="1.5"/>
</Style>
</ControlTemplate.Resources>
<Canvas>
<Path x:Name="circle">
<Path.Data>
<EllipseGeometry Center="7,7" RadiusX="7" RadiusY="7"/>
</Path.Data>
</Path>
<Path x:Name="cross">
<Path.Data>
<GeometryGroup>
<LineGeometry StartPoint="4,4" EndPoint="10,10"/>
<LineGeometry StartPoint="10,4" EndPoint="4,10"/>
</GeometryGroup>
</Path.Data>
</Path>
</Canvas>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="circle" Property="Stroke" Value="Red"/>
<Setter TargetName="cross" Property="Stroke" Value="Green"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
也可以使用
这篇关于WPF路径几何样式以不同的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!