I have been interested about this since I started using MS template templates as a basis for creating custom controls.
take an example shortcut, for example: http://msdn.microsoft.com/en-us/library/ms752327.aspx
why it is defined as follows:
<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>
not like that:
<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>
and then called as a template directly, and not through a style property?
Is there a hidden reason why I don't see such things? or is it just one way to do something and what?
(NB: don’t tell me that this is due to the horizontal and vertical alignment setting! We all know that these are the default values for the label, and it’s basically useless if you keep these values)
templates styles wpf xaml
David
source share