天天看點

WPF筆記(1.3 屬性元素)——Hello,WPF!

這一節中“屬性元素”的概念可以用匪夷所思形容。

1。WPF用标簽元素實作對象模組化,有兩種:Control和Container,都用來裝載内容和行為,前者如Button,後者如Window。

你可以這樣寫:

WPF筆記(1.3 屬性元素)——Hello,WPF!

<Window 

WPF筆記(1.3 屬性元素)——Hello,WPF!

>

WPF筆記(1.3 屬性元素)——Hello,WPF!

  <Button Width="100" Height="100">

WPF筆記(1.3 屬性元素)——Hello,WPF!

    <Image Source="tom.png" />

WPF筆記(1.3 屬性元素)——Hello,WPF!

  </Button>

WPF筆記(1.3 屬性元素)——Hello,WPF!

</Window>

也可以這樣:

WPF筆記(1.3 屬性元素)——Hello,WPF!
WPF筆記(1.3 屬性元素)——Hello,WPF!
WPF筆記(1.3 屬性元素)——Hello,WPF!
WPF筆記(1.3 屬性元素)——Hello,WPF!

    <TextBox Width="75">edit me</TextBox>

WPF筆記(1.3 屬性元素)——Hello,WPF!
WPF筆記(1.3 屬性元素)——Hello,WPF!

就是說,将原來Button的Image屬性和TextBox屬性當作對象提取出來。這是因為Button起源于一個類:ContentControl ,該類知道如何生成其裝載的所有控件。

2。其實完整的寫法是這樣的:

WPF筆記(1.3 屬性元素)——Hello,WPF!

<Button Width="100" Height="100">

WPF筆記(1.3 屬性元素)——Hello,WPF!

  <Button.Content>

WPF筆記(1.3 屬性元素)——Hello,WPF!
WPF筆記(1.3 屬性元素)——Hello,WPF!

  </Button.Content>

WPF筆記(1.3 屬性元素)——Hello,WPF!

</Button>

但是,<Button.Content>标簽内不能有兩個控件,會顯示文法錯誤,隻能是一個屬性元素——這時候要用Panel。

Window控件有和Button同樣的用法,見下面章節。