我有一个WPF应用程序,它有一个第三方数据网格,周围有一个边框。我已经使用 DropShadowEffect 在边框后面添加了一个阴影,但这似乎有点影响性能(不像 BitmapEffect 那么多,但仍然很明显)并使字体渲染模糊。有没有办法以某种方式将效果应用于边框,而不是其内容?

我尝试将内容的效果设置为 {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>
有帮助吗?

解决方案

来自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>

其他提示

一个简单的(黑客?)解决方案是

<StackPanel Background="White">

这应解决带有阴影问题的文本(虽然不确定性能问题)。 问题是WPF将效果应用于可视树中的set元素及其所有子元素。 此链接更好地解释了它: DropShadowEffect性能问题

对所有TextBlocks尝试以下块(或类似):

<TextBlock>
    <TextBlock.Effect>
        <DropShadowEffect BlurRadius="30" ShadowDepth="5" Color="White"/>
    </TextBlock.Effect>
</TextBlock>
scroll top