如何在 WPF 中的图像周围放置边框? [英] How do I put a border around an image in WPF?

查看:44
本文介绍了如何在 WPF 中的图像周围放置边框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含五个图像的 StackPanel,我想在每个图像周围放置一个黑色边框.

I have a StackPanel containing five images and I want to put a black border around each image.

我目前拥有的 XAML 是:

The XAML I have at the moment is:

<Image Name="imgPic1"
       Width="100"
       Height="75"
       Stretch="Fill"
       VerticalAlignment="Top" />

我以为我只能在图像上放置一个单位的边距或填充并将背景颜色设置为 000000PaddingBackground 对图片都无效.

I thought I would be just able to put a one-unit margin or padding on the image and set a background color to 000000 but Padding and Background are both invalid for images.

在 XAML 中执行此操作的简单方法是什么?我真的必须将每个图像放在另一个控件中才能在它周围设置边框还是我可以使用其他一些技巧?

What is an easy way to do this in XAML? Do I really have to put each image inside another control to get a border around it or is there some other trickery I can use?

推荐答案

只需将图像包裹在边框控件中

Simply wrap the Image in a Border control

<Border BorderThickness="1">
    <Image Name="imgPic1"
           Width="100"
           Height="75"
           Stretch="Fill"
           VerticalAlignment="Top" />
</Border>

如果您不想在每个图像周围都这样做,您还可以提供一种应用于图像的样式

You could also provide a style you apply to images that does this if you don't want to do it around every image

来自 Pax 添加的答案和评论的最终解决方案:

Final solution from answer and comments added by Pax:

<Border BorderThickness="1"
        BorderBrush="#FF000000"
        VerticalAlignment="Top">
    <Image Name="imgPic1"
           Width="100"
           Height="75"
           Stretch="Fill"
           VerticalAlignment="Top"/>
</Border>

这篇关于如何在 WPF 中的图像周围放置边框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆