长时间以来,我一直在寻找质量WPF图像按钮控件。我需要的按钮,包括一个图像只有,并让我设置图像选项为悬停和按。 SO和网络其余部分的所有当前解决方案都包括基于CustomTemplate的不太友好的解决方案(即TriState Button)。WPF图像按钮控件
也许有一组控件像现代UI或MahApps有人能指出我有这样的按钮吗?
长时间以来,我一直在寻找质量WPF图像按钮控件。我需要的按钮,包括一个图像只有,并让我设置图像选项为悬停和按。 SO和网络其余部分的所有当前解决方案都包括基于CustomTemplate的不太友好的解决方案(即TriState Button)。WPF图像按钮控件
也许有一组控件像现代UI或MahApps有人能指出我有这样的按钮吗?
编辑:该控件可以在整个项目中重复使用多次,就像其他控件一样,并且与其他控件库的使用方式相同。没有边界效果,你可以添加任何你想要的悬停效果。这是它的工作原理。
将名为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"/>
你可以试试这个
<Button Background="Transparent" BorderThickness="0">
<Image Source="Azure Automation.jpg"/>
</Button>
这种方式没有图像悬停效果。 – eYe
@eYe在这种情况下,你需要编写按钮模板 –
在这里你有:https://imagebuttonwpf.codeplex.com/releases/view/70356
您必须下载代码,编译它,然后在您的项目中使用生成的DLL。
检查代码,制作起来非常简单,并且遵循CJK的实际控制指令,您可以立即自行完成!
简单地将图像设置为按钮的内容有什么问题? – Domysee
悬停图像和Button控件的边界效果如何? – eYe