我想检查一下mouseOver对wpf控件的影响。在我的代码中,它仅适用于textblock,而不适用于其他文本块。代码正确,不会生成错误。我在哪里可以获得触发器的属性值?
<Window x:Class="RoutedEventPr.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid Width="200" Height="100" Background="AliceBlue" >
<Grid.Style>
<Style TargetType="{x:Type Grid}">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="Red"/>
</Trigger>
</Style.Triggers>
</Style>
</Grid.Style>
<Ellipse Fill="#FF3AB0B0" StrokeThickness="4" Width="200" Height="100">
<Ellipse.Effect>
<DropShadowEffect/>
</Ellipse.Effect>
<Ellipse.Style>
<Style TargetType="{x:Type Ellipse}">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Fill" Value="Red"/>
</Trigger>
</Style.Triggers>
</Style>
</Ellipse.Style>
</Ellipse>
<TextBlock Text="Press" HorizontalAlignment="Center" VerticalAlignment="Center">
<TextBlock.Style>
<Style TargetType="{x:Type TextBlock}">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="Red"/>
</Trigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</Grid>
答案 0 :(得分:1)
因为TextBlock
是您不必手动设置初始值的唯一控件,就像Background
针对Grid
和Fill
针对{{1}设置的那样}}。如果您希望Ellipse
能够覆盖默认值,则需要将其Trigger
作为Style
Setter
<Grid Width="200" Height="100" >
<Grid.Style>
<Style TargetType="{x:Type Grid}">
<Setter Property="Background" Value="AliceBlue"/>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="Red"/>
</Trigger>
</Style.Triggers>
</Style>
</Grid.Style>
<Ellipse StrokeThickness="4" Width="200" Height="100">
<Ellipse.Effect>
<DropShadowEffect/>
</Ellipse.Effect>
<Ellipse.Style>
<Style TargetType="{x:Type Ellipse}">
<Setter Property="Fill" Value="#FF3AB0B0"/>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Fill" Value="Red"/>
</Trigger>
</Style.Triggers>
</Style>
</Ellipse.Style>
</Ellipse>
<TextBlock Text="Press" HorizontalAlignment="Center" VerticalAlignment="Center">
<TextBlock.Style>
<Style TargetType="{x:Type TextBlock}">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="Red"/>
</Trigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</Grid>
遵循源层次结构,手动值优先于DependencyProperty
。您可以在Dependency Property Setting Precedence List