How do I apply an effect to a Border but not to its contents in WPF?

前端 未结 3 1762
栀梦
栀梦 2020-12-10 04:54

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 se

相关标签:
3条回答
  • 2020-12-10 04:55

    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

    0 讨论(0)
  • 2020-12-10 05:14

    Try the following block (or similar) for all TextBlocks:

    <TextBlock>
        <TextBlock.Effect>
            <DropShadowEffect BlurRadius="30" ShadowDepth="5" Color="White"/>
        </TextBlock.Effect>
    </TextBlock>
    
    0 讨论(0)
  • 2020-12-10 05:21

    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.

    <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>
    
    0 讨论(0)
提交回复
热议问题