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?
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.
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.
精彩评论