WPF ToggleButton XAML样式

时间:2014-12-06 23:36:42

标签: wpf xaml togglebutton wpf-style

所以我有两个切换按钮,我试图结合 - 有点。所以第一个按钮根据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>

感谢您提供任何帮助。这让我疯了。

2 个答案:

答案 0 :(得分:5)

无论哪种情况,您的自定义选项都是无穷无尽的。你有没有试过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>

答案 1 :(得分: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

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