自定义标题栏

自定义列表,可以直接修改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 自定义标题栏 自定义菜单栏的更多相关文章

  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. 微信JSAPI支付

    最近在微信H5页面内集成微信JSAPI支付,遇到不少问题,现将集成步骤及遇到的问题记录如下: 1.官方下载SDK,下载地址:https://pay.weixin.qq.com/wiki/doc/api ...

  2. 【Win 10 应用开发】文件读写的三种方案

    本文老周就跟伙伴们探讨一下关于文件读写的方法.总得来说嘛,有三种方案可以用,而且每种方案都各有特色,也说不上哪种较好.反正你得记住老祖宗留给我们的大智慧——事无定法,灵活运用者为上. OK,咱们开始吧 ...

  3. 【JAVA框架】Hibernate 与Mybatis 区别

    Hibernate Mybatis 简介 区别 与联系 欢迎提出见解及转载. 1 简单简介     1.1    Hibernate 框架          Hibernate是一个开放源代码的对象关 ...

  4. 从零开始编写自己的C#框架(20)——框架异常处理及日志记录

    最近很忙,杂事也多,所以开发本框架也是断断续续的,终于在前两天将前面设定的功能都基本完成了,剩下一些小功能遗漏的以后发现再补上.接下来的章节主要都是讲解在本框架的基础上进行开发的小巧. 本框架主要有四 ...

  5. TCP三次握手的正确使用姿势

    背景 和女朋友异地恋一年多,为了保持感情我提议每天晚上视频聊天一次. 从好上开始,到现在,一年多也算坚持下来了. 问题 有时候聊天的过程中,我的网络或者她的网络可能会不好,视频就会卡住,听不到对方的声 ...

  6. JavaScript学习总结(一)——延迟对象、跨域、模板引擎、弹出层、AJAX示例

    一.AJAX示例 AJAX全称为“Asynchronous JavaScript And XML”(异步JavaScript和XML) 是指一种创建交互式网页应用的开发技术.改善用户体验,实现无刷新效 ...

  7. 【中文分词】二阶隐马尔可夫模型2-HMM

    在前一篇中介绍了用HMM做中文分词,对于未登录词(out-of-vocabulary, OOV)有良好的识别效果,但是缺点也十分明显--对于词典中的(in-vocabulary, IV)词却未能很好地 ...

  8. iOS 调试工具

    仪表  xcode5 引入了调试仪表,通过仪表可以直观的看出应用的CPU和内存占用量.运行一个程序,点击仪表栏.可以发现当程序处于运行状态时,调试导航面板会以柱状图显示CPU和内存占用量,并随着应用实 ...

  9. Ionic 简单操作

    在使用 Ionic 之前要安装 Nodejs,Cordova . Java 下载Java 网站.Java 默认安装在 C:\Program Files\Java 文件目录. Android 下载And ...

  10. jquery动态生成的元素添加事件的方法

    动态生成的元素如果要添加事件,要写成 $(document).on("click", "#txtName", function() { alert(this.v ...