德国世界杯_2012年世界杯 - fyycdq.com

德国世界杯_2012年世界杯 - fyycdq.com

控件 - WPF

Windows Presentation Foundation(WPF)包括许多常用的 UI 组件,例如几乎每个 Windows 应用中使用的 Button、Label、TextBox、Menu 和 ListBox。 从历史上看,开发人员将这些对象称为控件。 开发人员使用术语“control”松散地表示应用中可见对象的任何类。 类不需要从 Control 类继承才能有可见状态。 继承自类的 Control 类包含一个 ControlTemplate,它允许控件使用者从根本上更改控件的外观,而无需创建新的子类。 本文介绍如何在 WPF 中使用控件(既继承自 Control 类的控件,又使用不继承控件)。

创建控件的实例

可以使用可扩展应用程序标记语言(XAML)或代码将控件添加到应用。 例如,请考虑以下 WPF 窗口的图像,该窗口要求用户输入其名称和地址:

此窗口包含六个控件:两个标签、两个文本框和两个按钮。 应用使用 XAML 创建这些控件,如以下代码片段所示:

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Title="Input Record" Height="Auto" Width="300" SizeToContent="Height">

可以在 XAML 中类似地创建所有控件。 还可以在代码中创建同一窗口:

// Grid container which is the content of the Window

Grid container = new() { Margin = new Thickness(5) };

container.RowDefinitions.Add(new RowDefinition());

container.RowDefinitions.Add(new RowDefinition());

container.RowDefinitions.Add(new RowDefinition());

container.ColumnDefinitions.Add(new ColumnDefinition());

container.ColumnDefinitions.Add(new ColumnDefinition());

// Create the two labels, assign the second label to the second row

Label labelName = new() { Content = "Enter your name:" };

container.Children.Add(labelName);

Label labelAddress = new() { Content = "Enter your address:" };

Grid.SetRow(labelAddress, 1);

container.Children.Add(labelAddress);

// Create the two textboxes, assign both to the second column and

// assign the second textbox to the second row.

TextBox textboxName = new() { Margin = new Thickness(2) };

Grid.SetColumn(textboxName, 1);

container.Children.Add(textboxName);

TextBox textboxAddress = new() { Margin = new Thickness(2) };

Grid.SetRow(textboxAddress, 1);

Grid.SetColumn(textboxAddress, 1);

container.Children.Add(textboxAddress);

// Create the two buttons, assign both to the third row and

// assign the second button to the second column.

Button buttonReset = new() { Margin = new Thickness(2), Content = "Reset" };

Grid.SetRow(buttonReset, 2);

container.Children.Add(buttonReset);

Button buttonSubmit = new() { Margin = new Thickness(2), Content = "Submit" };

Grid.SetColumn(buttonSubmit, 1);

Grid.SetRow(buttonSubmit, 2);

container.Children.Add(buttonSubmit);

// Create the popup window and assign the container (Grid) as its content

Window inputWindow = new()

{

Title = "Input Record",

Height = double.NaN,

Width = 300,

SizeToContent = SizeToContent.Height,

Content = container

};

inputWindow.Show();

' Grid container which is the content of the Window

Dim container As New Grid() With {.Margin = New Thickness(5)}

container.RowDefinitions.Add(New RowDefinition())

container.RowDefinitions.Add(New RowDefinition())

container.RowDefinitions.Add(New RowDefinition())

container.ColumnDefinitions.Add(New ColumnDefinition())

container.ColumnDefinitions.Add(New ColumnDefinition())

' Create the two labels, assign the second label to the second row

Dim labelName As New Label() With {.Content = "Enter your name:"}

container.Children.Add(labelName)

Dim labelAddress As New Label() With {.Content = "Enter your address:"}

Grid.SetRow(labelAddress, 1)

container.Children.Add(labelAddress)

' Create the two textboxes, assign both to the second column and

' assign the second textbox to the second row.

Dim textboxName As New TextBox() With {.Margin = New Thickness(2)}

Grid.SetColumn(textboxName, 1)

container.Children.Add(textboxName)

Dim textboxAddress As New TextBox() With {.Margin = New Thickness(2)}

Grid.SetRow(textboxAddress, 1)

Grid.SetColumn(textboxAddress, 1)

container.Children.Add(textboxAddress)

' Create the two buttons, assign both to the third row and

' assign the second button to the second column.

Dim buttonReset As New Button() With {.Margin = New Thickness(2), .Content = "Reset"}

Grid.SetRow(buttonReset, 2)

container.Children.Add(buttonReset)

Dim buttonSubmit As New Button() With {.Margin = New Thickness(2), .Content = "Submit"}

Grid.SetColumn(buttonSubmit, 1)

Grid.SetRow(buttonSubmit, 2)

container.Children.Add(buttonSubmit)

' Create the window and assign the container (Grid) as its content

Dim inputWindow As New Window() With

{

.Title = "Input Record",

.Height = Double.NaN,

.Width = 300,

.SizeToContent = SizeToContent.Height,

.Content = container

}

inputWindow.Show()

订阅事件

可以使用 XAML 或代码订阅控件的事件,但只能在代码中处理事件。

在 XAML 中,将事件设置为元素上的属性。 不能将 handler 表示法用于事件。 以下代码片段演示如何订阅 Click 以下 Button事件:

下面介绍如何在代码中执行相同的操作:

Button buttonSubmit = new() { Margin = new Thickness(2), Content = "Submit" };

buttonSubmit.Click += Submit_Click;

Dim buttonSubmit As New Button() With {.Margin = New Thickness(2), .Content = "Submit"}

AddHandler buttonSubmit.Click, AddressOf Submit_Click

以下代码片段处理 Click 以下 Button事件:

private void Submit_Click(object sender, RoutedEventArgs e)

{

MessageBox.Show("Someone clicked the submit button.");

}

Private Sub Submit_Click(sender As Object, e As Windows.RoutedEventArgs)

MessageBox.Show("Someone clicked the submit button.")

End Sub

更改控件的外观

通常更改控件的外观以适应应用的外观。 可以通过执行以下作之一来更改控件的外观,具体取决于要完成的内容:

更改控件的属性的值。

为控件创建一个 Style 。

为控件创建新 ControlTemplate 控件。

更改控件的属性

许多控件都有可用于更改控件显示方式的属性,例如按钮的背景。 在 XAML 和代码中设置值属性。 以下示例在 XAML 中的Background上设置FontSize、FontWeight和Button属性:

下面介绍如何在代码中执行相同的操作:

Button buttonSubmit = new() { Margin = new Thickness(2), Content = "Submit" };

buttonSubmit.FontSize = 18f;

buttonSubmit.FontWeight = FontWeights.Bold;

buttonSubmit.Background =

new LinearGradientBrush(

(Color)ColorConverter.ConvertFromString("#0073E6"),

(Color)ColorConverter.ConvertFromString("#81D4FA"),

new Point(0d, 0d),

new Point(0.9d, 0d));

Dim buttonSubmit As New Button() With {.Margin = New Thickness(2), .Content = "Submit"}

buttonSubmit.FontSize = 18.0F

buttonSubmit.FontWeight = FontWeights.Bold

buttonSubmit.Background =

New LinearGradientBrush(

ColorConverter.ConvertFromString("#0073E6"),

ColorConverter.ConvertFromString("#81D4FA"),

New Point(0D, 0D),

New Point(0.9D, 0D))

下图显示了示例窗口:

为控件创建样式

WPF 使你能够通过创建控件 Style而不是在每个控件上设置属性来指定控件的外观。 通常在 XAML 中定义Style,这类定义通常位于ResourceDictionary中,例如控件或 Window 的Resources属性。 资源适用于您声明它们时指定的范围。 有关详细信息,请参阅 XAML 资源的概述。

以下示例将 Style 应用于定义样式的同一 Button 中包含的每个 Grid:

下面介绍如何在代码中执行相同的操作:

Grid container = new() { Margin = new Thickness(5) };

container.RowDefinitions.Add(new RowDefinition());

container.RowDefinitions.Add(new RowDefinition());

container.RowDefinitions.Add(new RowDefinition());

container.ColumnDefinitions.Add(new ColumnDefinition());

container.ColumnDefinitions.Add(new ColumnDefinition());

Style buttonStyle = new(typeof(Button));

buttonStyle.Setters.Add(new Setter(Button.FontSizeProperty, 18d));

buttonStyle.Setters.Add(new Setter(Button.FontWeightProperty, FontWeights.Bold));

buttonStyle.Setters.Add(new Setter(Button.BackgroundProperty,

new LinearGradientBrush(

(Color)ColorConverter.ConvertFromString("#0073E6"),

(Color)ColorConverter.ConvertFromString("#81D4FA"),

new Point(0d, 0d),

new Point(0.9d, 0d))));

container.Resources.Add(typeof(Button), buttonStyle);

Dim container As New Grid() With {.Margin = New Thickness(5)}

container.RowDefinitions.Add(New RowDefinition())

container.RowDefinitions.Add(New RowDefinition())

container.RowDefinitions.Add(New RowDefinition())

container.ColumnDefinitions.Add(New ColumnDefinition())

container.ColumnDefinitions.Add(New ColumnDefinition())

Dim buttonStyle As New Style(GetType(Button))

buttonStyle.Setters.Add(New Setter(Button.FontSizeProperty, 18.0R))

buttonStyle.Setters.Add(New Setter(Button.FontWeightProperty, FontWeights.Bold))

buttonStyle.Setters.Add(New Setter(Button.BackgroundProperty,

New LinearGradientBrush(

ColorConverter.ConvertFromString("#0073E6"),

ColorConverter.ConvertFromString("#81D4FA"),

New Point(0D, 0D),

New Point(0.9D, 0D))))

container.Resources.Add(GetType(Button), buttonStyle)

下图显示了应用于窗口网格的样式,这会更改两个按钮的外观:

与其将样式应用于特定类型的所有控件,不如通过在资源字典中的样式中添加一个键,并在控件的Style属性中引用该键,以将样式分配给特定控件。 有关样式的详细信息,请参阅 样式和模板化。

创建 ControlTemplate

一个Style可以一次性设置多个控件的属性,但有时你需要自定义控件的外观,超出Style能够提供的功能。 从 Control 类继承的类具有一个 ControlTemplate,它定义了控件的结构和外观。

请考虑该 Button 控件,即几乎每个应用使用的通用控件。 按钮的主要行为是在用户选择按钮时使应用能够执行某些作。 默认情况下,WPF 中的按钮显示为凸起的矩形。 在开发应用时,你可能想要利用按钮的行为(即用户如何与引发 Click 事件的按钮交互),但你可能希望更改按钮的外观,超出仅仅通过更改按钮属性所能实现的程度。 在这种情况下,可以创建新的 ControlTemplate。

以下示例为ControlTemplate创建Button。

ControlTemplate 会创建一个视觉效果,为 Button 呈现带有圆角和渐变背景的边框。

注释

将 Button 的 Background 属性设置为 SolidColorBrush,以便示例正常工作。

下面介绍如何在代码中执行相同的操作。 以下代码创建一个 XAML 字符串并对其进行分析,以生成可应用的模板。 此方法是运行时生成模板的受支持方法。

Button buttonSubmit = new() { Margin = new Thickness(2), Content = "Submit" };

// Create the XAML used to define the button template

const string xaml = """

EndPoint="1,0.5">

""";

// Load the XAML into a stream that can be parsed

using MemoryStream stream = new(System.Text.Encoding.UTF8.GetBytes(xaml));

// Create a parser context and add the default namespace and

// the x namespace, which is common to WPF XAML

System.Windows.Markup.ParserContext context = new();

context.XmlnsDictionary.Add("", "http://schemas.microsoft.com/winfx/2006/xaml/presentation");

context.XmlnsDictionary.Add("x", "http://schemas.microsoft.com/winfx/2006/xaml");

// Parse the XAML and assign it to the button's template

buttonSubmit.Template = (ControlTemplate)System.Windows.Markup.XamlReader.Load(stream, context);

// Set the other properties of the button

Grid.SetColumn(buttonSubmit, 1);

Grid.SetRow(buttonSubmit, 2);

// Assign the button to the grid container

container.Children.Add(buttonSubmit);

Dim buttonSubmit As New Button() With {.Margin = New Thickness(2), .Content = "Submit"}

' Create the XAML used to define the button template

Const xaml As String = "

EndPoint=""1,0.5"">

"

' Load the XAML into a stream that can be parsed

Using stream As New MemoryStream(System.Text.Encoding.UTF8.GetBytes(xaml))

' Create a parser context and add the default namespace and

' the x namespace, which is common to WPF XAML

Dim context = New System.Windows.Markup.ParserContext()

context.XmlnsDictionary.Add("", "http://schemas.microsoft.com/winfx/2006/xaml/presentation")

context.XmlnsDictionary.Add("x", "http://schemas.microsoft.com/winfx/2006/xaml")

' Parse the XAML and assign it to the button's template

buttonSubmit.Template = System.Windows.Markup.XamlReader.Load(stream, context)

End Using

' Set the other properties of the button

Grid.SetColumn(buttonSubmit, 1)

Grid.SetRow(buttonSubmit, 2)

' Assign the button to the grid container

container.Children.Add(buttonSubmit)

下图显示了应用模板时的外观:

在上一个示例中,你将 ControlTemplate 应用于单个按钮。 但是,你可以分配一个ControlTemplateStyle并将其应用于所有按钮,如创建控件样式部分中所示。

有关如何利用模板提供的独特功能的详细信息,请参阅 样式设置和模板化。

控件中的丰富内容

大多数从 Control 类继承的类可以包含丰富的内容。 例如,Label 可以包含任何对象,例如字符串、Image或Panel。 以下类支持丰富的内容,并充当 WPF 中大多数控件的基类:

ContentControl- 继承自此类的类的一些示例是 Label, Button以及 ToolTip。

ItemsControl- 继承自此类的类的一些示例是 ListBox, Menu以及 StatusBar。

HeaderedContentControl- 继承自此类的类的一些示例是 TabItem, GroupBox以及 Expander。

HeaderedItemsControl- 继承自此类的类的一些示例是 MenuItem, TreeViewItem以及 ToolBar。

相关内容

样式设计与模板制作

数据绑定概述