WPF应用程序允许创建自定义控件,这使得创建功能丰富且可自定义的控件变得非常容易.当Microsoft提供的所有内置控件未满足您的条件或您不想为第三方控件付费时,将使用自定义控件.
在本章中,您将使用学习如何创建自定义控件.在我们开始介绍自定义控件之前,让我们先看看用户控件.
用户控件提供了一种方式收集并组合不同的内置控件并将它们打包成可重复使用的XAML.用户控件用于以下场景 :
如果控件包含现有控件,即可以创建单个控件控制多个已存在的控件.
如果控件不需要支持主题.用户控件不支持复杂的自定义,控件模板和难以设置样式.
如果开发人员更喜欢使用代码隐藏模型编写控件,其中一个视图然后是事件处理程序的直接代码.
您不会跨应用程序共享您的控件.
让我们转到用户控件的示例,并按照以下步骤进行操作.
创建一个新的WPF项目,然后右键单击您的解决方案并选择Add>新项目......
将打开以下窗口.现在选择用户控件(WPF)并将其命名为MyUserControl.
单击"添加"按钮,您将看到两个新的文件(MyUserControl.xaml和MyUserControl.cs)将添加到您的解决方案中.
这是XAML代码,其中包含一个按钮和使用MyUserControl.xaml文件中的一些属性创建一个文本框.
<UserControl x:Class = "WPFUserControl.MyUserControl" 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" mc:Ignorable = "d" d:DesignHeight = "300" d:DesignWidth = "300"> <Grid> <TextBox Height = "23" HorizontalAlignment = "Left" Margin = "80,49,0,0" Name = "txtBox" VerticalAlignment = "Top" Width = "200" /> <Button Content = "Click Me" Height = "23" HorizontalAlignment = "Left" Margin = "96,88,0,0" Name = "button" VerticalAlignment = "Top" Click = "button_Click" /> </Grid> </UserControl>
以下是MyUserControl.cs文件中按钮点击事件的C#代码,用于更新文本框.
using System; using System.Windows; using System.Windows.Controls; namespace WPFUserControl { /// <summary> /// Interaction logic for MyUserControl.xaml /// </summary> public partial class MyUserControl : UserControl { public MyUserControl() { InitializeComponent(); } private void button_Click(object sender, RoutedEventArgs e) { txtBox.Text = "You have just clicked the button"; } } }
以下是MainWindow.xaml中添加用户控件的实现.
<Window x:Class = "XAMLUserControl.MainWindow" xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml" xmlns:control = "clr-namespace:WPFUserControl" Title = "MainWindow" Height = "350" Width = "525"> <Grid> <control:MyUserControl/> </Grid> </Window>
当你编译并执行上面的代码时,它会产生以下窗口.
点击"Click Me"按钮后,您会注意到文本框内的文本已更新.
自定义控件是一个提供自己的样式和模板的类,通常在generic.xaml中定义.自定义控件用于以下场景 :
如果控件不存在,您必须从中创建它刮擦.
如果您想通过添加额外的属性或额外的功能来扩展或添加预先存在的控件的功能,以适合您的特定场景.
如果你的控件需要支持主题和样式.
如果你想分享你的控件跨应用程序.
让我们举个例子来了解自定义控件的工作原理.创建一个新的WPF项目,然后右键单击您的解决方案并选择Add>新项目...
它将打开以下内容窗口.现在选择自定义控件(WPF)并将其命名为 MyCustomControl .
单击"添加"按钮,您将看到解决方案中将添加两个新文件(Themes/Generic.xaml和MyCustomControl.cs).
以下是为Generic.xaml文件中的自定义控件设置样式的XAML代码.
<ResourceDictionary xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local = "clr-namespace:WPFCustomControls"> <Style TargetType = "{x:Type local:MyCustomControl}" BasedOn = "{StaticResource {x:Type Button}}"> <Setter Property = "Background" Value = "LightSalmon" /> <Setter Property = "Foreground" Value = "Blue"/> </Style> </ResourceDictionary>
这是MyCustomControl类的C#代码,它继承自按钮类,在构造函数中它覆盖了元数据.
using System; using System.Windows; using System.Windows.Controls; namespace WPFCustomControls { public class MyCustomControl : Button { static MyCustomControl() { DefaultStyleKeyProperty.OverrideMetadata(typeof(MyCustomControl), new FrameworkPropertyMetadata(typeof(MyCustomControl))); } } }
这是C#中的自定义控件点击事件实现,它更新了文本块的文本.
using System; using System.Windows; using System.Windows.Controls; namespace WPFCustomControls { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void customControl_Click(object sender, RoutedEventArgs e) { txtBlock.Text = "You have just click your custom control"; } } }
这是MainWindow.xaml中的实现,用于添加自定义控件和一个TextBlock.
<Window x:Class = "WPFCustomControls.MainWindow" xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml" xmlns:control = "clr-namespace:WPFCustomControls" Title = "MainWindow" Height = "350" Width = "604"> <StackPanel> <control:MyCustomControl x:Name = "customControl" Content = "Click Me" Width = "70" Margin = "10" Click = "customControl_Click"/> <TextBlock Name = "txtBlock" Width = "250" Height = "30"/> </StackPanel>>
当您编译并执行上面的代码时,它将生成以下窗口,其中包含一个自定义控件,这是一个自定义按钮.
单击自定义按钮后,您将看到文本块内的文本已更新.