WPF图像按钮控件

WPF image button control

我一直在寻找高质量的 WPF 图像按钮控件。我需要按钮仅包含一个 图像 ,并让我为 悬停和按下 设置图像选项。 SO 和网络其余部分的所有当前解决方案都包括基于 CustomTemplate 的解决方案,这些解决方案不是很友好(即 TriState Button)。

也许有一组控件,例如 Modern UIMahApps,有人可以指出我有这个那种按钮?

编辑: 这个控件可以在整个项目中根据需要重复使用多次,它的行为与任何其他控件完全一样,并且使用方式与任何其他控件库相同.没有边框效果,您可以添加任何您想要的悬停效果。这就是它的工作原理。

将名为 CustomControls 的文件夹添加到您的解决方案。

在该文件夹中创建一个名为 ImageButton 的自定义控件 (WPF)。自定义控件代码放在那里。

现在应该已经在您的项目中创建了一个名为 Themes 的文件夹,其中包含一个名为 generic.xaml 的资源字典。打开它并使用资源字典代码。

现在将其添加到您的 window 标签

xmlns:b="clr-namespace:MyProject.CustomControls"

现在,您可以通过在答案末尾使用标签结构,像使用常规按钮一样多次使用 window 中的控件。

这是我制作的一个,您可以从主 window 设置图像源、高度和宽度,所有标准按钮事件都在那里。

这里是自定义控件

namespace MyProject.CustomControls
{
public class ImageButton : Button
{
     public static DependencyProperty SourceProperty =
        DependencyProperty.Register(
            "Source",
            typeof(Uri),
            typeof(ImageButton));

    static ImageButton()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(ImageButton), new FrameworkPropertyMetadata(typeof(ImageButton)));
    }

    public Uri Source
    {
        get { return (Uri)GetValue(SourceProperty); }
        set { SetValue(SourceProperty, value); }
    }
}

}

这是资源词典中的样式,您可以在触发器部分添加鼠标悬停事件和 button.pressed 事件,或者删除触发器并在 window 上设置它们。

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:b="clr-namespace:MyProject.CustomControls">

<Style x:Key="{x:Type b:ImageButton}" TargetType="{x:Type b:ImageButton}">
    <Setter Property="Height" Value="{Binding Path=Height, RelativeSource={RelativeSource TemplatedParent}}"/>
    <Setter Property="Width" Value="{Binding Path=Width, RelativeSource={RelativeSource TemplatedParent}}"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type b:ImageButton}">
                <Grid x:Name="LayoutGrid">
                    <Image x:Name="_Image" HorizontalAlignment="Center" VerticalAlignment="Center" Width="{TemplateBinding Width}"
                           Source="{Binding Path=Source, RelativeSource={RelativeSource TemplatedParent}}" Height="{TemplateBinding Height}"/>
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="" TargetName="" Value=""/>
                    </Trigger>
                    <Trigger Property="Button.IsPressed" Value="True">
                        <Setter Property="" TargetName="" Value=""/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

要在您的项目中使用它,请将命名空间添加到 window,然后标签将像这样

<b:ImageButton Source="Image.png" Height="50" Width="50" Click="do_Something"/>

你可以试试这个

    <Button Background="Transparent" BorderThickness="0">
            <Image Source="Azure Automation.jpg"/>
    </Button>

这里有:https://imagebuttonwpf.codeplex.com/releases/view/70356

您必须下载代码,编译它,然后在您的项目中使用生成的 DLL。

查看代码,制作起来非常简单,按照实际控件的CJK说明,您很快就可以自己完成!