WPF ToggleButton XAML 样式

问题描述 投票:0回答:2

我有两个切换按钮,我正在尝试将它们组合起来。因此,第一个按钮根据 IsChecked 是 true 还是 false 来切换图像,但该按钮周围有一个我无法摆脱的边框。

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

我想要的是两全其美。更改图像并去掉边框。我已经尝试了 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
2个回答
7
投票

无论哪种情况,您的定制选项都是无穷无尽的。您尝试过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>

2
投票

尝试使用与您的第一个 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

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

© www.soinside.com 2019 - 2024. All rights reserved.