WPF圆动画旋转、边框、内容渲染、阴影
时间: 2023-09-18 12:13:28 浏览: 109
WPF中可以使用Storyboard和DoubleAnimation来实现圆的旋转动画。首先,在XAML中创建一个圆形,然后使用Storyboard和DoubleAnimation实现圆的旋转动画。例如:
```xml
<Grid>
<Ellipse Width="100" Height="100" Fill="Blue">
<Ellipse.Triggers>
<EventTrigger RoutedEvent="Loaded">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="(UIElement.RenderTransform).(RotateTransform.Angle)"
From="0" To="360" Duration="0:0:2" RepeatBehavior="Forever" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Ellipse.Triggers>
<Ellipse.RenderTransform>
<RotateTransform CenterX="50" CenterY="50" />
</Ellipse.RenderTransform>
</Ellipse>
</Grid>
```
边框可以使用Border控件来实现。Border控件有多个属性可以用来设置边框的颜色、宽度和样式。例如:
```xml
<Border BorderBrush="Black" BorderThickness="2" CornerRadius="10">
<TextBlock Text="Hello, world!" />
</Border>
```
内容渲染可以通过控件的样式和模板来实现。例如,可以使用控件模板和VisualBrush来将一个图像作为控件的背景。例如:
```xml
<Style TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid>
<Rectangle>
<Rectangle.Fill>
<VisualBrush Stretch="Uniform">
<VisualBrush.Visual>
<Image Source="myImage.png" />
</VisualBrush.Visual>
</VisualBrush>
</Rectangle.Fill>
</Rectangle>
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
```
阴影可以通过控件的样式和DropShadowEffect来实现。例如,可以使用控件模板和DropShadowEffect将一个控件的边缘添加阴影。例如:
```xml
<Style TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border BorderThickness="2" BorderBrush="Black" CornerRadius="10">
<Border.Effect>
<DropShadowEffect ShadowDepth="5" BlurRadius="5" Color="Black" />
</Border.Effect>
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
```
阅读全文