带有 + 和 - 的 WPF 扩展器按钮样式 [英] WPF Expander button style with + and -

查看:24
本文介绍了带有 + 和 - 的 WPF 扩展器按钮样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

造型大师,我需要帮助想出一种看起来像在 Visual Studio 的代码编辑器中找到的扩展器样式.到目前为止,我已经想出了这个

Styling gurus, I need help coming up with an Expander style that looks like the one found in Visual Studio's code editor. So far, I have come up with this

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

在折叠状态下看起来像这样.

which looks like this in collapsed state.

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

It clearly needs some fine tuning, which is what I need help with. My challenge is getting the correct button content and its vertical alignment. In the attached figure, the expanders are simply being added to a dock panel, so I do not understand why the button content is not centered as specified.

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>

这篇关于带有 + 和 - 的 WPF 扩展器按钮样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆