WPF扩展器按钮样式,带+;及-

WPF扩展器按钮样式,带+;及-,wpf,styles,Wpf,Styles,样式专家们,我需要帮助提出一个类似于VisualStudio代码编辑器的扩展样式。到目前为止,我已经想到了这个 <ControlTemplate x:Key="ExpanderToggleButton" TargetType="ToggleButton"> <Border SnapsToDevicePixels="true" Height

样式专家们,我需要帮助提出一个类似于VisualStudio代码编辑器的扩展样式。到目前为止,我已经想到了这个

<ControlTemplate x:Key="ExpanderToggleButton" TargetType="ToggleButton">               
                <Border
                    SnapsToDevicePixels="true"
                    Height="12" 
                    Name="Border"
                    CornerRadius="0"                   
                    Margin="2,4" 
                    Background="Transparent"                      
                    BorderBrush="Black"                       
                    BorderThickness="0.5" >                   
                <TextBlock  Name="Arrow" Text="+"  
                            Foreground="{StaticResource GlyphBrush}" 
                            HorizontalAlignment="Center"  VerticalAlignment="Center" />
            </Border>
                <ControlTemplate.Triggers>                  
                <Trigger Property="IsChecked" Value="true">                      
                    <Setter TargetName="Arrow" Property="Text" Value="-" />
                </Trigger>                 
            </ControlTemplate.Triggers>
        </ControlTemplate>

看起来像是处于崩溃状态

它显然需要一些微调,这正是我需要帮助的地方。我的挑战是获得正确的按钮内容及其垂直对齐。在附图中,扩展器只是被添加到一个对接面板上,所以我不明白为什么按钮内容没有按规定居中

TIA.

这是我使用的一种风格

<Style x:Key="ExpandCollapseToggleStyle" TargetType="ToggleButton">
    <Setter Property="Focusable" Value="False"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ToggleButton">
                <Grid Width="14" Height="14">
                    <Rectangle Fill="{DynamicResource primaryBackgroundBrush}" />
                    <Border Name="ExpandBorder" RenderOptions.EdgeMode="Aliased" BorderBrush="Black" BorderThickness="2">
                        <Path RenderOptions.EdgeMode="Aliased" Name="ExpandPath" Stroke="Black" Margin="0" StrokeThickness="2" Data="M 5 1 L 5 9 M 1 5 L 9 5" />
                    </Border>
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsChecked" Value="True">
                        <Setter Property="Data" TargetName="ExpandPath" Value="M 1 5 L 9 5"/>
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter TargetName="ExpandBorder" Property="BorderBrush" Value="Gray" />
                        <Setter TargetName="ExpandPath" Property="Stroke" Value="Gray" />
                        <Setter Property="Data" TargetName="ExpandPath" Value=""/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>


感谢分享您的模板。PrimaryBackgroundBrush由什么组成?这是一个基于用户主题设置的动态资源。现在看着它,我意识到可以移动到控件中边界的背景。效果很好,谢谢。但是,它有一点奇怪,例如,如果将边框和矩形的厚度减少到1,则矩形不再在网格中居中。@Klaus Nji-I通过将Path元素封闭在网格中来修复它。例如:将此复制到Window.Resources(并添加缺少的标记)时,不会发生任何事情。是否有一种特殊的方式告诉扩展器使用其切换按钮的样式?这个答案过时了吗(从7年前开始)?