如何在 WPF 中将效果应用于边框而不应用于其内容?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/807420/
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
How do I apply an effect to a Border but not to its contents in WPF?
提问by Eddie Deyo
I have a WPF application that has a 3rd party data grid with a border around it. I've used the DropShadowEffect
to put a shadow behind the border, but this seems to affect performance somewhat (not nearly as much as a BitmapEffect
, but still noticeable) and makes the font rendering fuzzy. Is there a way to somehow apply the effect to the border, but not its contents?
我有一个 WPF 应用程序,它有一个 3rd 方数据网格,周围有边框。我已经使用DropShadowEffect
将阴影放在边框后面,但这似乎会在某种程度上影响性能(几乎没有BitmapEffect
,但仍然很明显)并使字体渲染模糊。有没有办法以某种方式将效果应用于边框,而不是它的内容?
I tried setting the Effect on the contents to {x:Null}
, but that didn't help.
我尝试将内容的 Effect 设置为{x:Null}
,但这没有帮助。
Here is a sample app I came up with. It puts a shadow behind the border, but it also puts a shadow behind each line of text. I want the shadow behind the border, but not the text.
这是我想出的示例应用程序。它在边框后面放置一个阴影,但它也在每行文本后面放置一个阴影。我想要边框后面的阴影,而不是文本。
<Window x:Class="WpfEffectTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
<Border BorderBrush="Black" BorderThickness="10" CornerRadius="5" Margin="25">
<Border.Effect>
<DropShadowEffect BlurRadius="10" ShadowDepth="5" />
</Border.Effect>
<StackPanel>
<TextBlock>This is some text</TextBlock>
<TextBlock>This is some text</TextBlock>
<TextBlock>This is some text</TextBlock>
<TextBlock>This is some text</TextBlock>
<TextBlock>This is some text</TextBlock>
<TextBlock>This is some text</TextBlock>
</StackPanel>
</Border>
</Grid>
</Window>
回答by Eddie Deyo
The link from gcores had the answer, which is to put the border and its content together in the same grid so the content overlays the border.
gcores 的链接给出了答案,即将边框及其内容放在同一个网格中,以便内容覆盖边框。
<Window x:Class="WpfEffectTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
<Border BorderBrush="Black" BorderThickness="10" CornerRadius="5" Margin="25">
<Border.Effect>
<DropShadowEffect BlurRadius="10" ShadowDepth="5" />
</Border.Effect>
</Border>
<StackPanel Margin="35">
<TextBlock>This is some text</TextBlock>
<TextBlock>This is some text</TextBlock>
<TextBlock>This is some text</TextBlock>
<TextBlock>This is some text</TextBlock>
<TextBlock>This is some text</TextBlock>
<TextBlock>This is some text</TextBlock>
</StackPanel>
</Grid>
</Window>
回答by gcores
One simple (hack?) solution is to do
一个简单的(黑客?)解决方案是做
<StackPanel Background="White">
This should solve the text with drop-shadow problem (Not sure about the performance problem though). The problem is that WPF applies effects to the set element and all it's children in the visual tree. This link explains it better: DropShadowEffect performance issue
这应该可以解决带有阴影问题的文本(虽然不确定性能问题)。问题在于 WPF 将效果应用于集合元素及其在可视化树中的所有子元素。这个链接解释得更好: DropShadowEffect performance issue
回答by Zolador
Try the following block (or similar) for all TextBlocks:
为所有 TextBlock 尝试以下块(或类似块):
<TextBlock>
<TextBlock.Effect>
<DropShadowEffect BlurRadius="30" ShadowDepth="5" Color="White"/>
</TextBlock.Effect>
</TextBlock>