带圆角的 WPF 超链接图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16521320/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF Hyperlinked Image with rounded corners
提问by Alexander Bell
Question: How to create hyperlinked Image with rounded corners in WPF/XAML?
问题:如何在 WPF/XAML 中创建带圆角的超链接图像?
So far, existing code for hyperlinked image (no rounded corners) is working (see below):
到目前为止,超链接图像(无圆角)的现有代码正在运行(见下文):
Hyperlinked Image (WPF XAML)
超链接图像 (WPF XAML)
<TextBlock Name="txtbFooterRight" >
<Hyperlink Name="lnkImg" TextDecorations="None"
NavigateUri="http://webinfocentral.com"
ToolTip="Navigate to web page">
<Image Name="someName" Source="some url" />
</Hyperlink>
</TextBlock>
Hyperlinked image code behind (C#):
背后的超链接图像代码(C#):
lnkImg.RequestNavigate += (s, e) => {Process.Start(e.Uri.ToString()); };
Image control with rounded corners (no hyperlinks) is implemented as:
带圆角(无超链接)的图像控制实现为:
Image with rounded corners (WPF/XAML):
带圆角的图像 (WPF/XAML):
<Border Name="brdRounded" BorderThickness="0" CornerRadius="10">
<Border.Background >
<ImageBrush>
<ImageBrush.ImageSource>
<BitmapImage UriSource="some Uri to .jpg" />
</ImageBrush.ImageSource>
</ImageBrush>
</Border.Background>
</Border>
I need to "round the corners" of the hyperlinked image (WPF/XAML), probably combining aforementioned techniques. Thanks and regards,
我需要“绕过”超链接图像(WPF/XAML),可能结合上述技术。感谢致敬,
Note: I've accepted the answer posted by user @lisp with just minor fix: Border background color should match the surrounding color in order to avoid slight "color leakage". Kudos to the author!
注意:我已经接受了用户@lisp 发布的答案,只是做了一些小修正:边框背景颜色应该与周围的颜色相匹配,以避免轻微的“颜色泄漏”。为作者点赞!
On a separate note: it was an eye-opening experience on how relatively difficult it is to achieve such simple effect while using WPF/XAML comparing to HTML5/CSS3 (see for, example, essentially the same effect on rounded corner image at: http://infosoft.biz/SlideShowCSS.aspx). It seems like WPF folks at Microsoft should take a note...
另请注意:与 HTML5/CSS3 相比,使用 WPF/XAML 实现如此简单的效果是多么困难,这是一个令人大开眼界的体验(例如,圆角图像上的效果基本相同,网址为:http ://infosoft.biz/SlideShowCSS.aspx)。似乎微软的 WPF 人员应该注意...
回答by lisp
Border is used for rounded corners. But in your case if you simply put TextBlock inside of Border, you wouldn't get the desired effect. Here Corners are made transparent using a border. Grid is used so that Border stretches exactly to the size of TextBlock.
边框用于圆角。但是在您的情况下,如果您只是将 TextBlock 放在 Border 内,则不会获得预期的效果。这里使用边框使角落变得透明。使用 Grid 以便 Border 精确地延伸到 TextBlock 的大小。
<Grid>
<Border Name="CornersMask" Background="White" CornerRadius="20"/>
<TextBlock>
<TextBlock.OpacityMask>
<VisualBrush Visual="{Binding ElementName=CornersMask}"/>
</TextBlock.OpacityMask>
<Hyperlink ...>
<Image Name="someName" Source="some url" />
</Hyperlink>
</TextBlock>
</Grid>
TextBlock is displayed on top of Border, and because of that and antialiasing you may experience slight whiteness on the rounded edges. Either change the Border background to the surrounding background color, or enclose Border in another container that will autostretch it e.g. Border of Grid, and set it's Visibility to Hidden.
TextBlock 显示在 Border 的顶部,因此和抗锯齿,您可能会在圆角边缘看到轻微的白色。要么将边框背景更改为周围的背景颜色,要么将边框包含在另一个容器中,该容器将自动拉伸它,例如网格边框,并将其可见性设置为隐藏。
<Border Visibility="Hidden">
<Border Name="CornersMask" Background="White" CornerRadius="20"/>
</Border>
This also solves the problem when the surrounding background is not a SolidColorBrush
这也解决了周围背景不是 SolidColorBrush 时的问题

