首页 > 其他分享 >WPF中阴影效果和模糊效果的使用【Xaml】

WPF中阴影效果和模糊效果的使用【Xaml】

时间:2024-03-24 18:55:05浏览次数:17  
标签:效果 Xaml 模糊 Effect 阴影 WPF 属性

原文:https://blog.csdn.net/qq_39847278/article/details/129707074

前言

WPF中的控件效果主要通过Effect来实现,而Effect有DropShadowEffect(投影效果)和BlurEffect(模糊效果)两个派生类,本文将主要介绍Effect的运用!

一、DropShadowEffect

1、DropShadowEffect各属性效果图

 另外还有两个常用的属性,没有在上图介绍

  • Color 属性 设置阴影的颜色
  • Opacity 属性 设置阴影的透明度

2、实现代码

    <UniformGrid Background="LightGreen" Columns="4">
        <StackPanel Margin="20">
            <TextBlock Text="Direction=0" Margin="0,20">
                <TextBlock.Effect>
                    <DropShadowEffect Color="Black" Direction="0" ShadowDepth="10"/>
                </TextBlock.Effect>
            </TextBlock>
            <TextBlock Text="Direction=90" Margin="0,20">
                <TextBlock.Effect>
                    <DropShadowEffect Color="Black" Direction="90" ShadowDepth="10"/>
                </TextBlock.Effect>
            </TextBlock>
            <TextBlock Text="Direction=180" Margin="0,20">
                <TextBlock.Effect>
                    <DropShadowEffect Color="Black" Direction="180" ShadowDepth="10"/>
                </TextBlock.Effect>
            </TextBlock>
            <TextBlock Text="Direction=270" Margin="0,20">
                <TextBlock.Effect>
                    <DropShadowEffect Color="Black" Direction="270" ShadowDepth="10"/>
                </TextBlock.Effect>
            </TextBlock>
            <TextBlock Text="Direction=360" Margin="0,20">
                <TextBlock.Effect>
                    <DropShadowEffect Color="Black" Direction="360" ShadowDepth="10"/>
                </TextBlock.Effect>
            </TextBlock>

        </StackPanel>
        <StackPanel Margin="20" Background="White">
            <TextBlock Text="ShadowDepth=0" Margin="0,20">
                <TextBlock.Effect>
                    <DropShadowEffect Color="Red" Direction="0" ShadowDepth="0"/>
                </TextBlock.Effect>
            </TextBlock>
            <TextBlock Text="ShadowDepth=20" Margin="0,20">
                <TextBlock.Effect>
                    <DropShadowEffect Color="Red" Direction="0" ShadowDepth="20"/>
                </TextBlock.Effect>
            </TextBlock>
            <TextBlock Text="ShadowDepth=40" Margin="0,20">
                <TextBlock.Effect>
                    <DropShadowEffect Color="Red" Direction="0" ShadowDepth="40"/>
                </TextBlock.Effect>
            </TextBlock>
            <TextBlock Text="ShadowDepth=80" Margin="0,20">
                <TextBlock.Effect>
                    <DropShadowEffect Color="Red" Direction="0" ShadowDepth="80"/>
                </TextBlock.Effect>
            </TextBlock>
            
        </StackPanel>
        <StackPanel>
            <Border Height="80" Width="200" CornerRadius="25" Background="Green" Margin="0,20">
                <Border.Effect>
                    <DropShadowEffect Color="Blue" Direction="0" ShadowDepth="0" BlurRadius="20"/>
                </Border.Effect>
                <TextBlock Text="Direction=0 ShadowDepth=0 BlurRadius=20" FontSize="14" VerticalAlignment="Center" TextWrapping="Wrap" Width="150"></TextBlock>
            </Border>

            <Border Height="80" Width="200" CornerRadius="25" Background="Green" Margin="0,20">
                <Border.Effect>
                    <DropShadowEffect Color="Blue" Direction="0" ShadowDepth="0" BlurRadius="40"/>
                </Border.Effect>
                <TextBlock Text="Direction=0 ShadowDepth=0 BlurRadius=40" FontSize="14" VerticalAlignment="Center" TextWrapping="Wrap" Width="150"></TextBlock>
            </Border>

            <Border Height="80" Width="200" CornerRadius="25" Background="Green" Margin="0,20">
                <Border.Effect>
                    <DropShadowEffect Color="Blue" Direction="0" ShadowDepth="10" BlurRadius="60"/>
                </Border.Effect>
                <TextBlock Text="Direction=0 ShadowDepth=10 BlurRadius=60" FontSize="14" VerticalAlignment="Center" TextWrapping="Wrap" Width="150"></TextBlock>
            </Border>

            <Border Height="80" Width="200" CornerRadius="25" Background="Green" Margin="0,20">
                <Border.Effect>
                    <DropShadowEffect Color="Blue" Direction="0" ShadowDepth="100" BlurRadius="120"/>
                </Border.Effect>
                <TextBlock Text="Direction=0 ShadowDepth=100 BlurRadius=120" FontSize="14" VerticalAlignment="Center" TextWrapping="Wrap" Width="150"></TextBlock>
            </Border>
        </StackPanel>
    </UniformGrid>

二、BlurEffect

1、BlurEffect各属性效果图

 

2、实现代码

    <UniformGrid Background="LightGreen" Columns="3">
        <StackPanel Margin="20">
            <TextBlock Text="Radius=0   KernelType=Box" Margin="0,10">
                <TextBlock.Effect>
                    <BlurEffect Radius="0"  KernelType="Box"/>
                </TextBlock.Effect>
            </TextBlock>
            <TextBlock Text="Radius=1   KernelType=Box" Margin="0,10">
                <TextBlock.Effect>
                    <BlurEffect Radius="1"  KernelType="Box"/>
                </TextBlock.Effect>
            </TextBlock>
            <TextBlock Text="Radius=2   KernelType=Box" Margin="0,10">
                <TextBlock.Effect>
                    <BlurEffect Radius="2"  KernelType="Box"/>
                </TextBlock.Effect>
            </TextBlock>
        </StackPanel>
        <StackPanel Margin="20">
            <TextBlock Text="Radius=0   KernelType=Gaussian" Margin="0,10">
                <TextBlock.Effect>
                    <BlurEffect Radius="0"  KernelType="Gaussian"/>
                </TextBlock.Effect>
            </TextBlock>
            <TextBlock Text="Radius=1   KernelType=Gaussian" Margin="0,10">
                <TextBlock.Effect>
                    <BlurEffect Radius="1"  KernelType="Gaussian"/>
                </TextBlock.Effect>
            </TextBlock>
            <TextBlock Text="Radius=2   KernelType=Gaussian" Margin="0,10">
                <TextBlock.Effect>
                    <BlurEffect Radius="2"  KernelType="Gaussian"/>
                </TextBlock.Effect>
            </TextBlock>
        </StackPanel>

    </UniformGrid>

三、进阶使用

详情可查:[WPF] 使用 Effect 玩玩阴影、内阴影、 长阴影

四、注意事项

1、 由于使用Effect 导致字体模糊

 

        <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" Orientation="Horizontal">

            <Border Width="200" Height="100" Background="#5Eb978">
                <Border.Effect>
                    <DropShadowEffect BlurRadius="10" ShadowDepth="0"/>
                </Border.Effect>
                <TextBlock Text="这个是错误示范" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="25"></TextBlock>
            </Border>

            <Grid Width="200" Height="100" Margin="30,0,0,0">
                <Border Width="200" Height="100" Background="#5Eb978">
                    <Border.Effect>
                        <DropShadowEffect BlurRadius="10" ShadowDepth="0"/>
                    </Border.Effect>
                </Border>
                <TextBlock Text="正确示范" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="25"></TextBlock>
            </Grid>


        </StackPanel>

提示:当对Border 使用 Effect 的时候,最好不要在Border 内部添加TextBlock等展示元素,否则会造成 TextBlock 模糊。

标签:效果,Xaml,模糊,Effect,阴影,WPF,属性
From: https://www.cnblogs.com/cdaniu/p/18092817

相关文章

  • 【WPF应用11】如何对StackPanel中的控件进行间距设置?
    在WPF中,堆叠面板(StackPanel)是一个常用的布局控件,它允许您将子控件垂直或水平堆叠起来。在设计用户界面时,合理的间距设置可以提高界面的美观性和易用性。本文将介绍如何在StackPanel控件中设置控件之间的间距,以及如何使用Grid布局控件在X轴和Y轴上设置间距。1、在StackPanel......
  • 【WPF应用10】基本控件-StackPanel:布局原理与实际应用
    在WindowsPresentationFoundation(WPF)中,布局是用户界面设计的核心部分,它决定了控件如何排列和空间如何分配。WPF提供了一系列布局面板(Panel),以便开发者可以根据需要灵活地组织控件。在这些面板中,StackPanel是一个常用的布局控件,它按照子元素的顺序将它们堆叠起来。本文将深......
  • C# wpf 实现自定义撤销重做功能
    新建wpf项目,新建Undoable.cs(操作记录),main.xaml(页面)usingSystem;usingSystem.Collections.Generic;namespaceWpfApp1{///<summary>///撤销重做对象///ceatebyxin2022.6.26///2023.9.13去除Step的定义替换为KeyValuePair简化实现///</summ......
  • wpf datatemplate
    //xaml<Windowx:Class="WpfApp13.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.mic......
  • 文字下滑线渐变效果
    实现效果实现代码<p>Loremipsumdolorsitametconsecteturadipisicingelit.<a>Mollitianostrumplaceatconsequaturdeseruntvelitducimuspossimuscommoditemporibusdebitisquam</a>,molestiaelaboriosamsitrepellendussed......
  • wpf add resource dynamically in cs file
    //xaml<Windowx:Class="WpfApp12.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.mic......
  • WPF canvas draw lines via brush
    //xaml<Windowx:Class="WpfApp11.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.mic......
  • WPF中Image控件的绑定
    转载自:https://www.cnblogs.com/seekdream/p/5277237.html背景在我们平时的开发中会经常用到Image控件,通过设置Image控件的Source属性,我们可以加载图片,设置Image的source属性时可以使用相对路径也可以使用绝对路径,一般情况下建议使用绝对路径,类似于下面的形式 1......
  • WPF initialization for opening and unitialization for closing process
    //xaml<Windowx:Class="WpfApp10.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.mic......
  • Vue3 - Element Plus 下拉选择器 el-select 覆盖修改 placeholder样式,解决覆盖不生效
    前言如果需要Vue2版本,请访问这篇文章。本文实现了在vue3+element-plus网站开发中,完美覆盖el-select选择器样式,强力修改select下拉选择框placeholder样式,同时也支持修改文字、大小、边框、等,支持任意样式的覆盖修改!网上的教程几乎都不生效,使用本教程的方法......