[WPF 學習] 8.極簡ComboBox的內容模板

来源:https://www.cnblogs.com/catzhou/archive/2020/03/22/12546197.html
-Advertisement-
Play Games

一、設置內容模板如下 二、前端調用(xaml) 三、代碼調用(cs) 四、效果圖如下 ...


一、設置內容模板如下

 <Style x:Key="ComboBoxToggleButton" TargetType="{x:Type ToggleButton}">
            <Setter Property="OverridesDefaultStyle" Value="true"/>
            <Setter Property="IsTabStop" Value="false"/>
            <Setter Property="Focusable" Value="false"/>
            <Setter Property="ClickMode" Value="Press"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ToggleButton}">
                        <!--綁定背景色、邊框顏色及粗細、圓角5-->
                        <Border x:Name="templateRoot" Background="{TemplateBinding Background}" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" SnapsToDevicePixels="true" CornerRadius="5">
                            <Border x:Name="splitBorder" BorderThickness="1" BorderBrush="Transparent" HorizontalAlignment="Right" Margin="0" SnapsToDevicePixels="true" Width="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}">
                                <!--綁定箭頭的顏色為邊框顏色-->
                                <Path x:Name="arrow" Data="F1 M 0,0 L 2.667,2.66665 L 5.3334,0 L 5.3334,-1.78168 L 2.6667,0.88501 L0,-1.78168 L0,0 Z" Fill="{TemplateBinding BorderBrush}" HorizontalAlignment="Center" Margin="0" VerticalAlignment="Center"/>
                            </Border>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <ControlTemplate x:Key="ComboBoxTemplate1" TargetType="{x:Type ComboBox}">
            <Grid x:Name="templateRoot" SnapsToDevicePixels="true">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition MinWidth="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}" Width="0"/>
                </Grid.ColumnDefinitions>
                <Popup x:Name="PART_Popup" AllowsTransparency="true" Grid.ColumnSpan="2" IsOpen="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" Margin="1" Placement="Bottom" PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}">
                    <theme:SystemDropShadowChrome x:Name="shadow" Color="Transparent" MinWidth="{Binding ActualWidth, ElementName=templateRoot}" MaxHeight="{TemplateBinding MaxDropDownHeight}">
                        <!--綁定下拉框的背景色、框顏色及粗細、圓角5-->
                        <Border x:Name="dropDownBorder" Background="{TemplateBinding Background}" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" CornerRadius="5">
                            <ScrollViewer x:Name="DropDownScrollViewer">
                                <Grid x:Name="grid" RenderOptions.ClearTypeHint="Enabled">
                                    <Canvas x:Name="canvas" HorizontalAlignment="Left" Height="0" VerticalAlignment="Top" Width="0">
                                        <Rectangle x:Name="opaqueRect" Fill="{Binding Background, ElementName=dropDownBorder}" Height="{Binding ActualHeight, ElementName=dropDownBorder}" Width="{Binding ActualWidth, ElementName=dropDownBorder}"/>
                                    </Canvas>
                                    <ItemsPresenter x:Name="ItemsPresenter" KeyboardNavigation.DirectionalNavigation="Contained" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                                </Grid>
                            </ScrollViewer>
                        </Border>
                    </theme:SystemDropShadowChrome>
                </Popup>
                <ToggleButton x:Name="toggleButton" Background="{TemplateBinding Background}" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" Grid.ColumnSpan="2" IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" Style="{StaticResource ComboBoxToggleButton}"/>
                <ContentPresenter x:Name="contentPresenter" ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" Content="{TemplateBinding SelectionBoxItem}" ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" ContentStringFormat="{TemplateBinding SelectionBoxItemStringFormat}"
                                  HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" IsHitTestVisible="false" Margin="{TemplateBinding Padding}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
            </Grid>

        </ControlTemplate>

二、前端調用(xaml)

        <ComboBox Template="{DynamicResource ComboBoxTemplate1}"  Background="Red" BorderBrush="Blue" BorderThickness="2"  >
            <ComboBoxItem IsSelected="True">
                <TextBlock Foreground="White">aaa</TextBlock>
            </ComboBoxItem>
            <ComboBoxItem >
                <TextBlock Foreground="Green">bbb</TextBlock>
            </ComboBoxItem>
        </ComboBox>
        

三、代碼調用(cs)

            ComboBox cb = new ComboBox() { Background=Brushes.Red,BorderBrush=Brushes.Blue,BorderThickness=new Thickness(2)};
            cb.Items.Add( new TextBox { Text = "aaa" ,Foreground=Brushes.White} );
            cb.Items.Add(new TextBox { Text = "bbb", Foreground = Brushes.Green });
            cb.SelectedIndex = 0;
            cb.Template = Resources["ComboBoxTemplate1"] as ControlTemplate;

四、效果圖如下


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

-Advertisement-
Play Games
更多相關文章
  • 一、隊列(Queue) 1.1、概念 隊列(Queue)代表了一個先進先出的對象集合。當您需要對各項進行先進先出的訪問時,則使用隊列。當您在列表中添加一項,稱為入隊,當您從列表中移除一項時,稱為出隊。 1.2、Queue類的屬性 下表列出了Queue類的一些常用的屬性: 屬性描述 Count 獲取 ...
  • 動手造輪子:給微軟的日誌框架寫一個基於委托的日誌提供者 Intro 微軟的日誌框架現在已經比較通用,有時候我們不想使用外部的日誌提供者,但又希望提供一個比較簡單的委托就可以實現日誌記錄,於是就有了後面的探索和實現。 Solution 基於委托的 實現代碼: 自定義 Logger 微軟的日誌框架中記錄 ...
  • 服務的生命周期 鏈式註入時,生存期的選擇 TryAdd與泛型註入 替換內置服務容器 ASP.NET Core提供了預設的依賴註入容器,可以在Startup.ConfigureServices方法中進行服務註入的配置。 服務的生命周期 預設的依賴註入容器提供了三種生命周期: 暫時(AddTransie ...
  • Chromium has removed support for NPAPI and consequently CEF no longer supports loading of the NPAPI Flash plugin. To support loading of the Pepper (PP... ...
  • 在上一篇文章中,我們已經搭建了整個芒果後臺管理系統整個工程架構,並集成了AutoMapper,日誌組件等,接下來我們將使用Entity Framework完善系統的持久化存儲部分。這篇EF的構造,我將以一種快速集成的方式實現,並提供超多的Linq公共方法供業務使用。 Nuget引入EF 在XiaoM ...
  • 清理/重新生成解決方案 切換Release與Debug模式 切換目標平臺後再重新生成解決方案 正常後可再改回來 ...
  • 定義插件介面類,介面中定義需要的溝通方法 在組件中某類繼承該插件介面,實現方法 //載入組件DLL Assembly ab = Assembly.LoadFrom(file); Type[] types = ab.GetTypes(); foreach (Type t in types) { //如... ...
  • Border:只能有一個子集,如要在內容周圍顯示邊框,必須放在父Border元素中。 屬性BorderBrush設置邊框顏色,BorderThickness設置邊框寬度,CornerRadius設置圓角程度(90度就是直線,0度就是圓)。 <Grid> <Grid.RowDefinitions> < ...
一周排行
    -Advertisement-
    Play Games
  • 前言 在我們開發過程中基本上不可或缺的用到一些敏感機密數據,比如SQL伺服器的連接串或者是OAuth2的Secret等,這些敏感數據在代碼中是不太安全的,我們不應該在源代碼中存儲密碼和其他的敏感數據,一種推薦的方式是通過Asp.Net Core的機密管理器。 機密管理器 在 ASP.NET Core ...
  • 新改進提供的Taurus Rpc 功能,可以簡化微服務間的調用,同時可以不用再手動輸出模塊名稱,或調用路徑,包括負載均衡,這一切,由框架實現並提供了。新的Taurus Rpc 功能,將使得服務間的調用,更加輕鬆、簡約、高效。 ...
  • 順序棧的介面程式 目錄順序棧的介面程式頭文件創建順序棧入棧出棧利用棧將10進位轉16進位數驗證 頭文件 #include <stdio.h> #include <stdbool.h> #include <stdlib.h> 創建順序棧 // 指的是順序棧中的元素的數據類型,用戶可以根據需要進行修改 ...
  • 前言 整理這個官方翻譯的系列,原因是網上大部分的 tomcat 版本比較舊,此版本為 v11 最新的版本。 開源項目 從零手寫實現 tomcat minicat 別稱【嗅虎】心有猛虎,輕嗅薔薇。 系列文章 web server apache tomcat11-01-官方文檔入門介紹 web serv ...
  • C總結與剖析:關鍵字篇 -- <<C語言深度解剖>> 目錄C總結與剖析:關鍵字篇 -- <<C語言深度解剖>>程式的本質:二進位文件變數1.變數:記憶體上的某個位置開闢的空間2.變數的初始化3.為什麼要有變數4.局部變數與全局變數5.變數的大小由類型決定6.任何一個變數,記憶體賦值都是從低地址開始往高地 ...
  • 如果讓你來做一個有狀態流式應用的故障恢復,你會如何來做呢? 單機和多機會遇到什麼不同的問題? Flink Checkpoint 是做什麼用的?原理是什麼? ...
  • C++ 多級繼承 多級繼承是一種面向對象編程(OOP)特性,允許一個類從多個基類繼承屬性和方法。它使代碼更易於組織和維護,並促進代碼重用。 多級繼承的語法 在 C++ 中,使用 : 符號來指定繼承關係。多級繼承的語法如下: class DerivedClass : public BaseClass1 ...
  • 前言 什麼是SpringCloud? Spring Cloud 是一系列框架的有序集合,它利用 Spring Boot 的開發便利性簡化了分散式系統的開發,比如服務註冊、服務發現、網關、路由、鏈路追蹤等。Spring Cloud 並不是重覆造輪子,而是將市面上開發得比較好的模塊集成進去,進行封裝,從 ...
  • class_template 類模板和函數模板的定義和使用類似,我們已經進行了介紹。有時,有兩個或多個類,其功能是相同的,僅僅是數據類型不同。類模板用於實現類所需數據的類型參數化 template<class NameType, class AgeType> class Person { publi ...
  • 目錄system v IPC簡介共用記憶體需要用到的函數介面shmget函數--獲取對象IDshmat函數--獲得映射空間shmctl函數--釋放資源共用記憶體實現思路註意 system v IPC簡介 消息隊列、共用記憶體和信號量統稱為system v IPC(進程間通信機制),V是羅馬數字5,是UNI ...