ContenPresenter TextBlock样式在Visual Studio ToolWindow可扩展性

时间:2016-08-25 09:43:21

标签: c# wpf controltemplate visual-studio-extensions vs-extensibility

这很奇怪。 我创建了一个名为Switch的自定义控件。我还为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>

当我将Switch的一个实例(以及TextBlock样式)添加到一个简单的窗口时,Switch中使用的ContentPresenter继承TextBlock样式。

enter image description here

但是当在Visual Studio ToolWindow Extensibility中使用Switch时,Switch中使用的ContentPresenter不会继承TextBlock样式。

注意垂直对齐和边距,它们不是样式中设置的中心和3,3,3,3。

知道为什么吗?

我尝试使用snoop查找ContentPresenter中TextBlock的值,但它们并不符合我定义的样式。

注意:我无法在所有ContentPresenters上设置TextElement附加属性,因为我有很多自定义控件,因此更喜欢为TextBlock设置样式。

1 个答案:

答案 0 :(得分:1)

“为什么”的答案可能是:隐含的TextBox样式未在工具窗口中使用。

但是,我并不会真的关心这一点。如果我要设置这个开关,我会说,好吧,我想在Track和标签之间稍微分开,我希望它们全部垂直居中。而这正是Switch风格的内容。我不会依赖文本框来实现这一目标。所以它就像这样:

        <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>