如何在WPF中将效果应用于边框而不是其内容?

12
我有一个WPF应用程序,其中包含一个带有边框的第三方数据网格。我使用了DropShadowEffect在边框后面添加阴影,但这似乎会影响性能(远不及BitmapEffect那么多,但仍然可以注意到),并使字体渲染变得模糊。有没有办法将效果应用于边框而不是其内容?
我尝试在内容上设置Effect为{x:Null},但没有帮助。
以下是我想出的一个示例应用程序。它在边框后面放置阴影,但也在每行文本后面放置阴影。我想要边框后面的阴影,但不需要文本。
<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>
3个回答

19

在 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>

5
一种简单(hack?)的解决方案是执行:
<StackPanel Background="White">

这应该解决带有阴影问题的文本(不确定性能问题)。 问题在于WPF将效果应用于设置元素及其在视觉树中的所有子级。 此链接更好地解释了这一点: DropShadowEffect性能问题


-2
尝试使用以下代码块(或类似代码)来处理所有TextBlock:
<TextBlock>
    <TextBlock.Effect>
        <DropShadowEffect BlurRadius="30" ShadowDepth="5" Color="White"/>
    </TextBlock.Effect>
</TextBlock>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接