tags:

views:

327

answers:

2

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

A: 

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.

Mike Pateras
Thanks mike, but it will create problem elsewhere in my code. Is there a way that I can make the border the child of Image?
+1  A: 

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.

Dmitry Tashkinov
That is a much more elegant solution.
Mike Pateras
Great idea, yet I'm missing an element that has a IsSelected property or something like this. Any idea? Currently I have the Border and inside the Border I have an Image, however both are lacking something like IsSelected ):
stefan.at.wpf
@stefan, IsSelected is a property of the data context, which is a model layer object designed by yourself (read about MVVM or MVC patterns).
Dmitry Tashkinov