自定义标题栏

自定义列表,可以直接修改WPF中的ListBox模板,也用这样类似的效果。但是ListBox是不能设置默认选中状态的。

而我们需要一些复杂的UI效果,还是直接自定义控件来的快

GitHub下载地址:https://github.com/Kybs0/MenuListControl

一、设计界面样式

  1. <UserControl x:Class="WpfApplication6.TitleListControl"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  5. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  6. mc:Ignorable="d"
  7. d:DesignHeight="200" d:DesignWidth="800" Loaded="TitleListControl_OnLoaded" >
  8. <UserControl.Resources>
  9. <Style x:Key="FirstButtonStyle" TargetType="RadioButton">
  10. <Setter Property="Margin" Value="0.5,2"></Setter>
  11. <Setter Property="Template">
  12. <Setter.Value>
  13. <ControlTemplate TargetType="{x:Type RadioButton}">
  14. <Grid>
  15. <Border x:Name="ButtonBorder" Height="35" Width="100" Background="#FF286E9E" CornerRadius="15,0,0,15"></Border>
  16. <TextBlock Text="{TemplateBinding Content}" Foreground="White" FontSize="18" VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock>
  17. </Grid>
  18. <ControlTemplate.Triggers>
  19. <Trigger Property="IsChecked" Value="True">
  20. <Setter TargetName="ButtonBorder" Property="Background" Value="DeepSkyBlue"></Setter>
  21. </Trigger>
  22. </ControlTemplate.Triggers>
  23. </ControlTemplate>
  24. </Setter.Value>
  25. </Setter>
  26. </Style>
  27. <Style TargetType="RadioButton">
  28. <Setter Property="Margin" Value="0.5,2"></Setter>
  29. <Setter Property="Template">
  30. <Setter.Value>
  31. <ControlTemplate TargetType="{x:Type RadioButton}">
  32. <Grid>
  33. <Border x:Name="ButtonBorder" Height="35" Width="100" Background="#FF286E9E"></Border>
  34. <TextBlock Text="{TemplateBinding Content}" Foreground="White" FontSize="18" VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock>
  35. </Grid>
  36. <ControlTemplate.Triggers>
  37. <Trigger Property="IsChecked" Value="True">
  38. <Setter TargetName="ButtonBorder" Property="Background" Value="DeepSkyBlue"></Setter>
  39. </Trigger>
  40. </ControlTemplate.Triggers>
  41. </ControlTemplate>
  42. </Setter.Value>
  43. </Setter>
  44. </Style>
  45. <Style x:Key="LastButtonStyle" TargetType="RadioButton">
  46. <Setter Property="Margin" Value="0.5,2"></Setter>
  47. <Setter Property="Template">
  48. <Setter.Value>
  49. <ControlTemplate TargetType="{x:Type RadioButton}">
  50. <Grid>
  51. <Border x:Name="ButtonBorder" Height="35" Width="100" Background="#FF286E9E" CornerRadius="0,15,15,0"></Border>
  52. <TextBlock Text="{TemplateBinding Content}" Foreground="White" FontSize="18" VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock>
  53. </Grid>
  54. <ControlTemplate.Triggers>
  55. <Trigger Property="IsChecked" Value="True">
  56. <Setter TargetName="ButtonBorder" Property="Background" Value="DeepSkyBlue"></Setter>
  57. </Trigger>
  58. </ControlTemplate.Triggers>
  59. </ControlTemplate>
  60. </Setter.Value>
  61. </Setter>
  62. </Style>
  63. </UserControl.Resources>
  64. <Grid>
  65. <Border x:Name="ControlBorder" VerticalAlignment="Center" HorizontalAlignment="Center" CornerRadius="16,16,16,16">
  66. <Border.Background>
  67. <LinearGradientBrush StartPoint="0,1" EndPoint="1,1">
  68. <GradientStop Color="White" Offset="0.2"></GradientStop>
  69. <GradientStop Color="DeepSkyBlue" Offset="1"></GradientStop>
  70. </LinearGradientBrush>
  71. </Border.Background>
  72. <StackPanel x:Name="SpTitleList" Orientation="Horizontal" Background="Transparent" Margin="2,0">
  73. </StackPanel>
  74. </Border>
  75. </Grid>
  76. </UserControl>

二、控件后台代码

  1. public partial class TitleListControl : UserControl
  2. {
  3. public TitleListControl()
  4. {
  5. InitializeComponent();
  6. }
  7. /// <summary>
  8. /// get or set the items
  9. /// </summary>
  10. public List<TitleListItemModel> TitleListItems
  11. {
  12. get { return (List<TitleListItemModel>) GetValue(TitleListItemsProperty); }
  13. set{SetValue(TitleListItemsProperty,value);}
  14. }
  15.  
  16. public static readonly DependencyProperty TitleListItemsProperty = DependencyProperty.Register("TitleListItems", typeof(List<TitleListItemModel>),
  17. typeof(TitleListControl),new PropertyMetadata(new List<TitleListItemModel>()));
  18.  
  19. public UIElementCollection Items
  20. {
  21. get { return SpTitleList.Children; }
  22. }
  23.  
  24. private void TitleListControl_OnLoaded(object sender, RoutedEventArgs e)
  25. {
  26. if (TitleListItems!=null)
  27. {
  28. var items = TitleListItems;
  29. int index = 0;
  30. foreach (var item in items)
  31. {
  32. var radiaoButton=new RadioButton()
  33. {
  34. Content = item.Name
  35. };
  36.  
  37. if (index == 0)
  38. {
  39. radiaoButton.Style = GetStyle("first");
  40. }
  41. else if (index == items.Count - 1)
  42. {
  43. radiaoButton.Style = GetStyle("last");
  44. }
  45. item.Index = index;
  46. radiaoButton.DataContext = item;
  47.  
  48. radiaoButton.Checked += ToggleButton_OnChecked;
  49.  
  50. SpTitleList.Children.Add(radiaoButton);
  51. index++;
  52. }
  53. }
  54. }
  55.  
  56. private Style GetStyle(string type)
  57. {
  58. Style style = null;
  59. switch (type)
  60. {
  61. case "first":
  62. {
  63. style = this.Resources["FirstButtonStyle"] as Style;
  64. }
  65. break;
  66. case "last":
  67. {
  68. style = this.Resources["LastButtonStyle"] as Style;
  69. }
  70. break;
  71. }
  72. return style;
  73. }
  74.  
  75. private void ToggleButton_OnChecked(object sender, RoutedEventArgs e)
  76. {
  77. var radioButton=sender as RadioButton;
  78. var dataModel=radioButton.DataContext as TitleListItemModel;
  79. int index = dataModel.Index;
  80. int count = SpTitleList.Children.Count;
  81. var linerBrush = new LinearGradientBrush(){StartPoint=new Point(0,1),EndPoint = new Point(1,1)};
  82. if (index==0)
  83. {
  84. linerBrush.GradientStops.Add(new GradientStop()
  85. {
  86. Color = Colors.White,
  87. Offset = 0.2
  88. });
  89. linerBrush.GradientStops.Add(new GradientStop()
  90. {
  91. Color = Colors.DeepSkyBlue,
  92. Offset = 1
  93. });
  94. }
  95. else if (index == count - 1)
  96. {
  97. linerBrush.GradientStops.Add(new GradientStop()
  98. {
  99. Color = Colors.DeepSkyBlue,
  100. Offset = 0
  101. });
  102. linerBrush.GradientStops.Add(new GradientStop()
  103. {
  104. Color = Colors.White,
  105. Offset = 0.8
  106. });
  107. }
  108. else
  109. {
  110. double offsetValue = Convert.ToDouble(index) / count;
  111. linerBrush.GradientStops.Add(new GradientStop()
  112. {
  113. Color = Colors.DeepSkyBlue,
  114. Offset = 0
  115. });
  116. linerBrush.GradientStops.Add(new GradientStop()
  117. {
  118. Color = Colors.White,
  119. Offset = offsetValue
  120. });
  121. linerBrush.GradientStops.Add(new GradientStop()
  122. {
  123. Color = Colors.DeepSkyBlue,
  124. Offset = 1
  125. });
  126. }
  127. ControlBorder.Background = linerBrush;
  128. }
  129. }
  130.  
  131. public class TitleListItemModel
  132. {
  133. public int Index { get; set; }
  134. public string Name { get; set; }
  135. public string Remark { get; set; }
  136. }

三、引用UserControl

  1. <Window x:Class="WpfApplication6.MainWindow"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:wpfApplication6="clr-namespace:WpfApplication6"
  5. Title="MainWindow" Height="350" Width="800" Background="LightGray">
  6. <Grid>
  7. <wpfApplication6:TitleListControl VerticalAlignment="Center" HorizontalAlignment="Center">
  8. <wpfApplication6:TitleListControl.TitleListItems>
  9. <wpfApplication6:TitleListItemModel Name="综合" ></wpfApplication6:TitleListItemModel>
  10. <wpfApplication6:TitleListItemModel Name="语音体验" ></wpfApplication6:TitleListItemModel>
  11. <wpfApplication6:TitleListItemModel Name="网页浏览"></wpfApplication6:TitleListItemModel>
  12. <wpfApplication6:TitleListItemModel Name="视频播放" ></wpfApplication6:TitleListItemModel>
  13. <wpfApplication6:TitleListItemModel Name="综合覆盖"></wpfApplication6:TitleListItemModel>
  14. <wpfApplication6:TitleListItemModel Name="速率性能"></wpfApplication6:TitleListItemModel>
  15. <wpfApplication6:TitleListItemModel Name="网络延时"></wpfApplication6:TitleListItemModel>
  16. </wpfApplication6:TitleListControl.TitleListItems>
  17. </wpfApplication6:TitleListControl>
  18. </Grid>
  19. </Window>

如需要控件的SelectionChanged方法,在UserControl中添加个委托或者注册一个事件即可。

WPF 自定义标题栏 自定义菜单栏的更多相关文章

  1. WPF中自定义标题栏时窗体最大化处理之WindowChrome

    注意: 本文方法基础是WindowChrome,而WindowChrome在.NET Framework 4.5之后才集成发布的.见:WindowChrome Class 在.NET Framewor ...

  2. 基于electron+vue+element构建项目模板之【自定义标题栏&右键菜单项篇】

    1.概述 开发平台OS:windows 开发平台IDE:vs code 本篇章将介绍自定义标题栏和右键菜单项,基于electron现有版本安全性的建议,此次的改造中主进程和渲染进程彼此语境隔离,通过预 ...

  3. UWP中实现自定义标题栏

    UWP中实现自定义标题栏 0x00 起因 在UWP开发中,有时候我们希望实现自定义标题栏,例如在标题栏中加入搜索框.按钮之类的控件.搜了下资料居然在一个日文网站找到了一篇介绍这个主题的文章: http ...

  4. 【Win10开发】自定义标题栏

    UWP 现在已经可以自定义标题栏了,毕竟看灰色时间长了也会厌烦,开发者们还是希望能够将自己的UI做的更加漂亮,更加与众不同.那么废话不多说,我们开始吧! 首先要了解ApplicationViewTit ...

  5. Android开发-取消程序标题栏或自定义标题栏

    注:本文由Colin撰写,版权所有!转载请注明原文地址,谢谢合作! 在Android开发中,跟据需要我们有时候需要自定义应用程序的标题栏或者取消程序的标题栏,下面本菜鸟在此记录与分享一下自己使用的方法 ...

  6. setFeatureInt、android 自定义标题栏

    Android 自带的toolbar 往往不能很好的的满足我们的个性化要求.因此我们经常使用自定的的标题栏.而Android系统本身也允许我们自定以标题栏. 记录一下,自定义标题栏常遇到的问题.先上效 ...

  7. WPF中实现自定义虚拟容器(实现VirtualizingPanel)

    WPF中实现自定义虚拟容器(实现VirtualizingPanel) 在WPF应用程序开发过程中,大数据量的数据展现通常都要考虑性能问题.有下面一种常见的情况:原始数据源数据量很大,但是某一时刻数据容 ...

  8. WPF ScrollViewer(滚动条) 自定义样式表制作 再发一套样式 细节优化

    艾尼路 出的效果图 本人嵌套 WPF ScrollViewer(滚动条) 自定义样式表制作 图文并茂 WPF ScrollViewer(滚动条) 自定义样式表制作 (改良+美化) 源代码

  9. Android应用开发基础篇(14)-----自定义标题栏

    一.概述 每一个应用程序默认的标题栏(注意与状态栏的区别)只有一行文字(新建工程时的名字),而且颜色.大小等都是固定的,给人的感觉比较单调.但当程序需要美化的时候,那么修改标题栏是就是其中一项内容,虽 ...

随机推荐

  1. SQLSERVER 使用 ROLLUP 汇总数据,实现分组统计,合计,小计

    表结构: CREATE TABLE [dbo].[Students]( ,) NOT NULL, ) NULL, [Sex] [int] NOT NULL, ) NULL, ) NULL, , ) N ...

  2. WCF学习之旅—WCF服务的WAS寄宿(十二)

    上接    WCF学习之旅—WCF服务部署到IIS7.5(九) WCF学习之旅—WCF服务部署到应用程序(十) WCF学习之旅—WCF服务的Windows 服务程序寄宿(十一) 八.WAS宿主 IIS ...

  3. JavaScript高级程序设计--对象,数组(栈方法,队列方法,重排序方法,迭代方法)

    1.使用对象字面量定义对象 var person={}; 使用这种方式创建对象时,实际上不会调用Object构造函数. 开发人员更喜欢对象字面量的语法.   2.有时候需要传递大量可选参数的情形时,一 ...

  4. .net erp(办公oa)开发平台架构之流程服务概要介绍

    背景 搭建一个适合公司erp业务的开发平台. 架构概要图:    流程引擎开发平台:  包含流程引擎设计器,流程管理平台,流程引擎服务.目前只使用单个数据库进行管理.  流程引擎设计器 采用silve ...

  5. 使用UICollectionView实现首页的滚动效果

    实现类似这样的效果,可以滚动大概有两种实现方案 1. 使用scrollview来实现 2. 使用UICollectionView来实现 第一种比较简单,而且相对于性能来说不太好,于是我们使用第二种方案 ...

  6. 时间复杂度分别为 O(n)和 O(1)的删除单链表结点的方法

    有一个单链表,提供了头指针和一个结点指针,设计一个函数,在 O(1)时间内删除该结点指针指向的结点. 众所周知,链表无法随机存储,只能从头到尾去遍历整个链表,遇到目标节点之后删除之,这是最常规的思路和 ...

  7. 分页插件思想:pc加载更多功能和移动端下拉刷新加载数据

    感觉一个人玩lol也没意思了,玩会手机,看到这个下拉刷新功能就写了这个demo! 这个demo写的比较随意,咱不能当做插件使用,基本思想是没问题的,要用就自己封装吧! 直接上代码分析下吧! 布局: & ...

  8. Android指纹识别深入浅出分析到实战(6.0以下系统适配方案)

    指纹识别这个名词听起来并不陌生,但是实际开发过程中用得并不多.Google从Android6.0(api23)开始才提供标准指纹识别支持,并对外提供指纹识别相关的接口.本文除了能适配6.0及以上系统, ...

  9. 详解web容器 - Jetty与Tomcat孰强孰弱

    Jetty 基本架构 Jetty目前的是一个比较被看好的 Servlet 引擎,它的架构比较简单,也是一个可扩展性和非常灵活的应用服务器.它有一个基本数据模型,这个数据模型就是 Handler(处理器 ...

  10. 《高性能javascript》 领悟随笔之-------DOM编程篇

    <高性能javascript> 领悟随笔之-------DOM编程篇一 序:在javaSctipt中,ECMASCRIPT规定了它的语法,BOM实现了页面与浏览器的交互,而DOM则承载着整 ...