Learn WPF with me (2): WPF control basics, wpf control

Source: Internet
Author: User

Learn WPF with me (2): WPF control basics, wpf control
Introduction to WPF controls

Through the introduction of the previous XAML language, we know that XAML is a tree structure. Similarly, the WPF control is a part of the entire XAML tree and also a tree structure. Let's look at a simple example.

<Button. content> <DockPanel> <Image Source = "F: \ 01. code \ 01. myCode \ WpfApplication1 \ WpfApplication1 \ Resources \ he.jpg "Width =" 30 "/> <TextBlock Text =" Red "Foreground =" Red "verticalignment =" Center "> </TextBlock> <textBlock Text = "Green" Foreground = "Green" VerticalAlignment = "Center"/> <TextBlock Text = "Blue" Foreground = "Blue" verticalignment = "Center"/> </DockPanel> </Button. content>

Interface display Effect

WPF control category

As we mentioned above, the Button inherits from ContentControl. It should be said that it belongs to a content control. Next, we will classify the WPF control. Based on my understanding, we have two classification methods.

1. Classification by control inheritance features

Let's take a look at the inheritance relationship between element classes on the entire WPF interface:


1. Basic controls: controls that you can see and interact with, such as buttons and input boxes, mainly cover the Control, TextBlock, and Image sections above.

2. layout control: a container control that is invisible to the user but plays a decisive role in the layout of the system interface. It mainly covers the Panel and its subclass.

This classification method will be described in the future description.

WPF control properties

Through the above introduction, we know that all controls (basic controls and container controls) are inherited from the FrameworkElement base class, which defines most of the properties used by the WPF control, here, we will first introduce the attributes of these WPF controls (commonly used marked with red), and then introduce the unique attributes when introducing common basic controls separately.

  • ActualHeight: gets the rendering height of this element.
  • ActualWidth: gets the rendering width of the secondary element.
  • BindingGroup: gets or sets the System. Windows. Data. BindingGroup used for this element.
  • ContextMenu: gets or sets the context menu for this element, usually the mail menu.
  • Cursor: gets or sets the Cursor that is displayed when the mouse pointer is hovering over this element.
  • DataContext: gets or sets the data context when an element participates in data binding.
  • DefaultStyleKey: When you use or define a theme style, you can obtain or set a key for the style to reference this control.
  • FlowDirection: gets or sets the flow direction of text and other UI elements in any parent element that controls their layout. Is an enumerated value. The default value is System. Windows. FlowDirection. LeftToRight.
  • FocusVisualStyle: gets or sets an attribute that allows you to customize the appearance, effect, or other style features that will be applied to this element when capturing the keyboard focus.
  • ForceCursor: gets or sets a value that indicates whether the UI should force the Cursor to be rendered as declared by the Cursor attribute.
  • Height: gets or sets the suggested Height of an element.
  • HorizontalAlignment: gets or sets the horizontal alignment feature that is applied to this element when a parent element (such as a panel or item control) forms this element.
  • InputScope: gets or sets the Input Context used by this element.
  • IsInitialized: gets a value indicating whether the element has been initialized.
  • IsLoaded: gets a value indicating whether the element has been loaded for rendering.
  • Language: gets or sets localized/Global Language Information for an element.
  • LayoutTransform: gets or sets the graphic conversion method that should be applied to this element during layout execution.
  • LogicalChildren: gets an enumerator for the logical sub-element of this element.
  • Margin: gets or sets the Margin of an element. The value is System. Windows. Thickness where all attributes are equal to 0 (0.
  • MaxHeight: gets or sets the maximum height constraint of an element.
  • MaxWidth: gets or sets the maximum width constraint of an element.
  • MinHeight: gets or sets the minimum height constraint of an element.
  • MinWidth: gets or sets the minimum width constraint of an element.
  • Name: gets or sets the Name of an element. This name provides a reference so that when the XAML processor constructs a Tag Element during processing, the background code can reference this element.
  • OverridesDefaultStyle: gets or sets a value that indicates whether the element combines style attributes in the topic style.
  • Parent: gets the logical Parent element of this element.
  • Resources: gets or sets the locally defined resource dictionary.
  • Style: gets or sets the Style used when this element is rendered.
  • Tag: gets or sets any object value that can be used to store custom information about this element.
  • TemplatedParent: gets a template parent reference to this element. This attribute is irrelevant if this element is not created using a template.
  • ToolTip: gets or sets the ToolTip object displayed for this element in the UI.
  • Triggers: gets a set of Triggers created directly on this element or in sub-element.
  • VerticalAlignment: gets or sets the vertical alignment feature that is applied to a parent element (such as a panel or item control) when this element is combined.
  • VisualChildrenCount: gets the number of visual sub-elements in this element.
  • Width: gets or sets the Width of an element.

Read/write attributes (attributes with get or set descriptions) can be used in XAML. Read-Only attributes (only attributes with descriptions) can only be used in background code.

WPF control declaration

The use of the Wpf control is described through a simple Button definition.

1. Declare in XAML

You can find the Button control in the toolbox and drag it directly to the form. a xaml statement is generated at the corresponding position. We recommend that you use the hand-written XAML method to ensure that the control appears where you want it.

<Button Height = "30" Width = "120" Name = "myButton"/>

This statement declares a Button object named myButton, with a height of 30 and a width of 120. We can directly access and use this object in the background code, such as through

Mybutton. Height = 25; you can change the Height of the Button object to 25.

2. declare in the background. CS code

Button myButton = new Button();myButton.Height = 30;myButton.Width = 100;

In the next article, we will learn how to use basic controls such as Label, TextBLock, Button, and TextBox in WPF.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.