XAML具有创建自定义控件的最强大功能之一,可以非常轻松地创建功能丰富且可自定义的控件.当Microsoft提供的所有内置控件未满足您的条件或您不想支付3 rd 派对控件时,将使用自定义控件.
在本章中,您将学习如何创建自定义控件.在我们开始查看自定义控件之前,让我们先快速查看用户控件.
用户控件提供了一种技术收集并组合不同的内置控件并将它们打包成可重复使用的XAML.用户控件在以下场景中使用 :
如果控件包含现有控件,即您可以创建一个单个控制多个已存在的控件.
如果控件不需要支持主题.用户控件不支持复杂的自定义,控件模板,也很难设置样式.
如果开发人员喜欢使用代码隐藏模型编写控件,其中a查看,然后为事件处理程序编写直接代码.
您不会跨应用程序共享控件.
让我们以用户控制为例,按照下面给出的步骤&减去;
第1步 : 创建一个新的WPF项目,然后右键单击您的解决方案并选择Add>新项目...
第2步 : 将打开以下对话框,现在选择用户控制(WPF)并将其命名为 MyUserControl .
第3步 : 单击"添加"按钮,您将看到解决方案中将添加两个新文件(MyUserControl.xaml和MyUserControl.cs).
下面给出的是XAML代码,其中创建了一个按钮和文本框,其中包含MyUserControl.xaml文件中的一些属性.
<UserControl x:Class = "XAMLUserControl.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" Width = "75" Click = "button_Click" /> </Grid> </UserControl>
以下是MyUserControl.cs文件中用于更新文本框的按钮点击事件的C#代码.
using System; using System.Windows; using System.Windows.Controls; namespace XAMLUserControl { /// <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:XAMLUserControl" Title = "MainWindow" Height = "350" Width = "525"> <Grid> <control:MyUserControl/> </Grid> </Window>
当你编译并执行上面的代码时,它会产生以下输出 :
现在点击"Click Me"按钮,您将看到文本框文本已更新.
自定义control是一个提供自己的样式和模板的类,通常在 generic.xaml 中定义.自定义控件用于以下场景,
如果控件不存在,则必须从头开始创建.
如果您想通过添加额外的属性或额外的功能来扩展或添加预先存在的控件的功能,以适合您的特定场景.
如果你的控件需要支持主题和样式.
如果你想分享你跨应用程序的控制权.
让我们举一个自定义控件的例子,并按照下面给出的步骤.
第1步 : 创建一个新的WPF项目,然后右键单击您的解决方案并选择Add>新项目...
第2步 : 将打开以下对话框.现在选择自定义控件(WPF)并将其命名为 MyCustomControl .
第3步 : 单击Add按钮,您将看到解决方案中将添加两个新文件(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:XAMLCustomControls"> <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 XAMLCustomControls { 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 XAMLCustomControls { /// <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 = "XAMLCustomControls.MainWindow" xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml" xmlns:control = "clr-namespace:XAMLCustomControls" 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> </Window>
编译并执行上面的代码时,它将产生以下输出.观察输出包含一个自定义控件,这是一个自定义按钮.
现在点击自定义按钮.您将看到文本块文本已更新.