Comment appliquer un effet à une bordure mais pas à son contenu dans WPF?

StackOverflow https://stackoverflow.com/questions/807420

  •  03-07-2019
  •  | 
  •  

Question

J'ai une application WPF dotée d'une grille de données tierce entourée d'une bordure. J'ai utilisé DropShadowEffect pour créer une ombre derrière la frontière, mais cela semble affecter quelque peu les performances (pas autant que le BitmapEffect , mais reste perceptible) et rend le rendu de la police est flou. Existe-t-il un moyen d'appliquer l'effet à la frontière, mais pas à son contenu?

J'ai essayé de définir l'effet sur le contenu sur {x: Null} , mais cela n'a pas aidé.

Voici un exemple d'application que j'ai créé. Il met une ombre derrière la bordure, mais il place également une ombre derrière chaque ligne de texte. Je veux l'ombre derrière la frontière, mais pas le texte.

<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>
Était-ce utile?

La solution

Le lien de gcores a eu la réponse, qui consiste à placer la bordure et son contenu dans la même grille afin que le contenu recouvre la bordure.

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

Autres conseils

Une solution simple (bidouille?) consiste à faire

<StackPanel Background="White">

Ceci devrait résoudre le texte avec un problème d'ombre portée (pas sûr du problème de performance cependant). Le problème est que WPF applique des effets à l'élément set et à tous ses enfants dans l'arborescence visuelle. Ce lien l'explique mieux: a>

Essayez le bloc suivant (ou similaire) pour tous les TextBlocks:

<TextBlock>
    <TextBlock.Effect>
        <DropShadowEffect BlurRadius="30" ShadowDepth="5" Color="White"/>
    </TextBlock.Effect>
</TextBlock>
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top