WPF 自定義標題欄 自定義菜單欄

来源:http://www.cnblogs.com/kybs0/archive/2016/09/03/5811999.html
-Advertisement-
Play Games

自定義標題欄 一、設計界面樣式 <UserControl x:Class="WpfApplication6.TitleListControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="htt ...


自定義標題欄

 

一、設計界面樣式

<UserControl x:Class="WpfApplication6.TitleListControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="200" d:DesignWidth="800" Loaded="TitleListControl_OnLoaded" >
    <UserControl.Resources>
        <Style x:Key="FirstButtonStyle" TargetType="RadioButton">
            <Setter Property="Margin" Value="0.5,2"></Setter>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type RadioButton}">
                        <Grid>
                            <Border x:Name="ButtonBorder" Height="35" Width="100" Background="#FF286E9E" CornerRadius="15,0,0,15"></Border>
                            <TextBlock Text="{TemplateBinding Content}" Foreground="White" FontSize="18" VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsChecked" Value="True">
                                <Setter TargetName="ButtonBorder" Property="Background" Value="DeepSkyBlue"></Setter>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <Style TargetType="RadioButton">
                <Setter Property="Margin" Value="0.5,2"></Setter>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type RadioButton}">
                            <Grid>
                                <Border x:Name="ButtonBorder" Height="35" Width="100" Background="#FF286E9E"></Border>
                                <TextBlock Text="{TemplateBinding Content}" Foreground="White" FontSize="18" VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock>
                            </Grid>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsChecked" Value="True">
                                    <Setter TargetName="ButtonBorder" Property="Background" Value="DeepSkyBlue"></Setter>
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        <Style x:Key="LastButtonStyle" TargetType="RadioButton">
                <Setter Property="Margin" Value="0.5,2"></Setter>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type RadioButton}">
                            <Grid>
                                <Border x:Name="ButtonBorder" Height="35" Width="100" Background="#FF286E9E" CornerRadius="0,15,15,0"></Border>
                                <TextBlock Text="{TemplateBinding Content}" Foreground="White" FontSize="18" VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock>
                            </Grid>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsChecked" Value="True">
                                    <Setter TargetName="ButtonBorder" Property="Background" Value="DeepSkyBlue"></Setter>
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
    </UserControl.Resources>
    <Grid>
        <Border x:Name="ControlBorder" VerticalAlignment="Center" HorizontalAlignment="Center" CornerRadius="16,16,16,16">
            <Border.Background>
                <LinearGradientBrush StartPoint="0,1" EndPoint="1,1">
                    <GradientStop Color="White" Offset="0.2"></GradientStop>
                    <GradientStop Color="DeepSkyBlue" Offset="1"></GradientStop>
                </LinearGradientBrush>
            </Border.Background>
            <StackPanel x:Name="SpTitleList" Orientation="Horizontal" Background="Transparent" Margin="2,0">
            </StackPanel>
        </Border>
    </Grid>
</UserControl>
View Code

 二、控制項後臺代碼

public partial class TitleListControl : UserControl
    {
        public TitleListControl()
        {
            InitializeComponent();
        }
        /// <summary>
        /// get or set the items
        /// </summary>
        public List<TitleListItemModel> TitleListItems
        {
            get { return (List<TitleListItemModel>) GetValue(TitleListItemsProperty); }
            set{SetValue(TitleListItemsProperty,value);}
        }

        public static readonly DependencyProperty TitleListItemsProperty = DependencyProperty.Register("TitleListItems", typeof(List<TitleListItemModel>),
            typeof(TitleListControl),new PropertyMetadata(new List<TitleListItemModel>()));

        public UIElementCollection Items
        {
            get { return SpTitleList.Children; }
        }

        private void TitleListControl_OnLoaded(object sender, RoutedEventArgs e)
        {
            if (TitleListItems!=null)
            {
                var items = TitleListItems;
                int index = 0;
                foreach (var item in items)
                {
                    var radiaoButton=new RadioButton()
                    {
                        Content = item.Name
                    };

                    if (index == 0)
                    {
                        radiaoButton.Style = GetStyle("first");
                    }
                    else if (index == items.Count - 1)
                    {
                        radiaoButton.Style = GetStyle("last");
                    }
                    item.Index = index;
                    radiaoButton.DataContext = item;

                    radiaoButton.Checked += ToggleButton_OnChecked;

                    SpTitleList.Children.Add(radiaoButton);
                    index++;
                }
            }
        }

        private Style GetStyle(string type)
        {
            Style style = null;
            switch (type)
            {
                case "first":
                {
                    style = this.Resources["FirstButtonStyle"] as Style;
                }
                    break;
                case "last":
                {
                    style = this.Resources["LastButtonStyle"] as Style;
                }
                    break;
            }
            return style;
        }

        private void ToggleButton_OnChecked(object sender, RoutedEventArgs e)
        {
            var radioButton=sender as RadioButton;
            var dataModel=radioButton.DataContext as TitleListItemModel;
            int index = dataModel.Index;
            int count = SpTitleList.Children.Count;
            var linerBrush = new LinearGradientBrush(){StartPoint=new Point(0,1),EndPoint = new Point(1,1)};
            if (index==0)
            {
                linerBrush.GradientStops.Add(new GradientStop()
                {
                    Color = Colors.White,
                    Offset = 0.2
                });
                linerBrush.GradientStops.Add(new GradientStop()
                {
                    Color = Colors.DeepSkyBlue,
                    Offset = 1
                });
            }
            else if (index == count - 1)
            {
                linerBrush.GradientStops.Add(new GradientStop()
                {
                    Color = Colors.DeepSkyBlue,
                    Offset = 0
                });
                linerBrush.GradientStops.Add(new GradientStop()
                {
                    Color = Colors.White,
                    Offset = 0.8
                });
            }
            else
            {
                double offsetValue = Convert.ToDouble(index) / count;
                linerBrush.GradientStops.Add(new GradientStop()
                {
                    Color = Colors.DeepSkyBlue,
                    Offset = 0
                });
                linerBrush.GradientStops.Add(new GradientStop()
                {
                    Color = Colors.White,
                    Offset = offsetValue
                });
                linerBrush.GradientStops.Add(new GradientStop()
                {
                    Color = Colors.DeepSkyBlue,
                    Offset = 1
                });
            }
            ControlBorder.Background = linerBrush;
        }
    }

    public class TitleListItemModel
    {
        public int Index { get; set; }
        public string Name { get; set; }
        public string Remark { get; set; }
    }
View Code

三、引用UserControl

<Window x:Class="WpfApplication6.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:wpfApplication6="clr-namespace:WpfApplication6"
        Title="MainWindow" Height="350" Width="800" Background="LightGray">
    <Grid>
        <wpfApplication6:TitleListControl VerticalAlignment="Center" HorizontalAlignment="Center">
            <wpfApplication6:TitleListControl.TitleListItems>
                <wpfApplication6:TitleListItemModel Name="綜合" ></wpfApplication6:TitleListItemModel>
                <wpfApplication6:TitleListItemModel Name="語音體驗" ></wpfApplication6:TitleListItemModel>
                <wpfApplication6:TitleListItemModel Name="網頁瀏覽"></wpfApplication6:TitleListItemModel>
                <wpfApplication6:TitleListItemModel Name="視頻播放" ></wpfApplication6:TitleListItemModel>
                <wpfApplication6:TitleListItemModel Name="綜合覆蓋"></wpfApplication6:TitleListItemModel>
                <wpfApplication6:TitleListItemModel Name="速率性能"></wpfApplication6:TitleListItemModel>
                <wpfApplication6:TitleListItemModel Name="網路延時"></wpfApplication6:TitleListItemModel>
            </wpfApplication6:TitleListControl.TitleListItems>
        </wpfApplication6:TitleListControl>
    </Grid>
</Window>
View Code

 如需要控制項的SelectionChanged方法,在UserControl中添加個委托或者註冊一個事件即可。


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

-Advertisement-
Play Games
更多相關文章
  • 在 Linux 系統中,TCP/IP 網路是通過若幹個文本文件進行配置的,需要編輯這些文件來完成聯網工作。系統中重要的有關網路配置文件有以下幾項:/etc/sysconfig/network/etc/hosts/etc/services/etc/host.conf/etc/resolv.conf/e ...
  • 通過expr指令可以進行+、-、*、\、%等運算,但是有一點值得註意,使用乘法時,要在*前加上一個\符號。 通過test指令可以進行邏輯測試,進行測試的情況有四種: 1、整數測試 a、判斷兩個整數是否相等——test int1 -eq int2 b、判斷兩個整數是否不等——test int1 -ne ...
  • 1.下載Nginx:http://nginx.org/en/download.html 2.下載winsw配置包:http://files.cnblogs.com/files/objecttozero/nginx-service.zip 3.解壓zip文件 4.安裝 c:/nginx/nginx-s ...
  • 第一部分:安裝Mysql5.7 1.下載YUM庫 shell > wget http://dev.mysql.com/get/mysql57-community-release-el7-7.noarch.rpm 2.安裝YUM庫 shell > yum localinstall -y mysql57 ...
  • 一、準備tomcat 拷貝一份tomcat mkdir -pvri liumj cp -pvri purse-fpadmin/* liumj purse-fpadmin/*拷到liumj目錄下 二、改tomcat配置文件 vim conf/server.xml 三、解壓jar包 路徑:tomcat/ ...
  • 註:此文僅針對mac系統如果你是mac用戶,會發現桌面經常一團糟,桌面到處都是平時的截圖(mac系統的截圖是command+shift+3 和 command+shift+4 兩個快捷命令) 之前一直只知道截圖的預設保存路徑是在桌面,每次都是只能默默地定時清理...今天受不了了,想看看能否修改保存的 ...
  • 原創作品,允許轉載,轉載時請務必以超鏈接形式標明文章、作者信息和本聲明,否則將追究法律責任。 眾所周知,vim是vi的增強版本,實際體驗要比vi好用很多,由於筆者為ARM系統製作的基於busybox的文件系統中只帶了vi工具,本文主要介紹怎麼移植vim工具到基於busybox的製作的rootfs中。 ...
  • 本人系統win7專業版64位。 從5月底開始就時不時有藍屏發生,而且可以說是沒有任何徵兆就“啪”的一下藍了... 有時候是隔個四五天藍屏一次,有時候一天都能藍好幾次,實在是讓人惱火。 從第一次藍屏就開始尋找原因,直到一個月前才查到元凶,竟然就是Networx。作為一個藍屏小白,我做了挺多嘗試才最終找 ...
一周排行
    -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# ...