2015-05-26 117 views
0

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

也许有一组控件像现代UIMahApps有人能指出我有这样的按钮吗?

+3

简单地将图像设置为按钮的内容有什么问题? – Domysee

+0

悬停图像和Button控件的边界效果如何? – eYe

回答

2

编辑:该控件可以在整个项目中重复使用多次,就像其他控件一样,并且与其他控件库的使用方式相同。没有边界效果,你可以添加任何你想要的悬停效果。这是它的工作原理。

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

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

现在应该在项目中创建一个名为Themes的文件夹,其中包含一个名为generic.xaml的resourcedictionary。打开它并使用resourcedictionary代码。

现在,通过使用标签结构放在结尾添加到您的窗口标签

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

现在你可以使用在该窗口中多次控制,只要你愿意,就像一个普通按钮回答。

这是我制作的一个,你可以在主窗口中设置图片源,高度和宽度,并且所有标准按钮事件都在那里。

这里是自定义控件

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); } 
    } 
} 

}

这里是在ResourceDictionary中的样式,你可以在触发器部分中添加鼠标悬停事件和button.pressed事件或删除触发器和设置他们在你的窗户上。

<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> 

要使用它,在你的项目的命名空间添加到窗口,然后标签将是这个样子

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

我正在寻找更通用的控件,而不是模板解决方案 – eYe

+0

你是什么意思,更通用? – CJK

+0

库类型,很好地包装控制/ DLL等 – eYe

0

你可以试试这个

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

这种方式没有图像悬停效果。 – eYe

+0

@eYe在这种情况下,你需要编写按钮模板 –

1

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

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

检查代码,制作起来非常简单,并且遵循CJK的实际控制指令,您可以立即自行完成!

+0

说“项目尚未发布”,当我点击链接 – eYe

+0

是真的,不能发布它没有上传代码... Mpf!我会尝试找到另一个地方上传它:/ – almulo

+0

好吧,没关系,我发现了另一个由其他人制作的控件,在Codeplex中...更新我的答案。 – almulo