WPF学习(一) - XAML

Window、Grid、TextBox、Button等,都叫元素

xaml文档中,<>用来定义标签,标签可以用来描述元素或元素的属性,如:
  <Window>
    <Window.Resources>
    </Window.Resources>
  </Window>
Window是元素,Resources是Window的一个属性

标签内容可以包含其他元素的标签,如
  <Window>
    <Grid>
      <Button />
    </Grid>
  </Window>
有些元素只能包含一个子元素,有些能包含很多子元素。

每个元素都可以有属性,如
  <Window>
    <Grid>
      <Button Height = "30"/>
    </Grid>
  </Window>

描述元素的属性还可以用属性标签的方式来写
  <Window>
    <Grid>
      <Button>
        <Button.Height>40</Button.Height>
      </Button>
    </Grid>
  </Window>
属性标签更有层次,但超级啰嗦,没有使用的必要

属性的值可以是字符串,也可以用标记扩展的形式完成类似绑定数据等复杂的操作。
  <Window>
    <Grid>
      <TextBox Text ="{Binding ElementName= slider1, Path=Value, Mode=OneWay}"/>
      <Slider Height="19" Name="slider1" Width="139" />
    </Grid>
  </Window>

用属性标签的写法,可以写成这样
  <TextBox.Text>
    <Binding ElementName ="slider1" Path ="Value" Mode="OneWay"/>
   </TextBox.Text>
这种写法在vs2010下很不给力,智能提示不到位……

属性不仅可以是元素自身的,也可以是其他元素的属性
引用其他元素属性的用法叫做附加属性,被引用的元素必须要写明所在的命名空间前缀
  <Window>
    <Grid>
      <Button x:Name = "btn1" Grid.Row = "2"/>
    </Grid>
  </Window>

Grid.Row没有前缀?那是因为有个默认的命名空间xmlns,使用它包含的类时就不用写明命名空间前缀

命名空间在根元素的属性中定义
  <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="App1.Window3">

  </Window>

xmlns定义的即是默认的命名空间,这里把它指向了WPF所有的控件类。
xmlns:x 定义了另一个命名空间,这里的x叫做命名空间前缀,理解成简称也可以。
x:Class 是附加属性,指明后置代码中的cs类名称

x命名空间下的类用途乱七八糟的,记住名字就行
  x:Name
  x:Class
  x:Subclass
  x:Type
  x:Null
  x:Key
  x:Array
  x:Static
  x:Shared

  x:Code
  x:XData

xmal 是标记语言,如同html一样。类似Jquery,.net也提供了两个帮助类:VisualTreeHelper、LogicalTreeHelper

原文地址:https://www.cnblogs.com/ww960122/p/4590640.html