开发者

WPF : Add border style to Image programmatically

开发者 https://www.devze.com 2022-12-21 11:30 出处:网络
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.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号