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>
二、控件后台代码
- 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; }
- }
三、引用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>
如需要控件的SelectionChanged方法,在UserControl中添加个委托或者注册一个事件即可。
WPF 自定义标题栏 自定义菜单栏的更多相关文章
- WPF中自定义标题栏时窗体最大化处理之WindowChrome
注意: 本文方法基础是WindowChrome,而WindowChrome在.NET Framework 4.5之后才集成发布的.见:WindowChrome Class 在.NET Framewor ...
- 基于electron+vue+element构建项目模板之【自定义标题栏&右键菜单项篇】
1.概述 开发平台OS:windows 开发平台IDE:vs code 本篇章将介绍自定义标题栏和右键菜单项,基于electron现有版本安全性的建议,此次的改造中主进程和渲染进程彼此语境隔离,通过预 ...
- UWP中实现自定义标题栏
UWP中实现自定义标题栏 0x00 起因 在UWP开发中,有时候我们希望实现自定义标题栏,例如在标题栏中加入搜索框.按钮之类的控件.搜了下资料居然在一个日文网站找到了一篇介绍这个主题的文章: http ...
- 【Win10开发】自定义标题栏
UWP 现在已经可以自定义标题栏了,毕竟看灰色时间长了也会厌烦,开发者们还是希望能够将自己的UI做的更加漂亮,更加与众不同.那么废话不多说,我们开始吧! 首先要了解ApplicationViewTit ...
- Android开发-取消程序标题栏或自定义标题栏
注:本文由Colin撰写,版权所有!转载请注明原文地址,谢谢合作! 在Android开发中,跟据需要我们有时候需要自定义应用程序的标题栏或者取消程序的标题栏,下面本菜鸟在此记录与分享一下自己使用的方法 ...
- setFeatureInt、android 自定义标题栏
Android 自带的toolbar 往往不能很好的的满足我们的个性化要求.因此我们经常使用自定的的标题栏.而Android系统本身也允许我们自定以标题栏. 记录一下,自定义标题栏常遇到的问题.先上效 ...
- WPF中实现自定义虚拟容器(实现VirtualizingPanel)
WPF中实现自定义虚拟容器(实现VirtualizingPanel) 在WPF应用程序开发过程中,大数据量的数据展现通常都要考虑性能问题.有下面一种常见的情况:原始数据源数据量很大,但是某一时刻数据容 ...
- WPF ScrollViewer(滚动条) 自定义样式表制作 再发一套样式 细节优化
艾尼路 出的效果图 本人嵌套 WPF ScrollViewer(滚动条) 自定义样式表制作 图文并茂 WPF ScrollViewer(滚动条) 自定义样式表制作 (改良+美化) 源代码
- Android应用开发基础篇(14)-----自定义标题栏
一.概述 每一个应用程序默认的标题栏(注意与状态栏的区别)只有一行文字(新建工程时的名字),而且颜色.大小等都是固定的,给人的感觉比较单调.但当程序需要美化的时候,那么修改标题栏是就是其中一项内容,虽 ...
随机推荐
- SQLSERVER 使用 ROLLUP 汇总数据,实现分组统计,合计,小计
表结构: CREATE TABLE [dbo].[Students]( ,) NOT NULL, ) NULL, [Sex] [int] NOT NULL, ) NULL, ) NULL, , ) N ...
- WCF学习之旅—WCF服务的WAS寄宿(十二)
上接 WCF学习之旅—WCF服务部署到IIS7.5(九) WCF学习之旅—WCF服务部署到应用程序(十) WCF学习之旅—WCF服务的Windows 服务程序寄宿(十一) 八.WAS宿主 IIS ...
- JavaScript高级程序设计--对象,数组(栈方法,队列方法,重排序方法,迭代方法)
1.使用对象字面量定义对象 var person={}; 使用这种方式创建对象时,实际上不会调用Object构造函数. 开发人员更喜欢对象字面量的语法. 2.有时候需要传递大量可选参数的情形时,一 ...
- .net erp(办公oa)开发平台架构之流程服务概要介绍
背景 搭建一个适合公司erp业务的开发平台. 架构概要图: 流程引擎开发平台: 包含流程引擎设计器,流程管理平台,流程引擎服务.目前只使用单个数据库进行管理. 流程引擎设计器 采用silve ...
- 使用UICollectionView实现首页的滚动效果
实现类似这样的效果,可以滚动大概有两种实现方案 1. 使用scrollview来实现 2. 使用UICollectionView来实现 第一种比较简单,而且相对于性能来说不太好,于是我们使用第二种方案 ...
- 时间复杂度分别为 O(n)和 O(1)的删除单链表结点的方法
有一个单链表,提供了头指针和一个结点指针,设计一个函数,在 O(1)时间内删除该结点指针指向的结点. 众所周知,链表无法随机存储,只能从头到尾去遍历整个链表,遇到目标节点之后删除之,这是最常规的思路和 ...
- 分页插件思想:pc加载更多功能和移动端下拉刷新加载数据
感觉一个人玩lol也没意思了,玩会手机,看到这个下拉刷新功能就写了这个demo! 这个demo写的比较随意,咱不能当做插件使用,基本思想是没问题的,要用就自己封装吧! 直接上代码分析下吧! 布局: & ...
- Android指纹识别深入浅出分析到实战(6.0以下系统适配方案)
指纹识别这个名词听起来并不陌生,但是实际开发过程中用得并不多.Google从Android6.0(api23)开始才提供标准指纹识别支持,并对外提供指纹识别相关的接口.本文除了能适配6.0及以上系统, ...
- 详解web容器 - Jetty与Tomcat孰强孰弱
Jetty 基本架构 Jetty目前的是一个比较被看好的 Servlet 引擎,它的架构比较简单,也是一个可扩展性和非常灵活的应用服务器.它有一个基本数据模型,这个数据模型就是 Handler(处理器 ...
- 《高性能javascript》 领悟随笔之-------DOM编程篇
<高性能javascript> 领悟随笔之-------DOM编程篇一 序:在javaSctipt中,ECMASCRIPT规定了它的语法,BOM实现了页面与浏览器的交互,而DOM则承载着整 ...