長時間以來,我一直在尋找質量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