- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我无法将 MouseOver 样式应用于 ContentPresenter 内的路径。
我有一个包含 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>
<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>
<Viewbox x:Key="MyIcon">
<Grid>
<Path Data="M78,296 L37.5,306.5 45.5,354.5 123.5,343.5 z" />
</Grid>
</Viewbox>
<Button Style="{DynamicResource ContentButton}">
<ContentPresenter Content="{DynamicResource MyIcon}"/>
</Button>
<Style x:Key="ContentButtonPathStyle" TargetType="{x:Type Path}">
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource=
{RelativeSource Mode=FindAncestor, AncestorType={x:Type Button}}, Path=IsMouseOver}" Value="True">
<Setter Property="Fill" Value="Yellow"/>
<Setter Property="Stroke" Value="Blue"/>
</DataTrigger>
<DataTrigger Binding="{Binding RelativeSource=
{RelativeSource Mode=FindAncestor, AncestorType={x:Type Button}}, Path=IsPressed}" Value="True">
<Setter Property="Fill" Value="Red"/>
<Setter Property="Stroke" Value="Black"/>
</DataTrigger>
</Style.Triggers>
<Setter Property="Fill" Value="Green"/>
<Setter Property="Stroke" Value="Red"/>
</Style>
<Viewbox Stretch="Fill" x:Shared="False" x:Key="MyIcon">
<Path StrokeThickness="6" Data="M160.26077,0.5 L196.5,36.739223 232.73923,0.5 251.12399,18.884777 214.88478,55.124001 251.12399,91.363222 232.73923,109.748 196.5,73.508779 160.26077,109.748 141.87601,91.363222 178.11522,55.124001 141.87601,18.884777 z" Stretch="Fill"/>
</Viewbox>
<Style x:Key="ContentButton" TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<ControlTemplate.Resources>
<Style TargetType="{x:Type Path}" BasedOn="{StaticResource ContentButtonPathStyle}"/>
</ControlTemplate.Resources>
<Grid Background="Transparent"><ContentPresenter /></Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Grid>
<Button Style="{DynamicResource ContentButton}" HorizontalAlignment="Left" Width="128" Height="128" VerticalAlignment="Top" Margin="85.5,87,0,0">
<ContentPresenter Content="{DynamicResource MyIcon}" d:IsLocked="True"/>
</Button>
<Button Style="{DynamicResource ContentButton}" Height="64" VerticalAlignment="Top" Margin="0,87,204.5,0" HorizontalAlignment="Right" Width="64">
<ContentPresenter Content="{DynamicResource MyIcon}" d:IsLocked="True"/>
</Button>
<Button Style="{DynamicResource ContentButton}" Height="96" VerticalAlignment="Bottom" Margin="234,0,0,66.5" HorizontalAlignment="Left" Width="96">
<ContentPresenter Content="{DynamicResource MyIcon}" d:IsLocked="True"/>
</Button>
<Button Style="{DynamicResource ContentButton}" Height="32" VerticalAlignment="Bottom" Margin="0,0,138.5,130.5" HorizontalAlignment="Right" Width="32">
<ContentPresenter Content="{DynamicResource MyIcon}" d:IsLocked="True"/>
</Button>
</Grid>
最佳答案
问题不在于“BasedOn”,您可以定义整个样式而不是使用 baseon,但它仍然不起作用。你只需要在 ControlTemplate 资源中移动你的 stile,它肯定会工作
<Style x:Key="ContentButton" TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<ControlTemplate.Resources>
<Style TargetType="{x:Type Path}" BasedOn="{StaticResource ContentButtonPathStyle}"/>
</ControlTemplate.Resources>
<ContentPresenter x:Name="contentPresenter" Content="{TemplateBinding Content}">
</ContentPresenter>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="ContentButtonPathStyle" TargetType="{x:Type Path}">
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource=
{RelativeSource Mode=FindAncestor, AncestorType={x:Type Button}}, Path=IsMouseOver}" Value="True">
<Setter Property="Fill" Value="#FF00FF10"/>
<Setter Property="Stroke" Value="Red"/>
<Setter Property="StrokeThickness" Value="6"/>
</DataTrigger>
</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>
<Style x:Key="ContentButton" TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<ContentPresenter x:Name="contentPresenter" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Viewbox x:Key="MyIcon">
<Grid Background="Transparent">
<Grid.Resources>
<Style TargetType="{x:Type Path}" BasedOn="{StaticResource ContentButtonPathStyle}"/>
</Grid.Resources>
<Path Data="M78,296 L37.5,306.5 45.5,354.5 123.5,343.5 z" />
</Grid>
</Viewbox>
<Path Style="{StaticResource ContentButtonPathStyle}" Data="...." />
关于wpf - 将样式应用于 ContentPresenter 内的路径(BasedOn 不起作用!),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8410651/
今天有小伙伴给我留言问到,try{...}catch(){...}是什么意思?它用来干什么? 简单的说 他们是用来捕获异常的 下面我们通过一个例子来详细讲解下
我正在努力提高网站的可访问性,但我不知道如何在页脚中标记社交媒体链接列表。这些链接指向我在 facecook、twitter 等上的帐户。我不想用 role="navigation" 标记这些链接,因
说现在是 6 点,我有一个 Timer 并在 10 点安排了一个 TimerTask。之后,System DateTime 被其他服务(例如 ntp)调整为 9 点钟。我仍然希望我的 TimerTas
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我就废话不多说了,大家还是直接看代码吧~ ? 1
Maven系列1 1.什么是Maven? Maven是一个项目管理工具,它包含了一个对象模型。一组标准集合,一个依赖管理系统。和用来运行定义在生命周期阶段中插件目标和逻辑。 核心功能 Mav
我是一名优秀的程序员,十分优秀!