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)-----自定义标题栏
一.概述 每一个应用程序默认的标题栏(注意与状态栏的区别)只有一行文字(新建工程时的名字),而且颜色.大小等都是固定的,给人的感觉比较单调.但当程序需要美化的时候,那么修改标题栏是就是其中一项内容,虽 ...
随机推荐
- clearfix的最佳方案----在路上(22)
clearfix的纠结 骨灰级解决办法: .clear{clear:both;height:0;overflow:hidden;} 上诉办法是在需要清除浮动的地方加个div.clear或者br.cle ...
- SQLServer:什么是主键(PK)和外键(FK)?
一.主键与外键 1.主键是用来唯一地标识一行数据.主键列必须包含唯一的值,且不能包含空值(null). 2.主键可以建立在每张二维表中单列或者多列上. 3.一张二维表上的外键可以引用另一张二维表上对应 ...
- .NET Core 使用Dapper 操作MySQL
MySQL官方驱动:http://www.cnblogs.com/linezero/p/5806814.html .NET Core 使用Dapper 操作MySQL 数据库, .NET Core 使 ...
- Webstorm编译TypeScript
下载webstorm 下载node.js编译器npm Webstorm的安装很简单.但如果没有Java For Mac 环境打开Webstorm时会有提示,点击提示会跳转下载链接,下载安装就好. ...
- 多线程映射工具——ThreadLocal
ThreadLocal相当于一个Map<Thread, T>,各线程使用自己的线程对象Thread.currentThread()作为键存取数据,但ThreadLocal实际上是一个包装了 ...
- 用枚举enum替代int常量
枚举的好处: 1. 类型安全性 2.使用方便性 public class EnumDemo { enum Color{ RED(3),BLUE(5),BLACK(8),YELLOW(13),GREEN ...
- react入门(3)
在第一篇文章里我们介绍了jsx.组件.css写法 点击查看react入门(1) 第二篇文章里我们介绍了事件.this.props.children.props....other.map循环 点击查 ...
- Netty简介
Netty简介 Netty是由JBOSS提供的一个Java开源框架.Netty提供异步的.事件驱动的网络应用程序框架和工具,用以快速开发高性能.高可靠性的网络服务器和客户端程序.和传统BIO不同,NI ...
- 使用nvm利器,管理node版本
node.js越来越热,应用的场景也越来越多. 但也因为是开源软件,所以具备大多数开源软件都存在的“版本问题”,版本发展很快,版本前后差异性大,老系统用新版本node跑不过,全局安装的第三方组件和no ...
- VS2015 Update2中有关cordova和xamarin安装的问题
最近VS2015出了Update2,当然是第一时间进行了安装,中间过程曲折,反复安装卸载n次,也算是获得了一定的安装经验值.现在说一下经常出的问题. Update2里最吸引人的当然是跨平台开发的部分, ...