C# WPF 自定義按鈕的方法
本文介紹WPF一種自定義按鈕的方法。
實(shí)現(xiàn)效果
- 使用圖片做按鈕背景;
- 自定義鼠標(biāo)進(jìn)入時(shí)效果;
- 自定義按壓效果;
- 自定義禁用效果
實(shí)現(xiàn)效果如下圖所示:
實(shí)現(xiàn)步驟
- 創(chuàng)建CustomButton.cs,繼承自Button;
- 創(chuàng)建一個(gè)資源文件ButtonStyles.xaml;
- 在資源文件中設(shè)計(jì)按鈕的Style;
- 在CustomButton.cs中添加Style中需要的依賴屬性;
- 在程序中添加資源并引用(為了方便在不同的程序中引用自定義按鈕,自定義按鈕放在獨(dú)立的類庫(kù)中,應(yīng)用程序中進(jìn)行資源合并即可)。
示例代碼
// ButtonStyles.xaml <Style x:Key="CustomButton" TargetType="{x:Type local:CustomButton}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type local:CustomButton}"> <Grid x:Name="container"> <Image Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" Source="{Binding ButtonImage,RelativeSource={RelativeSource Mode=TemplatedParent}}"> <Image.RenderTransformOrigin> <Point X="0.5" Y="0.5"/> </Image.RenderTransformOrigin> <Image.RenderTransform> <ScaleTransform x:Name="scaletrans" ScaleX="1" ScaleY="1"/> </Image.RenderTransform> </Image> </Grid> <ControlTemplate.Triggers> <Trigger Property="IsEnabled" Value="False"> <Setter Property="Opacity" Value="0.5" TargetName="container"/> </Trigger> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="Background" Value="#2c000000" TargetName="container"/> </Trigger> <Trigger Property="IsPressed" Value="True"> <Trigger.EnterActions> <BeginStoryboard> <Storyboard> <DoubleAnimation Storyboard.TargetName="scaletrans" Storyboard.TargetProperty="(ScaleTransform.ScaleX)" To="0.8" Duration="0:0:0.15" AutoReverse="True"/> <DoubleAnimation Storyboard.TargetName="scaletrans" Storyboard.TargetProperty="(ScaleTransform.ScaleY)" To="0.8" Duration="0:0:0.15" AutoReverse="True"/> </Storyboard> </BeginStoryboard> </Trigger.EnterActions> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> // CustomButton.cs public class CustomButton : Button { public ImageSource ButtonImage { get { return (ImageSource)GetValue(ButtonImageProperty); } set { SetValue(ButtonImageProperty, value); } } public static readonly DependencyProperty ButtonImageProperty = DependencyProperty.Register("ButtonImage", typeof(ImageSource), typeof(CustomButton), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender)); } // App.xaml 合并資源 <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source=".../ButtonStyles.xaml"/> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources> // view.xaml 使用 <Grid> <customcontrols:CustomButton Width="48" Height="48" Style="{StaticResource CustomButton}" ButtonImage="/Louzi.Paint;component/Images/Toolbar/write.png"/> </Grid>
以上就是C# WPF 自定義按鈕的方法的詳細(xì)內(nèi)容,更多關(guān)于C# WPF 自定義按鈕的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C#通過(guò)System.CommandLine快速生成支持命令行的應(yīng)用程序
這篇文章介紹了C#通過(guò)System.CommandLine快速生成支持命令行應(yīng)用程序的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07C#快速實(shí)現(xiàn)IList非泛型類接口的自定義類作為數(shù)據(jù)源
本文主要介紹了C#快速實(shí)現(xiàn)IList非泛型類接口的自定義類作為數(shù)據(jù)源,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02C#使用ScrapySharp快速?gòu)木W(wǎng)頁(yè)采集數(shù)據(jù)
這篇文章介紹了使用ScrapySharp快速?gòu)木W(wǎng)頁(yè)采集數(shù)據(jù)的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06c#根據(jù)網(wǎng)址抓取網(wǎng)頁(yè)截屏生成圖片的示例
本文主要介紹了c#根據(jù)網(wǎng)址抓取網(wǎng)頁(yè)截屏生成圖片并保存的示例,代碼中使用了WebBrowser控件來(lái)完成這個(gè)功能,大家參考使用吧2014-01-01C#下實(shí)現(xiàn)創(chuàng)建和刪除目錄的實(shí)例代碼
這篇文章主要介紹了C#下實(shí)現(xiàn)創(chuàng)建和刪除目錄的方法,功能非常實(shí)用,需要的朋友可以參考下2014-08-08Unity UGUI實(shí)現(xiàn)卡片橢圓方向滾動(dòng)
這篇文章主要為大家詳細(xì)介紹了UGUI實(shí)現(xiàn)卡片橢圓方向滾動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-03-03C#使用MailAddress類發(fā)送html格式郵件的實(shí)例代碼
這篇文章主要介紹如何使用C#的MailAddress類發(fā)送郵件的方法,大家參考使用吧2013-11-11解析C#多線程編程中異步多線程的實(shí)現(xiàn)及線程池的使用
這篇文章主要介紹了C#多線程編程中異步多線程的實(shí)現(xiàn)及線程池的使用,同時(shí)對(duì)多線程的一般概念及C#中的線程同步并發(fā)編程作了講解,需要的朋友可以參考下2016-03-03