6
自從我開始使用MS的控件模板示例作爲構建自定義控件的基礎之後,我一直在想這一點。爲什麼在xaml,WPF中的樣式中定義模板?
採取標籤例子如:http://msdn.microsoft.com/en-us/library/ms752327.aspx
爲什麼地球上是這樣定義的:
<Style x:Key="{x:Type Label}" TargetType="Label">
<Setter Property="HorizontalContentAlignment" Value="Left" />
<Setter Property="VerticalContentAlignment" Value="Top" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Label">
<Border>
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
RecognizesAccessKey="True" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground">
<Setter.Value>
<SolidColorBrush Color="{DynamicResource DisabledForegroundColor}" />
</Setter.Value>
</Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
並沒有直接像這樣:
<ControlTemplate x:Key="{x:Type Label}" TargetType="Label">
<Border>
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
RecognizesAccessKey="True" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground">
<Setter.Value>
<SolidColorBrush Color="{DynamicResource DisabledForegroundColor}" />
</Setter.Value>
</Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
然後叫作爲模板直接,而不是通過樣式屬性?
有沒有隱藏的原因,我沒有看到做這樣的事情?還是隻是一種做事的方式,就是這樣?
(NB!不要告訴我這是由於水平和垂直對齊制定者,我們都知道這些都是一個標籤的默認值,這是基本沒用,如果你讓那些值)
oy,雖然它對模板和樣式的工作方式相同。我會盡我所能去親眼看到它(我無法相信這兩種行爲會有所不同......)至於你答案的第二部分,我不得不不同意(你在這個問題上看到了我的另一個問題) 。仍然沒有找到一個有效的解釋,我得到的行爲,但刪除x:鍵並不總是工作,因爲它應該(至少對我來說)... – David 2011-02-02 09:22:32