WPF 實現圖標按鈕

来源:https://www.cnblogs.com/fengxinyuan/p/18295339
-Advertisement-
Play Games

假設需要實現一個圖標和文本結合的按鈕 ,普通做法是 直接重寫該按鈕的模板; 如果想作為通用的呢? 兩種做法: 附加屬性 自定義控制項 推薦使用附加屬性的形式 第一種:附加屬性 創建Button的附加屬性 ButtonExtensions 1 public static class ButtonExte ...


假設需要實現一個圖標和文本結合的按鈕 ,普通做法是 直接重寫該按鈕的模板;

如果想作為通用的呢?

兩種做法:

  1. 附加屬性
  2. 自定義控制項

推薦使用附加屬性的形式

第一種:附加屬性

創建Button的附加屬性  ButtonExtensions

 1 public static class ButtonExtensions
 2 {
 3     // Using a DependencyProperty as the backing store for IconWidth.  This enables animation, styling, binding, etc...
 4     public static readonly DependencyProperty IconWidthProperty =
 5         DependencyProperty.RegisterAttached("IconWidth", typeof(int), typeof(ButtonExtensions), new PropertyMetadata(0));
 6 
 7     public static int GetIconWidth(DependencyObject obj)
 8     {
 9         return (int)obj.GetValue(IconWidthProperty);
10     }
11 
12     public static void SetIconWidth(DependencyObject obj, int value)
13     {
14         obj.SetValue(IconWidthProperty, value);
15     }
16 
17     // Using a DependencyProperty as the backing store for IconHeight.  This enables animation, styling, binding, etc...
18     public static readonly DependencyProperty IconHeightProperty =
19         DependencyProperty.RegisterAttached("IconHeight", typeof(int), typeof(ButtonExtensions), new PropertyMetadata(0));
20 
21     public static int GetIconHeight(DependencyObject obj)
22     {
23         return (int)obj.GetValue(IconHeightProperty);
24     }
25 
26     public static void SetIconHeight(DependencyObject obj, int value)
27     {
28         obj.SetValue(IconHeightProperty, value);
29     }
30 
31     // Using a DependencyProperty as the backing store for IconGeometry.  This enables animation, styling, binding, etc...
32     public static readonly DependencyProperty IconGeometryProperty =
33         DependencyProperty.RegisterAttached("IconGeometry", typeof(Geometry), typeof(ButtonExtensions), new PropertyMetadata((object)null));
34 
35     public static Geometry GetIconGeometry(DependencyObject obj)
36     {
37         return (Geometry)obj.GetValue(IconGeometryProperty);
38     }
39 
40     public static void SetIconGeometry(DependencyObject obj, Geometry value)
41     {
42         obj.SetValue(IconGeometryProperty, value);
43     }
44 
45 }

樣式

 1 <ResourceDictionary
 2     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 3     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 4     xmlns:coreHelper="clr-namespace:NeonGenesis.Core.AttachedProperties;assembly=NeonGenesis.Core">
 5     <Style x:Key="ButtonVerBase" TargetType="{x:Type Button}">
 6         <Setter Property="BorderThickness" Value="0" />
 7         <Setter Property="HorizontalContentAlignment" Value="Center" />
 8         <Setter Property="VerticalContentAlignment" Value="Center" />
 9         <Setter Property="Padding" Value="10,5" />
10         <Setter Property="FrameworkElement.Cursor" Value="Hand" />
11         <Setter Property="UIElement.SnapsToDevicePixels" Value="True" />
12         <Setter Property="coreHelper:ButtonExtensions.IconHeight" Value="24" />
13         <Setter Property="coreHelper:ButtonExtensions.IconWidth" Value="24" />
14         <Setter Property="Template">
15             <Setter.Value>
16                 <ControlTemplate TargetType="{x:Type ButtonBase}">
17                     <Border
18                         Name="border"
19                         Background="{TemplateBinding Background}"
20                         BorderBrush="{TemplateBinding BorderBrush}"
21                         BorderThickness="{TemplateBinding BorderThickness}"
22                         SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}">
23                         <Grid>
24                             <StackPanel
25                                 Margin="{TemplateBinding Padding}"
26                                 HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
27                                 VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
28                                 Orientation="Vertical">
29                                 <Path
30                                     Name="pathIcon"
31                                     Width="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=(coreHelper:ButtonExtensions.IconWidth)}"
32                                     Height="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=(coreHelper:ButtonExtensions.IconHeight)}"
33                                     Margin="0,0,0,5"
34                                     Data="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=(coreHelper:ButtonExtensions.IconGeometry)}"
35                                     Fill="{TemplateBinding Foreground}"
36                                     Stretch="Uniform" />
37                                 <ContentPresenter
38                                     Name="contentPresenter"
39                                     HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
40                                     VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
41                                     Focusable="False"
42                                     RecognizesAccessKey="True"
43                                     SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
44                             </StackPanel>
45                         </Grid>
46                     </Border>
47                     <ControlTemplate.Triggers>
48                         <Trigger Property="coreHelper:ButtonExtensions.IconGeometry" Value="{x:Null}">
49                             <Setter TargetName="pathIcon" Property="Visibility" Value="Collapsed" />
50                         </Trigger>
51                         <Trigger Property="Content" Value="{x:Null}">
52                             <Setter TargetName="pathIcon" Property="Margin" Value="0" />
53                         </Trigger>
54                     </ControlTemplate.Triggers>
55                 </ControlTemplate>
56             </Setter.Value>
57         </Setter>
58     </Style>
59 </ResourceDictionary>

使用示例

 1     <Button
 2         Width="80"
 3         Height="80"
 4         coreHelper:ButtonExtensions.IconGeometry="{StaticResource RunningGeometry}"
 5         coreHelper:ButtonExtensions.IconHeight="40"
 6         coreHelper:ButtonExtensions.IconWidth="40"
 7         Background="#1e90ff"
 8         Content="運行"
 9         Foreground="White"
10         Style="{StaticResource ButtonVerBase}" />

RunningGeometry為

<PathGeometry x:Key="RunningGeometry">M41.355947 0h572.962133a41.355947 41.355947 0 0 1 41.355947 41.355947v100.037973H0V41.355947A41.355947 41.355947 0 0 1 41.355947 0zM0 210.356907v772.287146A41.355947 41.355947 0 0 0 41.355947 1024h941.288106A41.355947 41.355947 0 0 0 1024 982.644053V210.356907z m851.88608 295.867733L581.973333 776.137387a47.786667 47.786667 0 0 1-66.710186 0.832853 47.786667 47.786667 0 0 1-7.796054-6.294187l-115.083946-115.0976-120.54528 120.558934a47.786667 47.786667 0 0 1-67.611307 0 47.786667 47.786667 0 0 1 0-67.611307l147.12832-147.12832a48.237227 48.237227 0 0 1 13.653333-9.557333 47.786667 47.786667 0 0 1 62.887254 4.096l119.6032 119.507626 236.776106-236.817066a47.786667 47.786667 0 0 1 67.611307 0 47.786667 47.786667 0 0 1 0 67.597653z</PathGeometry>

效果

 

第二種:自定義控制項

後續更新

 

本文來自博客園,作者:阿金啊,轉載請註明原文鏈接:https://www.cnblogs.com/fengxinyuan/p/18295339


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 目錄<thread>this_thread命名空間1. get_id()2. sleep_for()3. sleep_until()4. yield()thread類構造函數:類方法1. get_id()2. join()3. detach()4. joinable()5. operator=6. ...
  • 本文主要介紹 HSQLDB 的基本使用,文中所使用到的軟體版本:Java 11.0.22、HSQLDB 2.7.2。 1、進程內模式 直接使用 JDBC 連接資料庫即可,如果資料庫不存在會自動創建。 1.1、file 資料庫 @Test public void inProcessFile() thr ...
  • 前言 Serilog是 .NET 上的一個原生結構化高性能日誌庫,這個庫能實現一些比內置庫更高度的定製。日誌持久化是其中一個非常重要的功能,生產環境通常很難掛接調試器或者某些bug的觸發條件很奇怪。為了在脫離調試環境的情況下儘可能保留更多線索來輔助解決生產問題,持久化的日誌就顯得很重要了。目前Ser ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他們有一個崩潰的dump讓我幫忙看下怎麼回事,確實有太多的人在網上找各種故障分析最後聯繫到了我,還好我一直都是免費分析,不收取任何費用,造福社區。 話不多說,既然有 dump 來了,那就上 windbg 說話吧。 二:WinDbg 分析 1. 為什麼 ...
  • 早兩天寫了一篇S3簡單上傳文件的小工具,知乎上看到了一個問題問如何實現顯示MINIO上傳進度,因此拓展一下這個小工具能夠在上傳大文件時顯示進度。 ...
  • 提高錄製視頻的質量,使腳本內容資料、筆記本電腦屏幕、手機錄製三者有機會整合在一起,主要解決的問題和特點:(1)在手機錄製視頻過程,不會因為眼睛看腳本內容導致眼神漂浮,眼睛與手機攝像頭保持對焦狀態。(2)隨意調整文字腳本區域大小、手機與電腦屏幕倚靠區域大小、引用資料區域大小。(3)載入錄製視頻文字腳本... ...
  • 前言 .NET 官方有一個用來管理國際化資源的擴展包Microsoft.Extensions.Localization,ASP.NET Core也用這個來實現國際化功能。但是這個包的翻譯數據是使用resx資源文件來管理的,這就意味著無法動態管理。雖然官方有在文檔中提供了一些第三方管理方案,但是都不太 ...
  • 在C#中,委托是一種引用類型的數據類型,允許我們封裝方法的引用。通過使用委托,我們可以將方法作為參數傳遞給其他方法,或者將多個方法組合在一起,從而實現更靈活的編程模式。委托類似於函數指針,但提供了類型安全和垃圾回收等現代語言特性。 基本概念 定義委托 定義委托需要指定它所代表的方法的原型,包括返回類 ...
一周排行
    -Advertisement-
    Play Games
  • 前言 微服務架構已經成為搭建高效、可擴展系統的關鍵技術之一,然而,現有許多微服務框架往往過於複雜,使得我們普通開發者難以快速上手並體驗到微服務帶了的便利。為瞭解決這一問題,於是作者精心打造了一款最接地氣的 .NET 微服務框架,幫助我們輕鬆構建和管理微服務應用。 本框架不僅支持 Consul 服務註 ...
  • 先看一下效果吧: 如果不會寫動畫或者懶得寫動畫,就直接交給Blend來做吧; 其實Blend操作起來很簡單,有點類似於在操作PS,我們只需要設置關鍵幀,滑鼠點來點去就可以了,Blend會自動幫我們生成我們想要的動畫效果. 第一步:要創建一個空的WPF項目 第二步:右鍵我們的項目,在最下方有一個,在B ...
  • Prism:框架介紹與安裝 什麼是Prism? Prism是一個用於在 WPF、Xamarin Form、Uno 平臺和 WinUI 中構建鬆散耦合、可維護和可測試的 XAML 應用程式框架 Github https://github.com/PrismLibrary/Prism NuGet htt ...
  • 在WPF中,屏幕上的所有內容,都是通過畫筆(Brush)畫上去的。如按鈕的背景色,邊框,文本框的前景和形狀填充。藉助畫筆,可以繪製頁面上的所有UI對象。不同畫筆具有不同類型的輸出( 如:某些畫筆使用純色繪製區域,其他畫筆使用漸變、圖案、圖像或繪圖)。 ...
  • 前言 嗨,大家好!推薦一個基於 .NET 8 的高併發微服務電商系統,涵蓋了商品、訂單、會員、服務、財務等50多種實用功能。 項目不僅使用了 .NET 8 的最新特性,還集成了AutoFac、DotLiquid、HangFire、Nlog、Jwt、LayUIAdmin、SqlSugar、MySQL、 ...
  • 本文主要介紹攝像頭(相機)如何採集數據,用於類似攝像頭本地顯示軟體,以及流媒體數據傳輸場景如傳屏、視訊會議等。 攝像頭採集有多種方案,如AForge.NET、WPFMediaKit、OpenCvSharp、EmguCv、DirectShow.NET、MediaCaptre(UWP),網上一些文章以及 ...
  • 前言 Seal-Report 是一款.NET 開源報表工具,擁有 1.4K Star。它提供了一個完整的框架,使用 C# 編寫,最新的版本採用的是 .NET 8.0 。 它能夠高效地從各種資料庫或 NoSQL 數據源生成日常報表,並支持執行複雜的報表任務。 其簡單易用的安裝過程和直觀的設計界面,我們 ...
  • 背景需求: 系統需要對接到XXX官方的API,但因此官方對接以及管理都十分嚴格。而本人部門的系統中包含諸多子系統,系統間為了穩定,程式間多數固定Token+特殊驗證進行調用,且後期還要提供給其他兄弟部門系統共同調用。 原則上:每套系統都必須單獨接入到官方,但官方的接入複雜,還要官方指定機構認證的證書 ...
  • 本文介紹下電腦設備關機的情況下如何通過網路喚醒設備,之前電源S狀態 電腦Power電源狀態- 唐宋元明清2188 - 博客園 (cnblogs.com) 有介紹過遠程喚醒設備,後面這倆天瞭解多了點所以單獨加個隨筆 設備關機的情況下,使用網路喚醒的前提條件: 1. 被喚醒設備需要支持這WakeOnL ...
  • 前言 大家好,推薦一個.NET 8.0 為核心,結合前端 Vue 框架,實現了前後端完全分離的設計理念。它不僅提供了強大的基礎功能支持,如許可權管理、代碼生成器等,還通過採用主流技術和最佳實踐,顯著降低了開發難度,加快了項目交付速度。 如果你需要一個高效的開發解決方案,本框架能幫助大家輕鬆應對挑戰,實 ...