DLL 标签 标签
- 一句话的事儿:将设计好的控件供第三方使用。
效果介绍
- 创建一个project(wpf library);
设计按钮组件
<UserControl x:Class="FSButtonlib.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:FSButtonlib"
mc:Ignorable="d"
d:DesignHeight="50" d:DesignWidth="150">
<Grid>
<Grid.Resources>
<Style x:Key="beautifulBtn" TargetType="Button">
<Setter Property="Width" Value="150"/>
<Setter Property="Height" Value="50"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Grid RenderTransformOrigin="0.5,0.5">
<Grid.RenderTransform>
<RotateTransform Angle="0" x:Name="gridRotate"/>
</Grid.RenderTransform>
<Grid.Triggers>
<EventTrigger RoutedEvent="FrameworkElement.Loaded">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetName="gridRotate" Storyboard.TargetProperty="Angle" To="360" Duration="0:0:2"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Grid.Triggers>
<TextBlock x:Name="txtBlock" Text="{TemplateBinding Button.Content}" TextAlignment="Center" VerticalAlignment="Center" FontSize="25" Foreground="Blue"/>
<!--<Rectangle x:Name="rec" Width="100" Height="50" Fill="Violet" RadiusX="25" RadiusY="25" Opacity="0.5"></Rectangle>-->
<Rectangle x:Name="rec" Width="100" Height="50" RadiusX="25" RadiusY="25" Opacity="0.5">
<!--将填充部分单独写,为了后面单独调用做准备-->
<Rectangle.Fill>
<!--采用刷子来填充-->
<SolidColorBrush x:Name="recBrush" Color="Blue"/>
</Rectangle.Fill>
<Rectangle.Triggers>
<EventTrigger RoutedEvent="UIElement.MouseEnter">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetName="txtBlock" Storyboard.TargetProperty="FontSize" To="30" Duration="0:0:0.5"/>
<ColorAnimation Storyboard.TargetName="recBrush" Storyboard.TargetProperty="Color" To="Gray" Duration="0:0:1"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger RoutedEvent="UIElement.MouseLeave">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetName="txtBlock" Storyboard.TargetProperty="FontSize" To="25" Duration="0:0:0.5"/>
<DoubleAnimation Storyboard.TargetName="txtBlock" Storyboard.TargetProperty="FontSize" To="25" Duration="0:0:0.5"/>
<ColorAnimation Storyboard.TargetName="recBrush" Storyboard.TargetProperty="Color" To="Blue" Duration="0:0:1"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Rectangle.Triggers>
</Rectangle>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Grid.Resources>
<Button x:Name="btn" Style="{StaticResource beautifulBtn}" Content="按钮模板" Height="50" Width="150" HorizontalAlignment="Left" VerticalAlignment="Top" />
</Grid>
</UserControl>
设全局变量ButtonContent ```csharp using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes;
namespace FSButtonlib { ///
private string buttonContent;
public string ButtonContent
{
get { return buttonContent; }
set { buttonContent = value;
this.Dispatcher.Invoke(() =>
{
btn.Content= value;
});
}
}
}
}
3. 调用按钮组件
3. 调用按钮组件时,Content设为全局变量,从而方便随时更改
```xml
<Window x:Class="WPFApp.MainWindow"
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:WPFApp"
xmlns:c="clr-namespace:FSButtonlib;assembly=FSButtonlib"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<c:UserControl1 Height="50" Width="150" ButtonContent="测试按钮内容" Margin="321,184.5,0,0" VerticalAlignment="Top" HorizontalAlignment="Left"/>
</Grid>
</Window>
- 本文作者:GeekPower - Felix Sun
- 版权声明: 本博客所有文章除特别声明外,均采用 MIT 许可协议。转载请注明出处!