XAML揭祕

 

  1. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  主要命名空間

  2.  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 次要命名空間

用如下方式創建按鈕

<WpfNamespace:Button
        xmlns:WpfNamespace="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 Content="PK"/>

在按鈕中填充一個矩形

<Button xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
    <Button.Content>
        <Rectangle Height="40" Width="50" Fill="Black"></Rectangle>
    </Button.Content>
</Button>

類型轉換器繼承自TypeConverter類

<Button xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" Content="OK">
    <Button.Background>   
        <SolidColorBrush Color="White"/>
    </Button.Background>
</Button>

 

<Button xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" Content="OK">
    <Button.Background>
        <SolidColorBrush>        
            <SolidColorBrush.Color>          
                <Color A="255" R="255" G="255" B="255"></Color>
            </SolidColorBrush.Color>
        </SolidColorBrush>
    </Button.Background>
</Button> 

 

標記擴展

派生自MarkUpExtension

標記擴展省略Extension

StaticExtension可以是靜態屬性字段和枚舉值

逗號分隔參數

SystemParameters.IconHeight 定位參數

Path  RelativeSource命名參數可以在已構造好的擴展對象上設置相應名字的屬性,這些屬性可以是標記擴展值自己,也可以是文本值

Binding沒有標記擴展 繼承自System.Windows.Data

<Button xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Background="{x:Null}"
        Height="{x:Static SystemParameters.IconHeight}"
        Content="{Binding Path=Height,RelativeSource={RelativeSource Self}}"/>

標記擴展與屬性結合,標記擴展有默認構造函數的類

<Button xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Button.Background>
        <x:Null/>
    </Button.Background>
    <Button.Height>
        <x:Static Member="SystemParameters.IconHeight"/>
    </Button.Height>
    <Button.Content>
        <Binding Path="Height" RelativeSource="{RelativeSource Self}" />
    </Button.Content>
</Button>

避免聲明爲標記擴展

1

<Button xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
    {This is not a markup extension}
</Button>

2

<Button xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        Content="{}{This is not a markup extension}">
</Button>

 

對象元素的子元素

內容屬性

<Button xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Rectangle Height="40" Width="40" Fill="Black"/>
</Button>

集合項

<ListBox xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
    <ListBox.Items>
        <ListBoxItem Content="Item1"/>
        <ListBoxItem Content="Item2"/>
    </ListBox.Items>
</ListBox>

<ListBox xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
        <ListBoxItem Content="Item1"/>
        <ListBoxItem Content="Item2"/>
</ListBox>

 

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Color x:Key="1" A="255" R="255" G="255" B="255"/>
    <Color x:Key="2" A="0" R="0" G="0" B="0"/>
</ResourceDictionary>

發佈了5 篇原創文章 · 獲贊 3 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章