将下述的XAML通过代码实现:
<Window x:Class="WpfAppLearn1.Window3"
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:WpfAppLearn1"
mc:Ignorable="d" Title="Window3" Height="206" Width="509" WindowStartupLocation="CenterScreen">
<Canvas x:Name="canvas1" Background="#FFCFC9C9" Margin="0,0,1,0">
<Canvas.Resources>
<SolidColorBrush x:Key="redSolid" Color="Red"/>
<Style x:Key="temp1" TargetType="Button">
<Setter Property="Foreground" Value="White" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid>
<Ellipse x:Name="ellipse1" StrokeThickness="0.5" Stroke="Aqua">
<Ellipse.Fill>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<GradientStop Offset="0" Color="#ff7e7e7e" />
<GradientStop Offset="1" Color="Black" />
</LinearGradientBrush>
</Ellipse.Fill>
</Ellipse>
<ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center"
Content="{TemplateBinding Content}" />
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="ellipse1" Property="Ellipse.Fill" Value="AliceBlue"/>
<Setter Property="Foreground" Value="{StaticResource redSolid}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Canvas.Resources>
<TextBox x:Name="tb1" Height="23" TextWrapping="Wrap" Text="TextBox" Width="120" Canvas.Left="60" Canvas.Top="75">
<TextBox.Style>
<Style TargetType="TextBox">
<Setter Property="Foreground" Value="Aqua" />
<Style.Triggers>
<Trigger Property="Text" Value="123">
<Setter Property="Foreground" Value="{StaticResource redSolid}" />
</Trigger>
<EventTrigger RoutedEvent="MouseEnter">
<BeginStoryboard>
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background">
<DiscreteObjectKeyFrame KeyTime="0:0:0">
<DiscreteObjectKeyFrame.Value>
<SolidColorBrush Color="Gold" />
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
<Button Style="{StaticResource temp1}" Content="Button" Width="100" Canvas.Left="215" Canvas.Top="60" Height="50"/>
</Canvas>
</Window>
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
void InitView()
{
Window window = new Window
{
Title = "Window3", Height = 206, Width = 509,
WindowStartupLocation = WindowStartupLocation.CenterScreen
};
Canvas canvas = new Canvas();
canvas.Name = "canvas1";
canvas.Background = new SolidColorBrush(Color.FromArgb(0xFF, 0xCF, 0xC9, 0xC9));
canvas.Margin = new Thickness(0, 0, 1, 0);
canvas.Resources.Add("redSolid", new SolidColorBrush(Colors.Red));
#region 样式temp1
Style styleTemp1 = new Style { TargetType = typeof(Button) };
styleTemp1.Setters.Add(new Setter
{
Property = Button.ForegroundProperty,
Value = new SolidColorBrush(Colors.White)
});
styleTemp1.Setters.Add(new Setter
{
Property = Button.BackgroundProperty,
Value = new SolidColorBrush(Colors.Gray)
});
//创建控件模板
ControlTemplate btnTemplate = new ControlTemplate { TargetType = typeof(Button) };
Setter temp1Temp = new Setter { Property = Button.TemplateProperty };
temp1Temp.Value = btnTemplate;
styleTemp1.Setters.Add(temp1Temp);
//创建鼠标移过触发器
Trigger overTrigger = new Trigger { Property = Button.IsMouseOverProperty, Value = true };
overTrigger.Setters.Add(new Setter
{
TargetName = "ellipse1",
Property = Ellipse.FillProperty,
Value = new SolidColorBrush(Colors.AliceBlue)
});
overTrigger.Setters.Add(new Setter
{
Property = Button.ForegroundProperty,
Value = canvas.Resources["redSolid"]
});
btnTemplate.Triggers.Add(overTrigger);
//创建控件模板根节点
FrameworkElementFactory factoryGrid = new FrameworkElementFactory(typeof(Grid));
//创建椭圆填充色
LinearGradientBrush lineBrush = new LinearGradientBrush
{
StartPoint = new Point(0, 0),
EndPoint = new Point(0, 1)
};
lineBrush.GradientStops.Add(new GradientStop { Offset = 0, Color = Color.FromArgb(0xFF, 0x7E, 0x7E, 0x7E) });
lineBrush.GradientStops.Add(new GradientStop { Offset = 1, Color = Colors.Black });
//创建椭圆节点
FrameworkElementFactory fEllipse = new FrameworkElementFactory(typeof(Ellipse), "ellipse1");
fEllipse.SetValue(Ellipse.NameProperty, "ellipse1");
fEllipse.SetValue(Ellipse.StrokeThicknessProperty, 0.5);
fEllipse.SetValue(Ellipse.StrokeProperty, new SolidColorBrush(Colors.Aqua));
fEllipse.SetValue(Ellipse.FillProperty, lineBrush);
factoryGrid.AppendChild(fEllipse); //将椭圆节点添加到根节点
//创建文本节点
FrameworkElementFactory fPresenter = new FrameworkElementFactory(typeof(ContentPresenter));
fPresenter.SetValue(ContentPresenter.VerticalAlignmentProperty, VerticalAlignment.Center);
fPresenter.SetValue(ContentPresenter.HorizontalAlignmentProperty, HorizontalAlignment.Center);
fPresenter.SetValue(ContentPresenter.ContentProperty, new TemplateBindingExtension(Button.ContentProperty));
factoryGrid.AppendChild(fPresenter); //将文本节点添加到根节点
btnTemplate.VisualTree = factoryGrid;
canvas.Resources.Add("temp1", styleTemp1);
#endregion
TextBox tb = new TextBox { Name = "tb1", Height = 23, Text = "TextBox", Width = 120 };
canvas.Children.Add(tb);
Canvas.SetLeft(tb, 60);
Canvas.SetTop(tb, 75);
//创建tb1的样式
Style tbStyle = new Style { TargetType = typeof(TextBox) };
//设置tb1的前景色
tbStyle.Setters.Add(new Setter
{
Property = TextBox.ForegroundProperty,
Value = new SolidColorBrush(Colors.Aqua)
});
//创建tb1的文本触发器
Trigger textTrigger = new Trigger { Property = TextBox.TextProperty, Value = "123" };
textTrigger.Setters.Add(new Setter
{
Property = TextBox.ForegroundProperty,
Value = canvas.Resources["redSolid"]
});
tbStyle.Triggers.Add(textTrigger);
//创建tb1的事件触发器
BeginStoryboard beginStoryboard = new BeginStoryboard();
beginStoryboard.Storyboard = new Storyboard();
ObjectAnimationUsingKeyFrames objectAnimationUsingKeyFrames = new ObjectAnimationUsingKeyFrames();
DiscreteObjectKeyFrame discreteObjectKeyFrame = new DiscreteObjectKeyFrame
{
Value = new SolidColorBrush(Colors.Gold),
KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(0))
};
objectAnimationUsingKeyFrames.KeyFrames.Add(discreteObjectKeyFrame);
beginStoryboard.Storyboard.Children.Add(objectAnimationUsingKeyFrames);
Storyboard.SetTargetProperty(objectAnimationUsingKeyFrames, new PropertyPath(TextBox.BackgroundProperty));
EventTrigger enterEvent = new EventTrigger(TextBox.MouseEnterEvent);
enterEvent.Actions.Add(beginStoryboard);
tbStyle.Triggers.Add(enterEvent);
tb.Style = tbStyle;
Button btn = new Button
{
Content = "Button",
Height = 50,
Width = 100,
Style = canvas.Resources["temp1"] as Style
};
canvas.Children.Add(btn);
Canvas.SetLeft(btn, 215);
Canvas.SetTop(btn, 60);
window.Content = canvas;
window.Show();
}