我无法将MouseOver样式应用于ContentPresenter内的Path.
我有一个包含ContentPresenter的Button样式:
<Style x:Key="ContentButton" TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<ContentPresenter
x:Name="contentPresenter"
Content="{TemplateBinding Content}">
<ContentPresenter.Resources>
<Style TargetType="{x:Type Path}"
BasedOn="{StaticResource ContentButtonPathStyle}"/>
</ContentPresenter.Resources>
</ContentPresenter>
Run Code Online (Sandbox Code Playgroud)
这是一种风格,所以我可以在路径上有一个翻转效果:
<Style x:Key="ContentButtonPathStyle" TargetType="{x:Type Path}">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Fill" Value="#FF00FF10"/>
<Setter Property="Stroke" Value="Red"/>
<Setter Property="StrokeThickness" Value="6"/>
</Trigger>
</Style.Triggers>
<Setter Property="Stroke" Value="Red"/>
<Setter Property="Fill">
<Setter.Value>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FFB4B3E7" Offset="0"/>
<GradientStop Color="#FF0800FF" Offset="1"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)
我还有一个图标资源文件,其中包含一个包含路径的Viewbox:
<Viewbox x:Key="MyIcon">
<Grid>
<Path Data="M78,296 L37.5,306.5 45.5,354.5 123.5,343.5 z" />
</Grid>
</Viewbox>
Run Code Online (Sandbox Code Playgroud)
最后,我创建了一个按钮并将Viewbox资源分配给内容:
<Button Style="{DynamicResource ContentButton}"> …Run Code Online (Sandbox Code Playgroud) 我有一个带有ContentPresenter模板的按钮样式,我试图将路径的填充绑定到按钮的前景:
<!-- This is inside the template of a button style -->
<ContentPresenter>
<ContentPresenter.Resources>
<Style TargetType="{x:Type Path}">
<Setter Property="Fill" Value="{Binding Path=Foreground, RelativeSource={RelativeSource AncestorType=Button}}"/>
</Style>
</ContentPresenter.Resources>
</ContentPresenter>
Run Code Online (Sandbox Code Playgroud)
我也有一个没有填充集的路径,我可以在按钮中引用内容,如下所示:
<Button Style="{DynamicResource MyButtonStyle}" Content="{DynamicResource PathIcon}" Foreground="Blue"/>
Run Code Online (Sandbox Code Playgroud)
我希望按钮内的路径是蓝色的,但它不是......它不会从按钮中获取前景.
如何获取绑定到按钮颜色的路径?
谢谢!
PS:
如果我在值中加入硬编码颜色(即值="红色"),按钮内的路径为红色...所以我知道它有效......
<ContentPresenter>
<ContentPresenter.Resources>
<Style TargetType="{x:Type Path}">
<Setter Property="Fill" Value="Red"/>
</Style>
</ContentPresenter.Resources>
</ContentPresenter>
Run Code Online (Sandbox Code Playgroud)
编辑:
这是完整的Style和ControlTemplate:
<Style x:Key="Button_Style" TargetType="{x:Type Button}">
<Setter Property="Foreground" Value="{StaticResource White_Brush}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid x:Name="grid" Background="Transparent">
<ContentPresenter>
<ContentPresenter.Resources>
<Style TargetType="{x:Type Path}">
<Setter Property="Fill" Value="{Binding Path=Foreground, RelativeSource={RelativeSource AncestorType=Button}}"/>
</Style> …Run Code Online (Sandbox Code Playgroud)