0

我想要做的是一个开/关切换按钮,因为我使用图像作为资源,所以我的主要想法是将一个图像设置为正常的 ON 状态,另一个用于鼠标悬停 ON 状态,第三个用于按下 ON 状态。关闭状态也是如此,所有这些都在一个按钮中。我对普通按钮做了完全相同的事情,但在这里我遇到了一些我不知道的事情:在 xaml 中使用所有触发器执行 IF 条件,我什至不知道从哪里开始。我读了一些关于多重触发器的东西,但这似乎很复杂——有更简单的方法吗?这就是我到目前为止所做的。

 <ToggleButton IsChecked="False" Height="70" Width="70" >
        <ToggleButton.Background>
            <ImageBrush ImageSource="Resources\off_button_1.png" Stretch="None" />
        </ToggleButton.Background>
        <ToggleButton.Style>
            <Style TargetType="{x:Type ToggleButton}" >
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type ToggleButton}" >
                            <StackPanel Orientation="Horizontal">
                                <Image x:Name="image" Source="Resources\off_button_1.png" Margin="0" Stretch="None" />

                            </StackPanel>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsMouseOver" Value="true">
                                    <Setter TargetName="image" Property="Source" Value="Resources/off_button_1_hover.png" />

                                </Trigger>
                                <Trigger Property="IsPressed" Value="true">
                                    <Setter TargetName="image" Property="Source" Value="Resources/off_button_1_pressed.png" />
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </ToggleButton.Style>
    </ToggleButton>

我真的会在这里使用一些帮助,提前谢谢你。

4

1 回答 1

0

您可以创建自定义按钮。

public class ImageButton : Button
{
    static ImageButton()
    { 
        DefaultStyleKeyProperty.OverrideMetadata(typeof(ImageButton), new FrameworkPropertyMetadata(typeof(ImageButton)));
    }


    public ImageSource NormalImage
    {
        get { return (ImageSource)GetValue(NormalImageProperty); }
        set { SetValue(NormalImageProperty, value); }
    }

    public ImageSource HoverImage
    {
        get { return (ImageSource)GetValue(HoverImageProperty); }
        set { SetValue(HoverImageProperty, value); }
    }

    public ImageSource PressedImage
    {
        get { return (ImageSource)GetValue(PressedImageProperty); }
        set { SetValue(PressedImageProperty, value); }
    }

 public static readonly DependencyProperty NormalImageProperty =
      DependencyProperty.Register(
          "NormalImage", typeof(ImageSource), typeof(ImageButton));

    public static readonly DependencyProperty HoverImageProperty =
        DependencyProperty.Register(
            "HoverImage", typeof(ImageSource), typeof(ImageButton));        

    public static readonly DependencyProperty PressedImageProperty =
        DependencyProperty.Register(
            "PressedImage", typeof(ImageSource), typeof(ImageButton));

}

这是 XAML ControlTemplate

<Style TargetType="{x:Type local:ImageButton}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:ImageButton}">
                <Border Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">
                    <Image x:Name="PART_Image" Source="{Binding NormalImage, RelativeSource={RelativeSource TemplatedParent}}"/>
                </Border>

                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Source" Value="{Binding HoverImage, RelativeSource={RelativeSource TemplatedParent}}" TargetName="PART_Image"/>
                    </Trigger>
                    <Trigger Property="IsPressed" Value="True">
                        <Setter Property="Source" Value="{Binding PressedImage, RelativeSource={RelativeSource TemplatedParent}}" TargetName="PART_Image"/>
                    </Trigger>                                          
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
于 2013-07-15T03:21:48.233 回答