Reputation: 3204
This is weird. I've created a custom control called Switch. I've also defined a style for TextBlock.
<!-- Switch -->
<Style TargetType="{x:Type controls:Switch}">
<Setter Property="Margin"
Value="3,3,3,3" />
<Setter Property="MinWidth"
Value="40" />
<Setter Property="MinHeight"
Value="24" />
<Setter Property="On"
Value="ON" />
<Setter Property="Off"
Value="OFF" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type controls:Switch}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<ContentPresenter Grid.Column="0"
Content="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=Off}" />
<Border Grid.Column="1"
Background="{TemplateBinding Property=Background}"
BorderBrush="{TemplateBinding Property=BorderBrush}"
BorderThickness="{TemplateBinding Property=BorderThickness}"
CornerRadius="12,12,12,12"
HorizontalAlignment="Stretch"
VerticalAlignment="Center">
<Track x:Name="PART_Track"
Minimum="0"
Maximum="1"
Orientation="Horizontal"
Value="0">
<Track.Thumb>
<Thumb x:Name="PART_Thumb"
Style="{DynamicResource ResourceKey=SwitchThumb}" />
</Track.Thumb>
</Track>
</Border>
<ContentPresenter Grid.Column="2"
Content="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=On}" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<!-- Text Block -->
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Margin"
Value="3,3,3,3" />
<Setter Property="HorizontalAlignment"
Value="Left" />
<Setter Property="VerticalAlignment"
Value="Center" />
<Setter Property="TextAlignment"
Value="Left" />
<Setter Property="TextWrapping"
Value="Wrap" />
</Style>
When I add an instance of Switch (along with TextBlock style) to a simple window, the ContentPresenter used within the Switch is inheriting TextBlock style.
But when the Switch is used within Visual Studio ToolWindow Extensibility, the ContentPresenter used within the Switch does not inherit the TextBlock style.
Notice the Vertical Alignment and Margin, they are not Center and 3,3,3,3 as set in the Style.
Any idea why?
I tried using snoop to find out the values of the TextBlock within ContentPresenter and they are not according to the style I have defined.
NOTE: I cannot set TextElement Attached Properties on all the ContentPresenters as I have a lot of custom controls and therefore would prefer setting a style for TextBlock instead.
Upvotes: 0
Views: 46
Reputation: 7906
The answer to "why" is probably: the implicit TextBox
Style is not being used in the Tool Window.
I wouldn't really care for that though. If I were to style this switch I'd say, OK, I want a little separation between the Track
and the labels and I want them all vertically centered. And that's exactly what would go in the Switch
style. I wouldn't rely on the textbox to accomplish that. So it'd be like this:
<ControlTemplate TargetType="{x:Type controls:Switch}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<ContentPresenter Grid.Column="0"
Content="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=Off}" VerticalAlignment="Center"/>
<Border Grid.Column="1"
Background="{TemplateBinding Property=Background}"
BorderBrush="{TemplateBinding Property=BorderBrush}"
BorderThickness="{TemplateBinding Property=BorderThickness}"
CornerRadius="12,12,12,12"
HorizontalAlignment="Stretch"
VerticalAlignment="Center" Margin="3,0">
<Track x:Name="PART_Track"
Minimum="0"
Maximum="1"
Orientation="Horizontal"
Value="0">
<Track.Thumb>
<Thumb x:Name="PART_Thumb"
Style="{DynamicResource ResourceKey=SwitchThumb}" />
</Track.Thumb>
</Track>
</Border>
<ContentPresenter Grid.Column="2"
Content="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=On}" VerticalAlignment="Center"/>
</Grid>
</ControlTemplate>
Upvotes: 1