wpf中的Border、Border.Effect和Background

在WPF(Windows Presentation Foundation)中,Border和Background是两个非常重要的属性,它们通常用于定义用户界面元素的外观样式。

Border

  • Border 是一个控件,它本身是一个装饰器,允许您为任何嵌套的内容添加边框。通过使用 Border 控件,您可以给内容加上具有特定颜色、宽度和圆角效果的边框。
  • 主要属性包括:
    • BorderBrush: 设置边框的颜色,可以是任意类型的 Brush 对象,例如 SolidColorBrush、LinearGradientBrush 等。
    • BorderThickness: 定义边框各边的宽度,用 Thickness 结构来指定,可以分别为四个边缘设置不同的宽度。
    • CornerRadius: 指定边框的圆角半径,可以让边框的角变得圆滑。
1<Border BorderBrush="Black" BorderThickness="2" CornerRadius="5">
2    <!-- 这里放置其他控件或内容 -->
3</Border>

在WPF(Windows Presentation Foundation)中,Border控件除了提供诸如BorderBrushBorderThicknessBackgroundCornerRadius等属性来定义边框的基本样式外,还支持使用Effect属性来添加视觉特效,如阴影、模糊、发光等。通过设置Effect属性,您可以为Border控件添加复杂的图形效果,从而增强用户界面的视觉吸引力和深度感。

特别地,您提到的Border.Effect属性允许您为Border控件指定一个System.Windows.Media.Effects.Effect类的实例。最常用的特效之一是DropShadowEffect,它用来为控件添加阴影,模拟光照下的投影效果。下面是一个使用DropShadowEffectBorder添加阴影的示例:

Xml

1<!-- 在WPF窗口或用户控件的XAML代码中 -->
2<Window ...>
3    <Grid>
4        <Border Margin="100"
5                BorderThickness="1"
6                BorderBrush="Blue">
7            <!-- 设置阴影效果 -->
8            <Border.Effect>
9                <DropShadowEffect Color="#E6EFFC"       <!-- 阴影颜色 -->
10                                  Direction="320"         <!-- 阴影方向(角度) -->
11                                  BlurRadius="10"         <!-- 模糊半径,控制阴影的扩散程度 -->
12                                  ShadowDepth="10" />    <!-- 阴影距离,控制阴影与控件边缘的距离 -->
13            </Border.Effect>
14
15            <!-- Border内的内容 -->
16            <Grid Background="White">
17                <TextBlock Text="WPF 边框投影"
18                           Margin="50"
19                           FontSize="30"/>
20            </Grid>
21        </Border>
22    </Grid>
23</Window>

在这个例子中:

  • DropShadowEffectEffect属性值的具体类型,用于生成阴影效果。
  • Color属性指定了阴影的颜色。
  • Direction属性以度为单位定义了阴影投射的方向。例如,320表示阴影朝西偏北方向投射。
  • BlurRadius属性设置了阴影边缘的模糊程度,数值越大,阴影越模糊且扩散范围越广。
  • ShadowDepth属性决定了阴影与控件主体之间的距离,即阴影看起来离控件有多远。
  • Opacity(阴影不透明度)属性与模糊效果相关,但它们描述的是不同的视觉特性:

    作用Opacity 属性应用于整个Border及其包含的内容(如果有的话),控制元素的整体透明度。它是一个介于0.0(完全透明)和1.0(完全不透明)之间的浮点数。
  • 模糊程度Opacity 不直接影响模糊程度。它不会使元素变得模糊,而是决定元素内容和背景之间以及元素与其他元素之间的混合程度。降低 Opacity 值会使Border及其中的任何内容看起来更淡,仿佛透过一层半透明材料观看。

除了DropShadowEffect,还有其他预定义的效果类,如BlurEffectGlowEffect等,以及通过实现Effect接口自定义复杂图形效果的可能性。这些特效都可以应用于Border或其他支持Effect属性的WPF控件,以丰富用户界面的视觉表现。

总之,Border.Effect属性是WPF中用于给Border控件添加图形特效的关键属性,通过设置合适的Effect实例,您可以轻松实现诸如阴影、模糊、发光等高级视觉效果,提升应用程序的外观和用户体验。

<Window x:Class="WpfApp1.BorderView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="BorderView" Height="450" Width="800">
    <Grid>
        <Border BorderBrush="red" BorderThickness="1" CornerRadius="10" >
           
            <!-- ShadowDepth 阴影与控件边缘的距离 -->
            <!-- Direction 阴影方向(角度) -->
            <!-- BlurRadius 阴影半径和扩散程度-->
            <!-- Opacity 阴影模糊程度-->
            
            <Border.Effect>
                <DropShadowEffect BlurRadius="1" ShadowDepth="200" Color="Green" Opacity ="00.5" Direction="0"/>
            </Border.Effect>
            <TextBlock Text="内层边框" HorizontalAlignment="Center" VerticalAlignment="Center"/>
        </Border>
        <Border BorderBrush="Black" BorderThickness="1" CornerRadius="10" Margin="55">
            <TextBlock Text="外层边框" HorizontalAlignment="Center" VerticalAlignment="Center"/>
        </Border>
    </Grid>
</Window>

Background

  • 在 WPF 中,几乎每个可视控件都有 Background 属性,它不是一个单独的控件,而是一个基本属性
  • Background 属性用来设置控件的背景颜色或背景图像,也可以是一个 Brush 类型的对象,如 SolidColorBrush、ImageBrush、VisualBrush 等。
  • 当应用于 Border 控件时,它将设置 Border 内部区域的填充颜色或图案。
1<!-- 设置控件背景颜色 -->
2<Button Background="AliceBlue">Click me</Button>
3
4<!-- 或者,在 Border 控件上应用背景色 -->
5<Border BorderBrush="Black" BorderThickness="1" Background="WhiteSmoke">
6    <TextBlock Text="Hello, World!"/>
7</Border>

总的来说,在设计WPF应用程序的用户界面时,Border 和 Background 都是用于增强视觉效果和布局的重要工具,前者提供了一种围绕内容绘制边框的能力,后者则负责填充控件背后的区域。

  • 9
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
using System; using System.Windows; using System.Windows.Controls; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Media.Media3D; using System.Windows.Media.Effects; namespace WPFDemo { public partial class Rotate3DContainer : Grid, Container { private Storyboard front2BackStory; private Storyboard back2FrontStory; private Border frontWarpper; private Border backWarpper; public Rotate3DContainer() { InitializeComponent(); this.Loaded += new RoutedEventHandler(Container_Loaded); } private void Container_Loaded(object sender, RoutedEventArgs e) { Init(); } private void Init() { if (this.Children.Count == 2) { UIElement[] array = new UIElement[2]; this.Children.CopyTo(array, 0); this.Children.Clear(); frontWarpper = new Border() { Child = array[0], HorizontalAlignment=HorizontalAlignment.Left, VerticalAlignment=VerticalAlignment.Top, Background = Brushes.Transparent }; backWarpper = new Border() { Child = array[1], Opacity = 0, Visibility = Visibility.Hidden, HorizontalAlignment=HorizontalAlignment.Left, VerticalAlignment=VerticalAlignment.Top, Background = Brushes.Transparent }; Viewport3D viewport = Get3DView(); viewport.HorizontalAlignment = HorizontalAlignment.Left; viewport.VerticalAlignment = VerticalAlignment.Top; viewport.Height = frontWarpper.Child.DesiredSize.Height; viewport.Width = frontWarpper.Child.DesiredSize.Width; front2BackStory = new Storyboard() { Children = new TimelineCollection() { GetShowHideAnimation(viewport, 0, 1100), GetShowHideAnimation(backWarpper, 1000, -1), GetShowHideAnimation(frontWarpper, -1, 50), GetFadeAnimation(frontWarpper, 0, -1, 50), GetFadeAnimation(backWarpper, 1, 1050, 50), GetCameraMoveAnimation(0, 0, 0.5, 0, 0, 1.1, 50, 500,viewport), GetRotateAnimation(0, -180, 0.3, 0.3, 50, 1000) } }; back2FrontStory = new Storyboard() { Children = new TimelineCollection() { GetShowHideAnimation(viewport, 0, 1100), GetShowHideAnimation(frontWarpper, 1000, -1), GetShowHideAnimation(backWarpper, -1, 50), GetFadeAnimation(backWarpper, 0, -1, 50), GetFadeAnimation(frontWarpper, 1, 1050, 50), GetCameraMoveAnimation(0, 0, 0.5, 0, 0, 1.1, 50, 500, viewport), GetRotateAnimation(180, 360, 0.3, 0.3, 50, 1000) } }; this.Effect = new DropShadowEffect() { BlurRadius = 10, Opacity = 0.8 }; this.Children.Add(frontWarpper); this.Children.Add(backWarpper); this.Children.Add(viewport); array = null; } } private DoubleAnimation GetFadeAnimation(UIElement target, int toOpacity, int beginTime, int duration) { DoubleAnimation result = new DoubleAnimation(toOpacity, new Duration(TimeSpan.FromMilliseconds(duration))); if (beginTime >= 0) { result.BeginTime = TimeSpan.FromMilliseconds(beginTime); } Storyboard.SetTarget(result, target); Storyboard.SetTargetProperty(result, new PropertyPath("Opacity")); return result; } private Point3DAnimation GetCameraMoveAnimation(double x1, double y1, double z1, double x2, double y2, double z2, int beginTime, int duration, Viewport3D view) { Point3DAnimation result = new Point3DAnimation(new Point3D(x1, y1, z1), new Point3D(x2, y2, z2), new Duration(TimeSpan.FromMilliseconds(duration))) { AutoReverse = true, BeginTime = TimeSpan.FromMilliseconds(beginTime), DecelerationRatio = 0.3 }; Storyboard.SetTarget(result, view); Storyboard.SetTargetProperty(result, new PropertyPath("Camera.Position")); return result; } private DoubleAnimation GetRotateAnimation(double fromDegree, double toDegree, double accelerationRatio, double decelerationRatio, int beginTime, int duration) { DoubleAnimation result = new DoubleAnimation(fromDegree, toDegree, new Duration(TimeSpan.FromMilliseconds(duration)), FillBehavior.HoldEnd) { AccelerationRatio = accelerationRatio, DecelerationRatio = decelerationRatio, BeginTime = TimeSpan.FromMilliseconds(beginTime) }; Storyboard.SetTargetName(result, "AxisAngleRotation3D"); Storyboard.SetTargetProperty(result, new PropertyPath("Angle")); return result; } Viewport3D Get3DView() { Viewport3D viewport = new Viewport3D() { Camera = new PerspectiveCamera(new Point3D(0, 0, 0.5), new Vector3D(0, 0, -1), new Vector3D(0, 1, 0), 90), Visibility = Visibility.Hidden }; viewport.Children.Add(GetLightVisual3D()); viewport.Children.Add(GetSceneVisual3D(frontWarpper, backWarpper)); return viewport; } ModelVisual3D GetLightVisual3D() { Model3DGroup group = new Model3DGroup(); group.Children.Add(new DirectionalLight(Color.FromRgb(0x44, 0x44, 0x44), new Vector3D(0, 0, -1))); group.Children.Add(new AmbientLight(Color.FromRgb(0xBB, 0xBB, 0xBB))); return new ModelVisual3D() { Content = group }; } ModelVisual3D GetSceneVisual3D(Border frontElement, Border backElement) { MeshGeometry3D meshgmod = new MeshGeometry3D() { TriangleIndices = new Int32Collection(new int[] { 0, 1, 2, 2, 3, 0 }), TextureCoordinates = new PointCollection(new Point[]{new Point(0, 1),new Point(1, 1),new Point(1, 0),new Point(0, 0)}), Positions = new Point3DCollection(new Point3D[]{new Point3D(-0.5, -0.5, 0),new Point3D(0.5, -0.5, 0),new Point3D(0.5, 0.5, 0),new Point3D(-0.5, 0.5, 0)}) }; VisualBrush frontBrush = new VisualBrush(frontElement.Child) { Stretch = Stretch.Uniform }; VisualBrush backBrush = new VisualBrush(backElement.Child) { Stretch = Stretch.Uniform, RelativeTransform = new ScaleTransform(-1, 1, 0.5, 0) }; AxisAngleRotation3D rotate = new AxisAngleRotation3D(new Vector3D(0, 1, 0), 0); this.RegisterName("AxisAngleRotation3D", rotate); GeometryModel3D gmode3d = new GeometryModel3D() { Geometry = meshgmod, Material = new DiffuseMaterial(frontBrush), BackMaterial = new DiffuseMaterial(backBrush), Transform = new RotateTransform3D(rotate) }; return new ModelVisual3D() { Content = gmode3d }; } ObjectAnimationUsingKeyFrames GetShowHideAnimation(UIElement element, int showTime, int hideTime) { ObjectAnimationUsingKeyFrames frame = new ObjectAnimationUsingKeyFrames(); Storyboard.SetTarget(frame, element); Storyboard.SetTargetProperty(frame, new PropertyPath("Visibility")); if (showTime >= 0) { frame.KeyFrames.Add(new DiscreteObjectKeyFrame(Visibility.Visible, TimeSpan.FromMilliseconds(showTime))); } if (hideTime >= 0) { frame.KeyFrames.Add(new DiscreteObjectKeyFrame(Visibility.Hidden, TimeSpan.FromMilliseconds(hideTime))); } return frame; } public void Turn(bool isReverse) { Storyboard target = null; DoubleAnimation direction = null; double fromAngle = 0; double step = 180; if (frontWarpper.Opacity != 0) { target = front2BackStory; } else { fromAngle = 180; target = back2FrontStory; } direction = (DoubleAnimation)target.Children[6]; if (!isReverse) { step = -180; } direction.From = fromAngle; direction.To = fromAngle + step; target.Begin(this); } } }
VS2010 部分代码: private Storyboard front2BackStory; private Storyboard back2FrontStory; private Border frontWarpper; private Border backWarpper; public Rotate3DContainer() { InitializeComponent(); this.Loaded += new RoutedEventHandler(Container_Loaded); } private void Container_Loaded(object sender, RoutedEventArgs e) { Init(); } private void Init() { if (this.Children.Count == 2) { UIElement[] array = new UIElement[2]; this.Children.CopyTo(array, 0); this.Children.Clear(); frontWarpper = new Border() { Child = array[0], HorizontalAlignment=HorizontalAlignment.Left, VerticalAlignment=VerticalAlignment.Top, Background = Brushes.Transparent }; backWarpper = new Border() { Child = array[1], Opacity = 0, Visibility = Visibility.Hidden, HorizontalAlignment=HorizontalAlignment.Left, VerticalAlignment=VerticalAlignment.Top, Background = Brushes.Transparent }; Viewport3D viewport = Get3DView(); viewport.HorizontalAlignment = HorizontalAlignment.Left; viewport.VerticalAlignment = VerticalAlignment.Top; viewport.Height = frontWarpper.Child.DesiredSize.Height; viewport.Width = frontWarpper.Child.DesiredSize.Width; front2BackStory = new Storyboard() { Children = new TimelineCollection() { GetShowHideAnimation(viewport, 0, 1100), GetShowHideAnimation(backWarpper, 1000, -1), GetShowHideAnimation(frontWarpper, -1, 50), GetFadeAnimation(frontWarpper, 0, -1, 50), GetFadeAnimation(backWarpper, 1, 1050, 50), GetCameraMoveAnimation(0, 0, 0.5, 0, 0, 1.1, 50, 500,viewport), GetRotateAnimation(0, -180, 0.3, 0.3, 50, 1000) } }; back2FrontStory = new Storyboard() { Children = new TimelineCollection() { GetShowHideAnimation(viewport, 0, 1100), GetShowHideAnimation(frontWarpper, 1000, -1), GetShowHideAnimation(backWarpper, -1, 50), GetFadeAnimation(backWarpper, 0, -1, 50), GetFadeAnimation(frontWarpper, 1, 1050, 50), GetCameraMoveAnimation(0, 0, 0.5, 0, 0, 1.1, 50, 500, viewport), GetRotateAnimation(180, 360, 0.3, 0.3, 50, 1000) } }; this.Effect = new DropShadowEffect() { BlurRadius = 10, Opacity = 0.8 }; this.Children.Add(frontWarpper); this.Children.Add(backWarpper); this.Children.Add(viewport); array = null; } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值