自定义标题栏

自定义列表,可以直接修改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. Atitit 外包管理规范attilax总结

    Atitit 外包管理规范attilax总结 1. 常见的外包问题2 1.1. 使用了过时的语言与技术2 1.2. 不易扩展的架构,架构落后2 1.3. 使用了小众语言,框架类库,组件等技术,导致维护 ...

  2. Cookie和Session的总结

    1.开篇 在之前学习这一段的时候我一直有点没弄清楚,其实对Session这块的理解还可以,但是Cookie感觉始终还是欠缺点火候.之后的很长一段时间都基本上很少用Cookie了,渐渐的也淡忘了这一块的 ...

  3. ITTC数据挖掘平台介绍(七)强化的数据库, 虚拟化,脚本编辑器

    一. 前言 好久没有更新博客了,最近一直在忙着找工作,目前差不多尘埃落定.特别期待而且准备的都很少能成功,反而是没怎么在意的最终反而能拿到,真是神一样的人生. 言归正传,一直以来,数据挖掘系统的数据类 ...

  4. 检索COM类工厂中CLSID为{00024500-0000-0000-C000-000000000046}的组件时失败

    具体解决方法如下: 1:在服务器上安装office的Excel软件: 2:在"开始"->"运行"中输入dcomcnfg.exe启动"组件服务&q ...

  5. 微信小程序定时器组件(输入时间字符串即可倒计时)

    昨天写了代码,今天发现要重用,干脆就抽出来做个组件得了,顺便还改善了一下代码通用性. 昨天的代码在这里 github下载地址 用法: 引入: var timer = require('../../pl ...

  6. 在mongoose中使用$match对id失效的解决方法

    Topic.aggregate( //{$match:{_id:"5576b59e192868d01f75486c"}}, //not work //{$match:{title: ...

  7. EntityFramework 事务处理

    默认情况下,当EF调用SaveChanges()时,会把生成的所有SQL命令“包”到一个“事务(transaction)”中,只要有一个数据更新操作失败,整个事务将回滚. 在多数情况下,如果你总在数据 ...

  8. VS2015 Update2中有关cordova和xamarin安装的问题

    最近VS2015出了Update2,当然是第一时间进行了安装,中间过程曲折,反复安装卸载n次,也算是获得了一定的安装经验值.现在说一下经常出的问题. Update2里最吸引人的当然是跨平台开发的部分, ...

  9. Qt安装配置

    Qt Creator: 下载: Qt 5.5.1 for Windows 32-bit(MinGW 4.9.2, 1.0 GB):http://download.qt.io/official_rele ...

  10. C#开发微信门户及应用(1)--开始使用微信接口

    微信应用如火如荼,很多公司都希望搭上信息快车,这个是一个商机,也是一个技术的方向,因此,有空研究下.学习下微信的相关开发,也就成为日常计划的重要事情之一了.本系列文章希望从一个循序渐进的角度上,全面介 ...