WPF預設控制項模板的獲取和資源詞典的使用

来源:https://www.cnblogs.com/xiaomengshan/archive/2019/09/02/11446436.html
-Advertisement-
Play Games

一、獲取預設的控制項模板 WPF修改控制項模板是修改外觀最方便的方式,但是會出現不知道原來的控制項的模板長什麼樣,或者想用來參考的,下麵分享一下獲取某控制項預設控制項模板的方式(已Button為例): 1、創建一個Button 2、在界面上選擇Button,右鍵->編輯模板->編輯副本 ,即可看到XAML中自 ...


一、獲取預設的控制項模板

WPF修改控制項模板是修改外觀最方便的方式,但是會出現不知道原來的控制項的模板長什麼樣,或者想用來參考的,下麵分享一下獲取某控制項預設控制項模板的方式(已Button為例):

1、創建一個Button

2、在界面上選擇Button,右鍵->編輯模板->編輯副本 ,即可看到XAML中自動生成了原始的控制項模板

3、可以在預設模板上修改其中的一些屬性運行測試是否生效

這樣在預設的控制項模板上編輯,只修改需要修改的部分即可,可以大大減少工作量,也添加了容錯率。但是會發現所有的模板和樣式都放在主界面的XAML代碼量會很多、很亂,所以可以採用單獨的資源詞典來存放這些模板和樣式,主界面只要根據Key調用即可。

 

二、資源字典的使用

1、選中項目右鍵->添加->新建項->資源詞典(WPF)

生成的初始資源詞典如下:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:TemplateDemo">
</ResourceDictionary>

現在可以在內容將模板和樣式作為資源分流到各個資源詞典,現在將Button的預設模板轉移至該控制項模板

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:TemplateDemo">
    
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary>
            <Style x:Key="FocusVisual">
                <Setter Property="Control.Template">
                    <Setter.Value>
                        <ControlTemplate>
                            <Rectangle Margin="2" SnapsToDevicePixels="true" Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" StrokeThickness="1" StrokeDashArray="1 2"/>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
            <SolidColorBrush x:Key="Button.Static.Background" Color="#FFDDDDDD"/>
            <SolidColorBrush x:Key="Button.Static.Border" Color="#FF707070"/>
            <SolidColorBrush x:Key="Button.MouseOver.Background" Color="#FFBEE6FD"/>
            <SolidColorBrush x:Key="Button.MouseOver.Border" Color="#FF3C7FB1"/>
            <SolidColorBrush x:Key="Button.Pressed.Background" Color="#FFC4E5F6"/>
            <SolidColorBrush x:Key="Button.Pressed.Border" Color="#FF2C628B"/>
            <SolidColorBrush x:Key="Button.Disabled.Background" Color="#FFF4F4F4"/>
            <SolidColorBrush x:Key="Button.Disabled.Border" Color="#FFADB2B5"/>
            <SolidColorBrush x:Key="Button.Disabled.Foreground" Color="#FF838383"/>
            <Style x:Key="ButtonStyle1" TargetType="{x:Type Button}">
                <Setter Property="FocusVisualStyle" Value="{StaticResource FocusVisual}"/>
                <Setter Property="Background" Value="{StaticResource Button.Static.Background}"/>
                <Setter Property="BorderBrush" Value="{StaticResource Button.Static.Border}"/>
                <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
                <Setter Property="BorderThickness" Value="1"/>
                <Setter Property="HorizontalContentAlignment" Value="Center"/>
                <Setter Property="VerticalContentAlignment" Value="Center"/>
                <Setter Property="Padding" Value="1"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type Button}">
                            <StackPanel>
                                <Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="true">
                                    <ContentPresenter x:Name="contentPresenter" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                                </Border>
                            </StackPanel>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsDefaulted" Value="true">
                                    <Setter Property="BorderBrush" TargetName="border" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
                                </Trigger>
                                <Trigger Property="IsMouseOver" Value="true">
                                    <Setter Property="Background" TargetName="border" Value="{StaticResource Button.MouseOver.Background}"/>
                                    <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource Button.MouseOver.Border}"/>
                                </Trigger>
                                <Trigger Property="IsPressed" Value="true">
                                    <Setter Property="Background" TargetName="border" Value="{StaticResource Button.Pressed.Background}"/>
                                    <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource Button.Pressed.Border}"/>
                                </Trigger>
                                <Trigger Property="IsEnabled" Value="false">
                                    <Setter Property="Background" TargetName="border" Value="{StaticResource Button.Disabled.Background}"/>
                                    <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource Button.Disabled.Border}"/>
                                    <Setter Property="TextElement.Foreground" TargetName="contentPresenter" Value="{StaticResource Button.Disabled.Foreground}"/>
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </ResourceDictionary>
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

 

2、要引用該資源字典還需要在App.Xaml中進行聲明,我的名稱叫TemplateDictionary.xaml,需要保證其命名空間一致

 <Application.Resources>
        <ResourceDictionary Source="TemplateDictionary.xaml"></ResourceDictionary>
    </Application.Resources>

 

3、在主XAML中使用StaticResource或DynamicResource進行靜態或動態引用即可

<Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="309,286,0,0" VerticalAlignment="Top" Width="75" Style="{StaticResource ButtonStyle1}"/>

 

以上就是關於獲取預設空間模板和使用資源詞典的一些簡單的介紹,結合起來使用可以搭建簡潔方便的代碼佈局

 


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

-Advertisement-
Play Games
更多相關文章
  • 在前面的文章(abp(net core)+easyui+efcore實現倉儲管理系統——展現層實現增刪改查之菜單與測試(九) )中我們學會瞭如何添加靜態菜單,但是做為一個信息管理系統,總不能每次有新功能新菜單,都靜態添加菜單,編譯,再上線。我們希望的是有一個菜單管理界面,在此頁面中輸入相應的菜單,只... ...
  • 今天裝好了,net core sdk 3.0之後,打開Visual Studio2019後,新建項目時發現盡然沒有.net core3.0的模板。 搜了下其他博主的文章,按照文章里做瞭如下設置: 然後發現,這些設置沒有用!!!依然顯示不出3.0的模板。 經過一下午的折騰,重裝了core sdk,重裝 ...
  • 上一編講了cap2.6的快速入門,這次我們來講講在控制臺中如何使用cap2.6。因為cap2.6的記憶體模式目前已經可以使用了,相關組件已經更新,所以這次我們以簡單的記憶體模式為例。 1:創建項目 創建一個名叫CAPConsoleDemo的 “控制台應用(.NET Core)” 程式,.netcore版 ...
  • @[toc] 前言 這幾天忙活著別的東西,耽誤了很長時間,從文件操作完了之後就在考慮著下一步鼓搗點兒啥,因為最開始的業務開發就是企業微信相關的,這剛好來做個內部應用的小例子玩玩。 企業微信 前身是企業號,當時微信主推的還是公眾號與服務號,後續戰略考慮到企業的OA了(當然還是跟某個搶市場),企業號應該 ...
  • 程式出現 System.AccessViolationException異常會終止進程,try catch是無法捕捉的。 有個處理方法在引發異常的發放上面加上 [System.Runtime.ExceptionServices.HandleProcessCorruptedStateException ...
  • 前提 入行已經7,8年了,一直想做一套漂亮點的自定義控制項,於是就有了本系列文章。 GitHub:https://github.com/kwwwvagaa/NetWinformControl 碼雲:https://gitee.com/kwwwvagaa/net_winform_custom_contr ...
  • ASP.NET Core 進程外(out-of-process)托管 在本節中,我們將討論 ASP.NET Core 中的Out Of Process Hosting。 ASP.NET Core 進程內(InProcess)托管 我們先簡單回顧下 ASP.NET Core 中,要配置 InProce ...
  • Core3的 SDK下載地址是:https://dotnet.microsoft.com/download/dotnet-core/3.0 ! 不要下載preview8!!!,請先下載 preview7,preview7,preview7主要的說3遍,少走彎路!!!64/32位的操作系統自己確認好! ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...