WPF draw Border on MouseOver

tcables picture tcables · Jun 15, 2010 · Viewed 10.6k times · Source

I have a WPF Application where I am trying to make a close button like image. I am trying to draw a border or have a border show up on mouse over... But I cannot seem to make it work. I have tried like 6 different methods of doing so...Images, borders with images, brushes, ect.

I am using the following code at the moment:

<Canvas Name="cMin" Height="16" Width="16"
        Grid.Column="1" Grid.Row="1">
    <Canvas.Background>
        <ImageBrush ImageSource="_.png" Stretch="None" />
    </Canvas.Background>
    <Border BorderBrush="Transparent" BorderThickness="1" Background="Transparent" 
            CornerRadius="0" Height="18" Width="18">
         <Border.Style>
            <Style TargetType="Border">
                <Style.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="BorderBrush" Value="LightBlue" />
                    </Trigger>
                    <Trigger Property="IsMouseOver" Value="False">
                        <Setter Property="BorderBrush" Value="Transparent" />
                    </Trigger>
                </Style.Triggers>
            </Style>
        </Border.Style>
    </Border>
</Canvas>

Any help would be appreciated, Thanks!

Answer

Abe Heidebrecht picture Abe Heidebrecht · Jun 15, 2010

The problem is that your local value for the BorderBrush property is taking precedence over your Style Trigger. This MSDN article describes how the effective value is resolved. Basically, remove the local value from the Border element, and it should work. If you need to specify the property, you can do so in a Setter in the Style. Also, the second Trigger is not needed, as the value will revert to the original value when the property switches back to false:

<Border BorderThickness="1" Background="Transparent" 
        CornerRadius="0" Height="18" Width="18">
     <Border.Style>
        <Style TargetType="Border">
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="BorderBrush" Value="LightBlue" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </Border.Style>
</Border>