【发布时间】:2011-03-17 16:23:40
【问题描述】:
我认为元素属性语法和属性属性语法没有太大的语义差异。但是,我发现肯定有一些不同。
例如下面的例子只是演示了一个简单的触发器:
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Button><Button.Template>
<ControlTemplate TargetType="{x:Type Button}">
<TextBlock x:Name="hello" Text="Hello" />
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Foreground" Value="Red" TargetName="hello"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Button.Template></Button>
</Page>
但是,如果我对触发器的属性Property 使用元素属性语法,它会抛出一个异常,说setter! (不是触发器)需要属性和值。
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Button><Button.Template>
<ControlTemplate TargetType="{x:Type Button}">
<TextBlock x:Name="hello" Text="Hello" />
<ControlTemplate.Triggers>
<Trigger Value="True">
<Trigger.Property>IsMouseOver</Trigger.Property>
<Setter Property="Foreground" Value="Red" TargetName="hello"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Button.Template></Button>
</Page>
那么,元素属性语法和属性属性语法的隐藏区别是什么?
【问题讨论】:
标签: c# .net wpf xaml exception