WPF 自定义标题栏 自定义菜单栏
自定义标题栏
自定义列表,可以直接修改WPF中的ListBox模板,也用这样类似的效果。但是ListBox是不能设置默认选中状态的。
而我们需要一些复杂的UI效果,还是直接自定义控件来的快
GitHub下载地址:https://github.com/Kybs0/MenuListControl
一、设计界面样式
<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中添加个委托或者注册一个事件即可。
WPF 自定义标题栏 自定义菜单栏相关推荐
- C# wpf style中实现可定制的自定义标题栏
wpf自定义标题栏系列 第一章 自定义标题栏 第二章 添加窗口阴影 第三章 style中定义标题栏 第四章 style使用参数及模板定义标题栏(本章) 文章目录 wpf自定义标题栏系列 前言 一.如何 ...
- 【最终版】PyQt5 自定义标题栏,实现无边框,最小化最大化关闭事件,窗口拖动移动,窗口改变大小,仿百度网盘色调美化,添加内容窗口
[最终版]PyQt5 自定义标题栏,实现无边框,最小化最大化关闭事件,窗口拖动移动,窗口改变大小,仿百度网盘色调美化,添加内容窗口 文章目录 [最终版]PyQt5 自定义标题栏,实现无边框,最小化最大 ...
- AutoCAD Electrical(ACE)的基本操作——自定义标题栏、解决项目描述乱码问题
使用版本为:AutoCAD Electrical 2020 - 简体中文 (Simplified Chinese) 目录 自定义标题栏 第一步:生成标题栏 ①绘制标题栏 ->如何设置选择框为方框 ...
- 【Win10开发】自定义标题栏
UWP 现在已经可以自定义标题栏了,毕竟看灰色时间长了也会厌烦,开发者们还是希望能够将自己的UI做的更加漂亮,更加与众不同.那么废话不多说,我们开始吧! 首先要了解ApplicationViewTit ...
- android分享的主标题,Android 自定义标题栏(title栏)
近日 需要在android的标题栏上添加按钮,所以对android的标题栏进行了一下简单的研究- 第一步,向实现自定义标题栏,需要在onCreate方法里这样写 requestWindowFeatur ...
- [WPF疑难] 继承自定义窗口
[WPF疑难] 继承自定义窗口 原文 [WPF疑难] 继承自定义窗口 [WPF疑难] 继承自定义窗口 周银辉 项目中有不少的弹出窗口,按照美工的设计其外边框(包括最大化,最小化,关闭等按钮)自然不同于 ...
- WPF DataGrid 通过自定义表头模拟首行固定
WPF DataGrid 通过自定义表头模拟首行固定 独立观察员 2021 年 9 月 25 日 最近工作中要在 WPF 中做个表格,自然首选就是 DataGrid 控件了.问题是,UI 设计的表格是 ...
- pccad自定义图框_(PCCAD自定义标题栏详细方法.doc
PCCAD2011自定义标题栏详细方法 下面以图3-1为例说明标题栏的自定义过程. 图3-1 1.新建文件(用New 命令). 2.用绘图和文字中的相关命令设计出图3-1所示的图形.其中在使用中不变的 ...
- android 标题栏进度圈使用方法,Android 自定义标题栏 显示网页加载进度的方法实例...
这阵子在做Lephone的适配,测试组提交一个bug:标题栏的文字较长时没有显示完全,其实这并不能算个bug,并且这个问题在以前其他机器也没有出现,只是说在Lephone的这个平台上显示得不怎么美观, ...
最新文章
- 浅谈话题模型:LSA、PLSA、LDA
- 链路追踪之zipkin
- C语言 | C语言中的输出函数:printf()
- taro 小程序转h5之后报错_记录微信小程序转Taro中遇到的问题
- CentOS 系统盘迁移
- 青春日志html,关于青春日记模板锦集四篇
- 关于AD导Gerber文件的理解和总结
- 江苏省计算机二级c语言备考,江苏省计算机二级C语言考试备考指南
- 思维模型 吸引力法则/定律
- 【SDOI2013】项链 题解
- 拉卡拉考拉超收,关于它的全部信息!
- SaaS之光照亮经济型酒店信息化道路
- java xmladapte_三步解决JAXB生成XML包含CDATA问题—JAVA编程
- Docker服务条款:禁止名单内的个人团体使用,自2020年8月13日起生效
- linux网络之怪现象一--接网线启动网络不通,不接网线启动再插线网络通
- 怎么在Mysql中添加列_mysql如何给表中添加列(字段)?
- 【游戏开发】unity教程4 打飞碟小游戏
- Vulkan spec 中文版 翻译基础版本切换
- 【JavaWeb04】
- 前端实现炫酷动效_Lottie-前端实现AE动效