user279244
user279244

Reputation: 971

WPF : Add border style to Image programmatically

I have few images in WPF. On mouse click event, I want to add a border to the image. Please tell me how to do it. Should I have to create a style element in the xaml and apply it in the code-behind?

Upvotes: 0

Views: 3353

Answers (2)

Dmitry Tashkinov
Dmitry Tashkinov

Reputation: 1994

There are a lot of ways. I recommend something like this, using xaml.

<Border BorderThickness="2">
    <Border.BorderBrush>
        <SolidColorBrush Color="LightGray" Opacity="{Binding Path=IsSelected, Converter={StaticResource BooleanToDouble}}"/>
    </Border.BorderBrush>
    <Image Source="{Binding Path=ImageUri}"/>
</Border>

DataContext of this block must have IsSelected property or something like this. Also you have to implement a IValueConverter to convert true to 1 and false to 0.

Upvotes: 1

Mike Pateras
Mike Pateras

Reputation: 15015

Just remove the image from its container, create the border, add the image as the border's child, and add the border back to the container where the image was. If you get stuck, post code and I'll help you adapt it, but it shouldn't be difficult at all. You can do it all in the code-behind.

Upvotes: 0

Related Questions