WPF圖片按鈕的實現

来源:http://www.cnblogs.com/PettyHandSome/archive/2017/10/23/7717908.html
-Advertisement-
Play Games

直接代碼 樣式代碼 調用實例 效果展示 本文原創出處:http://www.cnblogs.com/PettyHandSome/ 歡迎各位轉載,但是未經作者本人同意,轉載文章之後必須在文章頁面明顯位置給出作者和原文連接,否則保留追究法律責任的權利! ...


直接代碼

 1  public class ImageButton : System.Windows.Controls.Button
 2     {
 3 
 4         /// <summary>
 5         /// 圖片
 6         /// </summary>
 7         public static readonly DependencyProperty ImageProperty = DependencyProperty.Register("Image", typeof(ImageSource), typeof(ImageButton),
 8             new PropertyMetadata(null));
 9 
10         /// <summary>
11         /// 圖片的寬度
12         /// </summary>
13         public static readonly DependencyProperty ImageWidthProperty = DependencyProperty.Register("ImageWidth", typeof(double), typeof(ImageButton),
14             new PropertyMetadata(double.NaN));
15 
16         /// <summary>
17         /// 圖片的高度
18         /// </summary>
19         public static readonly DependencyProperty ImageHeightProperty = DependencyProperty.Register("ImageHeight", typeof(double), typeof(ImageButton),
20             new PropertyMetadata(double.NaN));
21 
22         /// <summary>
23         /// 構造函數
24         /// </summary>
25         static ImageButton()
26         {
27             DefaultStyleKeyProperty.OverrideMetadata(typeof(ImageButton), 
28                 new System.Windows.FrameworkPropertyMetadata(typeof(ImageButton)));
29         }
30 
31         /// <summary>
32         /// 設置圖片
33         /// </summary>
34         public ImageSource Image
35         {
36             get
37             {
38                 return GetValue(ImageProperty) as ImageSource;
39             }
40             set
41             {
42                 SetValue(ImageProperty, value);
43             }
44         }
45 
46         /// <summary>
47         /// 圖片寬度(屬性)
48         /// </summary>
49         public double ImageWidth
50         {
51             get
52             {
53                 return (double)GetValue(ImageWidthProperty);
54             }
55             set
56             {
57                 SetValue(ImageWidthProperty, value);
58             }
59         }
60 
61         /// <summary>
62         /// 圖片高度(屬性)
63         /// </summary>
64         public double ImageHeight
65         {
66             get
67             {
68                 return (double)GetValue(ImageHeightProperty);
69             }
70             set
71             {
72                 SetValue(ImageHeightProperty, value);
73             }
74         }
75 
76     }

樣式代碼

 1     <Style TargetType="{x:Type xi:ImageButton}">
 2         <Setter Property="Template">
 3             <Setter.Value>
 4                 <ControlTemplate TargetType="{x:Type xi:ImageButton}">
 5                     <Grid>
 6                         <Grid.RowDefinitions>
 7                             <RowDefinition Height="*"/>
 8                             <RowDefinition Height="Auto"/>
 9                         </Grid.RowDefinitions>
10                         <Border x:Name="border" Grid.RowSpan="2" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" 
11                                 SnapsToDevicePixels="true" CornerRadius="3,3,3,3"/>
12                         <Image Grid.Row="0" Source="{TemplateBinding Image}"
13                                    Width="{TemplateBinding ImageWidth}"
14                                    Height="{TemplateBinding ImageHeight}"
15                                    VerticalAlignment="{TemplateBinding VerticalAlignment}"/>
16                         <ContentPresenter Grid.Row="1" HorizontalAlignment="Center" Margin="{TemplateBinding Padding}"   
17                                     VerticalAlignment="Center" RecognizesAccessKey="True" />
18                     </Grid>
19                     <ControlTemplate.Triggers>
20                         <Trigger Property="IsPressed" Value="True">
21                             <Setter Property="Foreground" Value="#999999"/>
22                         </Trigger>
23                     </ControlTemplate.Triggers>
24 
25                 </ControlTemplate>
26             </Setter.Value>
27         </Setter>
28     </Style>

調用實例

1     <xi:ImageButton Image="../Image/設置.png" Content="新增會員" ImageHeight="52" ImageWidth="52" Width="72" Height="72" Margin="30,10,10,10"/>

效果展示

本文原創出處:http://www.cnblogs.com/PettyHandSome/

歡迎各位轉載,但是未經作者本人同意,轉載文章之後必須在文章頁面明顯位置給出作者和原文連接,否則保留追究法律責任的權利!


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

-Advertisement-
Play Games
更多相關文章
  • 最近項目中需要用到Python調用oracle實現讀寫操作,踩過很多坑,歷盡艱辛終於實現了。性能怎樣先不說,有方法後面再調優嘛。現在把代碼和註意點記錄一下。 1. 所需Python工具庫 cx_Oracle,pandas,可以使用通過控制台使用pip進行安裝(電腦中已經安裝) 2. 實現查詢操作 # ...
  • 1》Ansible 1>ansible簡介 ansible是新出現的自動化運維工具,基於Python開發,集合了眾多運維工具(puppet、cfengine、chef、func、fabric)的優點,實現了批量系統配置、批量程式部 署、批量運行命令等功能。ansible是基於模塊工作的,本身沒有批量 ...
  • 1.玩crond必須以下任務開啟 [root@wen ~]# chkconfig --list crond crond 0:off 1:off 2:on 3:on 4:on 5:on 6:off [root@wen ~]# ps -ef |grep crond|grep -v grep root 1 ...
  • 今天遇到這麼一個問題,打開VS啟動調試,始終報錯,如下圖: 我重啟VS,甚至重啟電腦都不得行,那個進程號還在變化,就在網上查找資料,各式各樣的解決方案,這裡我記錄我成功的方案。 打開項目文件地址,在解決方案根目錄下找到.vs文件: 刪除.vs 然後重啟vs即可。 ...
  • 手繪視頻最終的生成物是視頻文件,前面幾篇主要講的是手繪視頻的創作部分,今天講一下手繪視頻的導出問題。主要以 UWP 為例,另外會介紹一些 Web 端遇到的問題和解決方法。 如上所述,手繪視頻在創作後,最終會導出為視頻文件,如 MP4,WMV 等,我們目前的選擇是 MP4,整個導出大致分為幾個步驟: ...
  • Html.DropDownList傳值: 可以傳入明確的IEnumerable<SelectListItem>,也可以通過ViewBag或者ViewData隱式地傳入,前提是需要相同的名稱,比如:ViewBag.GenreId或者ViewData["GenreId"]。 示例: 視圖: 註意: 1. ...
  • C#的編譯器總是將匿名類型編譯成internal的,當在視圖中直接使用控制器傳遞的匿名對象時就會報錯錯誤代碼:控制器代碼視圖代碼執行結果:********************************************************************************* ...
  • 此示例展示了框架級依賴註入如何在 ASP.NET Core 中工作。 其簡單但功能強大,足以完成大部分的依賴註入工作。 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...