样式触发器不会更改行的StrokeThickness属性

时间:2014-02-12 16:22:43

标签: wpf xaml triggers styles line

我想使用触发器更改线形的 StrokeThickness 属性,但它没有被更改,我尝试使用不透明度属性并且它有效! / p>

<Window x:Class="Layout.LineTrigger"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="LineTrigger" Height="300" Width="300">
    <Window.Resources>
        <Style TargetType="{x:Type Line}">
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="StrokeThickness" Value="20"/>
                    <Setter Property="Opacity" Value=".3"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <Canvas Background="Black" Width="300" Height="300">
        <Line X1="10" Y1="10" X2="60" Y2="100" 
              Stroke="Red" StrokeThickness="1" 
              SnapsToDevicePixels="True">
        </Line>
    </Canvas>
</Window>

1 个答案:

答案 0 :(得分:1)

手动设置值时,它优先于样式触发器。将默认值设为Style Setter

<Style TargetType="{x:Type Line}">
    <Setter Property="StrokeThickness" Value="1"/>
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="StrokeThickness" Value="20"/>
            <Setter Property="Opacity" Value=".3"/>
        </Trigger>
    </Style.Triggers>
</Style>

并且不要针对Line

指定它
<Line X1="10" Y1="10" X2="60" Y2="100" Stroke="Red" SnapsToDevicePixels="True"/>

您可以在此处找到整个层次结构:Dependency Property Setting Precedence List