Vahid
Vahid

Reputation: 5444

Style Trigger doesn't change StrokeThickness property of a line

I want to change the StrokeThickness property of a Line shape using triggers, but it is not getting changed, I tried it with the Opacity property and it worked!

<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>

Upvotes: 0

Views: 1058

Answers (1)

dkozl
dkozl

Reputation: 33364

When you set value manually it takes priority over style trigger. Bring default value into Style as 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>

and don't specify it against the Line

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

you can find whole hierarchy here: Dependency Property Setting Precedence List

Upvotes: 1

Related Questions