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>