【问题标题】:WPF ToggleButton XAML stylingWPF 切换按钮 XAML 样式
【发布时间】:2015-02-04 22:01:25
【问题描述】:

所以我有两个切换按钮,我正在尝试组合 - 有点。所以第一个按钮会根据 IsChecked 是真还是假来切换图像,但是这个按钮周围有一个我无法摆脱的边框。

第二个切换按钮没有边框并且在单击时不会闪烁,但它也不会根据其状态更改图像。

我想要的是两全其美。更改图像并摆脱边框。我已经尝试了 23 种方法,但没有一种有效。

这是我正在使用的代码:

<ToggleButton x:Name="changeButBorderedBlinky" Margin="0,12,194,0" Width="82" Height="82" Background="Transparent" Padding="-1"  Focusable="false" VerticalAlignment="Top" HorizontalAlignment="Right">

        <ToggleButton.Style>
            <Style TargetType="{x:Type ToggleButton}">
                <Setter Property="Content">
                    <Setter.Value>
                        <Border BorderThickness="0"  >
                            <Image Source="images/buttonimages/back2.png" Stretch="Fill"  />
                        </Border>
                    </Setter.Value>
                </Setter>
                <Style.Triggers>
                    <Trigger Property="IsChecked" Value="True">
                        <Setter Property="Content">
                            <Setter.Value>
                                <Border  BorderThickness="0" >
                                    <Image Source="images/buttonimages/back.png" Stretch="fill" />
                                </Border>
                            </Setter.Value>
                        </Setter>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </ToggleButton.Style>

    </ToggleButton>
    <ToggleButton x:Name="noChangeNoBorder"  Margin="0,12,104,0" VerticalAlignment="Top" HorizontalAlignment="Right" Height="80" Width="80" >
        <ToggleButton.Template>
            <ControlTemplate TargetType="{x:Type ToggleButton}">
                <Border x:Name="border"  >
                    <Image x:Name="img" Source="images/buttonimages/back2.png"  />
                </Border>
            </ControlTemplate>
        </ToggleButton.Template>

    </ToggleButton>

感谢您对此的任何帮助。快把我逼疯了。

【问题讨论】:

    标签: wpf xaml togglebutton wpf-style


    【解决方案1】:

    尝试使用与您的第一个 ToggleButton 相关的稍作修改的 XAML:

    <ToggleButton x:Name="changeButBorderedBlinky" 
                  Margin="0,12,194,0"
                  Width="82" Height="82" 
                  Background="Transparent" 
                  Padding="-1"  
                  Focusable="false" 
                  VerticalAlignment="Top" HorizontalAlignment="Right">
    
            <ToggleButton.Style>
                <Style TargetType="{x:Type ToggleButton}">
                    <Setter Property="BorderThickness" Value="0"/>
                    <Setter Property="Content">
                        <Setter.Value>
    
                            <Image Source="images/buttonimages/back2.png" Stretch="Fill"  />
    
                        </Setter.Value>
                    </Setter>
                    <Style.Triggers>
                        <Trigger Property="IsChecked" Value="True">
                            <Setter Property="Content">
                                <Setter.Value>
    
                                    <Image Source="images/buttonimages/back.png" Stretch="fill" />
    
                                </Setter.Value>
                            </Setter>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </ToggleButton.Style>
        </ToggleButton>
    

    您也可以尝试自定义其他属性,例如:

      <Setter Property="Background" Value="#FF1F3B53"/>
      <Setter Property="Foreground" Value="#FF000000"/>
      <Setter Property="BorderBrush" Value="Transparent">
    

    更多样式选项请参考:http://msdn.microsoft.com/en-us/library/cc296245%28v=vs.95%29.aspx

    希望这会有所帮助。问候,

    【讨论】:

    • 我怎么知道哪个按钮被选中,哪个不是呢?
    【解决方案2】:

    无论哪种情况,您的自定义选项都是无穷无尽的。你试过Expression Blend吗?它附带Visual Studio 2013 Community(免费使用),它可以让您以任何您想要的方式自定义控件。

    这里,使用 Expression Blend 完成,没有闪烁,没有边框,图像交换:

            <ToggleButton x:Name="changeButBorderedBlinky" Margin="0,12,194,0" Width="82" Height="82" Background="Transparent" Padding="-1"  Focusable="false" VerticalAlignment="Top" HorizontalAlignment="Right">
            <ToggleButton.Template>
                <ControlTemplate TargetType="{x:Type ButtonBase}">
                    <ContentPresenter x:Name="contentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" ContentStringFormat="{TemplateBinding ContentStringFormat}" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                    <ControlTemplate.Triggers>
                        <Trigger Property="Button.IsDefaulted" Value="True"/>
                        <Trigger Property="IsMouseOver" Value="True"/>
                        <Trigger Property="IsPressed" Value="True"/>
                        <Trigger Property="ToggleButton.IsChecked" Value="True"/>
                        <Trigger Property="IsEnabled" Value="False">
                            <Setter Property="TextElement.Foreground" TargetName="contentPresenter" Value="#FF838383"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </ToggleButton.Template>
            <ToggleButton.Style>
                <Style TargetType="{x:Type ToggleButton}">
                    <Setter Property="Content">
                        <Setter.Value>
                            <Border BorderThickness="0"  >
                                <Image Source="images/buttonimages/back2.png" Stretch="Fill"  />
                            </Border>
                        </Setter.Value>
                    </Setter>
                    <Style.Triggers>
                        <Trigger Property="IsChecked" Value="True">
                            <Setter Property="Content">
                                <Setter.Value>
                                    <Border  BorderThickness="0" >
                                        <Image Source="images/buttonimages/back.png" Stretch="fill" />
                                    </Border>
                                </Setter.Value>
                            </Setter>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </ToggleButton.Style>
    
        </ToggleButton>
    

    【讨论】:

    • 太棒了,穆尔文。这样可行。我从来没有使用过混合。有什么好的资源可以用来学习这个?我知道我可以 google,但如果有建议,我宁愿从那里开始。
    • Expression Blend 是 Visual Studio 的姊妹工具。从 2012 版开始包含在其中。您可以在 Visual Studio 中右键单击 XAML 文件,您将看到“在 Blend 中打开”选项,因为它是一个所见即所得的环境,您只需四处探索即可轻松开始熟悉它。 MSDN 中有关于如何执行某些操作(例如动画和状态)的特定教程。还有一些教程网站,如 Lynda.com 和 Pluralsight。您也可以在这里开始探索并提出问题?
    猜你喜欢
    • 2018-02-13
    • 1970-01-01
    • 2014-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-08
    相关资源
    最近更新 更多