【问题标题】:How do I apply an effect to a Border but not to its contents in WPF?如何将效果应用于边框但不应用于 WPF 中的内容?
【发布时间】:2010-10-22 21:17:30
【问题描述】:

我有一个 WPF 应用程序,它有一个带有边框的第 3 方数据网格。我使用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>

【问题讨论】:

    标签: wpf border


    【解决方案1】:

    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>
    

    【讨论】:

      【解决方案2】:

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

      <StackPanel Background="White">
      

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

      【讨论】:

        【解决方案3】:

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2020-11-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-10-12
          • 2011-01-06
          • 2020-06-29
          相关资源
          最近更新 更多