WPF图像按钮控件
很长一段时间以来,我一直在搜索质量WPF图像按钮控件。我需要按钮只包含一个图像,并让我为悬停和按下设置图像选项。SO和web其余部分上的所有当前解决方案都包括基于CustomTemplate的解决方案,这些解决方案不太友好(即)WPF图像按钮控件,wpf,button,controls,Wpf,Button,Controls,很长一段时间以来,我一直在搜索质量WPF图像按钮控件。我需要按钮只包含一个图像,并让我为悬停和按下设置图像选项。SO和web其余部分上的所有当前解决方案都包括基于CustomTemplate的解决方案,这些解决方案不太友好(即) 也许有一组控件,比如Modern UI或MahApps,有人可以指给我看,它们有这种按钮吗 编辑:此控件可以根据需要在整个项目中重复使用多次。它的作用与任何其他控件完全相同,使用方式与任何其他控件库相同。没有边界效果,你可以添加任何你想要的悬停效果。这就是它的工作原理
也许有一组控件,比如Modern 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中的样式,您可以在触发器部分添加mouseover事件和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"/>
编辑:此控件可以在整个项目中根据需要多次重复使用。它的作用与任何其他控件完全相同,使用方式与任何其他控件库相同。没有边界效果,你可以添加任何你想要的悬停效果。这就是它的工作原理 将名为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中的样式,您可以在触发器部分添加mouseover事件和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"/>
编辑:此控件可以在整个项目中根据需要多次重复使用。它的作用与任何其他控件完全相同,使用方式与任何其他控件库相同。没有边界效果,你可以添加任何你想要的悬停效果。这就是它的工作原理 将名为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中的样式,您可以在触发器部分添加mouseover事件和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"/>
编辑:此控件可以在整个项目中根据需要多次重复使用。它的作用与任何其他控件完全相同,使用方式与任何其他控件库相同。没有边界效果,你可以添加任何你想要的悬停效果。这就是它的工作原理 将名为CustomControls的文件夹添加到解决方案中 在该文件夹中创建名为ImageButton的自定义控件(WPF)。自定义控制代码在那里 现在,一个名为Themes的文件夹应该已被删除