WPF study notes (1)-new WPF application

(1) New WPF application
XAML language: Extensible application markup language, the namespace is not one-to-one correspondence with .NET, but one-to-many, both of the "URL" type, and follow the XAML parser standard The naming rule is not the real URL.

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

Correspond to some CLR namespaces related to XAML syntax and compilation, such as

<Style x:key="buttonMouseOver" TargetType="{x:Type Button}">

The difference between xmlns and xmlns:x here is that x is used as an alias and appears in the form of a prefix when applied, while xmlns is used as a default namespace, and elements that do not use prefixes are derived from this namespace.
XAML namespace syntax:
xmlns[:optional mapping prefix]="namespace description"
**Note: * xmlns without optional mapping prefix is ​​the default namespace of WPF, and an xaml file can only have one default name Space
A complete xaml file must have two namespaces.
(2) The panels used by WPF for layout include:
StackPanel stack panel, WrapPanel surround panel, DockPanel dock panel, Canvas canvas, Grid grid panel, UniformGrid uniform grid HorizontalAlignment Horizontal alignment VerticalAlignment Vertical alignment
The following are respectively introduced:
I. Grid class Divide the form into several grids. There are two attributes, ColumnDefinitions and RowDefinitions, to define how many columns and rows. Three types of values ​​can be set for row height and column width:
1) Absolute value: double value plus unit suffix, the default is pixel
2) Proportion value: add after the double value
, which means that the row occupies all the remaining height and width of the window, so that the window is 3:1. Just set the two row definitions to 3 and respectively .
3) Automatic value: String Auto
If you want the column width to be dragged, the Grip layout itself does not support it. You need to use the Grid layout plus GridSplitter to achieve it. GridSplitter will change the initial row height and column width of the Grid. The code is as follows:

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="25"></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="150"></ColumnDefinition>
            <ColumnDefinition Width="Auto"></ColumnDefinition>
            <ColumnDefinition></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <TextBox Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="3" BorderBrush="Black" />
        <TextBox Grid.Row="1" Grid.Column="0" BorderBrush="Black" />
        <GridSplitter Grid.Row="1" Grid.Column="1" VerticalAlignment="Stretch" HorizontalAlignment="Center" Width="5" Background="Gray" ShowsPreview="True"></GridSplitter>
        <TextBox Grid.Row="1" Grid.Column="2" BorderBrush="Black" />
    </Grid>

Control across rows or columns: (equivalent to merging cells)
Normal state:

<Label Grid.Column=0”  Grid.Row=1”  HorizontalAlignment="Stretch "  VerticalAlignment="Stretch"Content=”Test4”/>

Cross column:

<Label Grid.Column=1”(控件的起始列)  Grid.ColumnSpan=2”(列跨度,这里是从列23)  Grid.Row=1”  HorizontalAlignment="Stretch "  VerticalAlignment="Stretch"  Content=”Test4”/>

Spanning rows is the same as spanning columns, use Grid.RowSpan. For adaptive width and height, HorizontalAlignment and VerticalAlignment use Stretch. When setting these two properties, if you fill the entire area, you cannot set the Width and Height properties. Margin sets all the margins between the control and the parent container; if it is Top then It is the effect of top alignment. At this time, Height can be set, but width cannot be set; if HorizontalAlignment is Left, VerticalAlignment is Bottom, it is horizontal left alignment, width can be set, vertical bottom alignment, height can be set.
II. StackPanel is equivalent to a row of horizontal rows or a column of vertical rows (Orientation sets the horizontal and vertical properties), nesting can also achieve the Grid effect, and the child elements are arranged in the horizontal or vertical direction.
III. WrapPanel places elements in a series of wrapable rows, from left to right in the horizontal direction and top to bottom in the vertical direction.
IV. DockPanel adjusts elements according to the entire border of the container.
V. UniformGrid prevents elements in tables that are invisible but force all cells to have the same size.
VI. Canvas uses fixed coordinates to position elements absolutely, and has no locking or docking function. It is not suitable for windows with variable sizes.

The method to let the control change with the form:
use the ViewBox container space in WPF, it will automatically scale the subspace in the container to fill itself, but it can only have one control, you can use the Canvas control as a child control of the ViewBox control , And then layout other controls in Canvas.

<Viewbox Name=”Viewbox1” Stretch=”Fill”>
<Canvas Height=200”Name=”Canvas1”Width=300”Background=”#FF8EDE75”>
</Canvas>
 </Viewbox>  

Guess you like

Origin blog.csdn.net/qq_42334826/article/details/113118877