如何在 WPF 运行时设置控件周围的边框?
我的 WPF 窗体上有一个图像控件。如何在运行时在它周围创建边框?
这是我的 XAML 代码:
<Image Margin="2.5"
Grid.Column="1" Grid.Row="0"
x:Name="Behemoth" Source="Images/Hero/Behemoth.gif" Stretch="Fill"
MouseEnter="HeroMouseEnter"
MouseLeave="HeroMouseLeave"
MouseDown="HeroMouseClick" />
另外,我想知道如何删除边框。
也许如果我更好地陈述我的问题,就会有更好的解决方案。
我有很多图片,当用户说:“嘿,给我看一下所有图片中的女人吧。”我想要一种方法来突出显示或吸引用户注意我需要他们看到的任何图像。我正在考虑添加一个边框,但对于可以更容易解决的问题来说,这可能是太多的工作。
有什么帮助吗?
I have an Image control on my WPF Form. How can I create a border around it during runtime?
Here's my XAML code:
<Image Margin="2.5"
Grid.Column="1" Grid.Row="0"
x:Name="Behemoth" Source="Images/Hero/Behemoth.gif" Stretch="Fill"
MouseEnter="HeroMouseEnter"
MouseLeave="HeroMouseLeave"
MouseDown="HeroMouseClick" />
Also, I want to know how to remove the border.
Maybe if I state my problem better there is an even better solution available.
I have many Images, and when a user says: "Hey, just show me the woman out of all the picture." I want a way to sort of highlight or draw the users attention to whatever images I need them to see. I was thinking about adding a border, but maybe that's too much work for something that can be solved easier.
Any help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尽管它在视觉上与边框非常不同,但您可以使用外部发光来表示图像的重要性。然后,您不必更改图像的父级。
或者,您可以使用自定义装饰器在图像周围放置边框。有关装饰器的详细信息可以在 msdn 上找到。
Although it's visually very different from a border, you could use an outter glow to signify the importance of the image. Then, you don't have to change the parent of the image.
Alternatively, you could use a custom Adorner to place a border around the image. Good info on Adorners can be found on msdn.
没有直接的方法可以做到这一点,因为
Border
是一个容器,因此您必须从其父级中删除Image
,然后将Border
放入> 相反,并将Image
放回Border
...另一种选择是使用模板:
当您想要在图像周围放置边框时,只需指定图像模板:
There's no straightforward way to do it, because the
Border
is a container, so you would have to remove theImage
from its parent, put theBorder
instead, and put theImage
back in theBorder
...Another option would be to use templates :
When you want to put the border around the image, just assign the template to the image :
对于您所述的需求,我建议您使用带有自定义 ItemContainerStyle 的 ListBox - 始终具有边框但仅在选择项目时才使其可见的列表框。
基本思想如下:
For your stated needs, I suggest you use a ListBox with a custom ItemContainerStyle - one that always has a border but only makes it visible if the item is selected.
Here's the basic idea: