对WPF中Expander控件美化

示例图:

Expander控件功能很常见, 一般用于系统左侧的菜单收缩面板。

主要的组成

一个头部(header) 和 一个 内容(content) 组成。

 <Expander ExpandDirection="Down"  SnapsToDevicePixels="True" VerticalAlignment="Top" Style="{DynamicResource ExpanderStyle1}" >
                <Expander.Header>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock  FontFamily="/WpfApplication1;component/Resources/#iconfont" Text=""
                                    FontSize="22" Foreground="#918C8C" VerticalAlignment="Center"></TextBlock>
                        <TextBlock FontSize="25" Text="首页" Margin="8,0,-51,0"  Foreground="#918C8C" ></TextBlock>
                    </StackPanel>
                </Expander.Header>
                <Expander.Content>
                    <StackPanel Background="#F6F8F8">
                        <RadioButton Style="{DynamicResource RadioButtonStyle}">控制中心</RadioButton>
                        <RadioButton Style="{DynamicResource RadioButtonStyle}">资源管理</RadioButton>
                        <RadioButton Style="{DynamicResource RadioButtonStyle}">仪表菜单</RadioButton>
                        <RadioButton Style="{DynamicResource RadioButtonStyle}">顶部导航</RadioButton>
                        <RadioButton Style="{DynamicResource RadioButtonStyle}">通知中心</RadioButton>
                        <RadioButton Style="{DynamicResource RadioButtonStyle}">蓝牙设置</RadioButton>
                    </StackPanel>
                </Expander.Content>
            </Expander>

为了修改掉原生的样式, 重新定义了一个Style /ExpanderStyle1

1.将原有的左侧圆形删除

2.把左侧的箭头移动至右侧 【主要修改红色加粗部分调整】

<Style x:Key="ExpanderStyle1" TargetType="{x:Type Expander}">
           <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
           <Setter Property="Background" Value="Transparent"/>
           <Setter Property="BorderBrush" Value="#918C8C"/>
           <Setter Property="BorderThickness" Value="0 0 0 0"/>
           <Setter Property="Template">
               <Setter.Value>
                   <ControlTemplate TargetType="{x:Type Expander}">
                       <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" CornerRadius="0" SnapsToDevicePixels="True">
                           <DockPanel>
                               <ToggleButton x:Name="HeaderSite" ContentTemplate="{TemplateBinding HeaderTemplate}"
                                             Content="{TemplateBinding Header}" DockPanel.Dock="Top" Foreground="{TemplateBinding Foreground}"
                                             FontWeight="{TemplateBinding FontWeight}" FontStyle="{TemplateBinding FontStyle}"
                                             FontStretch="{TemplateBinding FontStretch}" FontSize="{TemplateBinding FontSize}"
                                             FontFamily="{TemplateBinding FontFamily}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
                                             IsChecked="{Binding IsExpanded, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
                                             Margin="1" MinWidth="0" MinHeight="0" Padding="{TemplateBinding Padding}" VerticalContentAlignment="{TemplateBinding
                                   VerticalContentAlignment}">
                                   <ToggleButton.FocusVisualStyle>
                                       <Style>
                                           <Setter Property="Control.Template">
                                               <Setter.Value>
                                                   <ControlTemplate>
                                                       <Border>
                                                           <Rectangle Margin="0" SnapsToDevicePixels="True" Stroke="Black" StrokeThickness="1" StrokeDashArray="1 2"/>
                                                       </Border>
                                                   </ControlTemplate>
                                               </Setter.Value>
                                           </Setter>
                                       </Style>
                                   </ToggleButton.FocusVisualStyle>
                                   <ToggleButton.Style>
                                       <Style TargetType="{x:Type ToggleButton}">
                                           <Setter Property="Template">
                                               <Setter.Value>
                                                   <ControlTemplate TargetType="{x:Type ToggleButton}">
                                                       <Border Padding="{TemplateBinding Padding}">
                                                           <Grid>
                                                               <Grid.ColumnDefinitions>
                                                                   <ColumnDefinition Width="19*"/>
                                                               </Grid.ColumnDefinitions>
                                                               <Path Grid.Column="0" x:Name="arrow" Data="M1,1.5L4.5,5 8,1.5" HorizontalAlignment="Right"  SnapsToDevicePixels="False" Stroke="#918C8C" StrokeThickness="2" VerticalAlignment="Center" Height="10" Margin="0,10" />
                                                               <ContentPresenter ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" ContentStringFormat="{TemplateBinding ContentStringFormat}" HorizontalAlignment="Left" Margin="4,0,0,0" RecognizesAccessKey="True" SnapsToDevicePixels="True" VerticalAlignment="Center"  />
                                                           </Grid>
                                                       </Border>
                                                       <ControlTemplate.Triggers>
                                                           <Trigger Property="IsChecked" Value="True">
                                                               <Setter Property="Data" TargetName="arrow" Value="M1,4.5L4.5,1 8,4.5"/>
                                                           </Trigger>
                                                           <Trigger Property="IsMouseOver" Value="True">
                                                               <Setter Property="Stroke" TargetName="arrow" Value="Black"/>
                                                           </Trigger>
                                                           <Trigger Property="IsPressed" Value="True">
                                                               <Setter Property="Stroke" TargetName="arrow" Value="Black"/>
                                                           </Trigger>
                                                           <Trigger Property="IsEnabled" Value="False">
                                                               <Setter Property="Stroke" TargetName="arrow" Value="#FF707070"/>
                                                           </Trigger>
                                                       </ControlTemplate.Triggers>
                                                   </ControlTemplate>
                                               </Setter.Value>
                                           </Setter>
                                       </Style>
                                   </ToggleButton.Style>
                               </ToggleButton>
                               <ContentPresenter x:Name="ExpandSite" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" ContentStringFormat="{TemplateBinding ContentStringFormat}" DockPanel.Dock="Bottom" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" Visibility="Collapsed" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                           </DockPanel>
                       </Border>
                       <ControlTemplate.Triggers>
                           <Trigger Property="IsExpanded" Value="True">
                               <Setter Property="Visibility" TargetName="ExpandSite" Value="Visible"/>
                           </Trigger>
                           <Trigger Property="IsEnabled" Value="False">
                               <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                           </Trigger>
                       </ControlTemplate.Triggers>
                   </ControlTemplate>
               </Setter.Value>
           </Setter>
       </Style>

3.在头部同时添加了一个字体图标, 用FontFamily绑定字体, 通过设置Text实现图标

<TextBlock  FontFamily="/WpfApplication1;component/Resources/#iconfont" Text=""
                                   FontSize="22" Foreground="#918C8C" VerticalAlignment="Center"></TextBlock>

4.在Content区域, 利用一个stackPanel面板 和多个 单选按钮组成子元素【同时修改原生的RadioButton样式】

<Expander.Content>
                    <StackPanel Background="#F6F8F8">
                        <RadioButton Style="{DynamicResource RadioButtonStyle}">控制中心</RadioButton>
                        <RadioButton Style="{DynamicResource RadioButtonStyle}">资源管理</RadioButton>
                        <RadioButton Style="{DynamicResource RadioButtonStyle}">仪表菜单</RadioButton>
                        <RadioButton Style="{DynamicResource RadioButtonStyle}">顶部导航</RadioButton>
                        <RadioButton Style="{DynamicResource RadioButtonStyle}">通知中心</RadioButton>
                        <RadioButton Style="{DynamicResource RadioButtonStyle}">蓝牙设置</RadioButton>
                    </StackPanel>
                </Expander.Content>

5.修改stackpanel面板背景色, 打到header与子元素背景产生一定的色差 【图上蓝色区域】

6.给RadioButton添加一个Style / RadioButtonStyle 【具体样式见代码注释】

<Style x:Key="RadioButtonStyle" TargetType="{x:Type RadioButton}">
           <Setter Property="FocusVisualStyle">
               <Setter.Value>
                   <Style>
                       <Setter Property="Control.Template">
                           <Setter.Value>
                               <ControlTemplate>
                                   <Rectangle Margin="2" SnapsToDevicePixels="True" Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" StrokeThickness="1" StrokeDashArray="1 2"/>
                               </ControlTemplate>
                           </Setter.Value>
                       </Setter>
                   </Style>
               </Setter.Value>
           </Setter>             <!--Margin主要用于设置子元素距离左侧边距-->
           <Setter Property="Margin" Value="25 8 0 0"/>
           <Setter Property="FontSize" Value="20"/>
           <Setter Property="VerticalContentAlignment" Value="Center"/>
           <Setter Property="HorizontalContentAlignment" Value="Left"/>
           <Setter Property="BorderBrush" Value="Transparent"/>
           <Setter Property="Foreground" Value="#918C8C"/>
           <Setter Property="BorderThickness" Value="0"/>
           <Setter Property="Template">
               <Setter.Value>
                   <ControlTemplate TargetType="{x:Type RadioButton}">
                       <Grid x:Name="templateRoot" Background="Transparent" SnapsToDevicePixels="True">

                           <Border x:Name="border" BorderBrush="Red"
                                    BorderThickness="0"  Opacity="0.1"
                               Background="Transparent" SnapsToDevicePixels="True"/>                  <!-- 用于设置选中的左侧树形边框-->
                           <Border x:Name="bd2" BorderBrush="#2196F3" />
                           <ContentPresenter x:Name="contentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Grid.Column="1" ContentStringFormat="{TemplateBinding ContentStringFormat}" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                       </Grid>
                       <ControlTemplate.Triggers>
                           <Trigger Property="HasContent" Value="True">
                               <Setter Property="FocusVisualStyle">
                                   <Setter.Value>
                                       <Style>
                                           <Setter Property="Control.Template">
                                               <Setter.Value>
                                                   <ControlTemplate>
                                                       <Rectangle Margin="14,0,0,0" SnapsToDevicePixels="True" Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" StrokeThickness="1" StrokeDashArray="1 2"/>
                                                   </ControlTemplate>
                                               </Setter.Value>
                                           </Setter>
                                       </Style>
                                   </Setter.Value>
                               </Setter>
                               <Setter Property="Padding" Value="4,-1,0,0"/>
                           </Trigger>
                           <Trigger Property="IsMouseOver" Value="True">                   <!--当选中的时候, 改变字体的颜色, 同时左侧加一条宽度为2 的 边框 -->
                               <Setter Property="Foreground"  Value="#2196F3"/>
                               <Setter Property="BorderThickness" Value="2 0 0 0" TargetName="bd2"/>
                           </Trigger>
                           <Trigger Property="IsChecked" Value="true">
                           </Trigger>
                           <Trigger Property="IsChecked" Value="{x:Null}"/>
                       </ControlTemplate.Triggers>
                   </ControlTemplate>
               </Setter.Value>
           </Setter>
           <Setter Property="MinHeight" Value="20"/>
           <Setter Property="HorizontalAlignment" Value="Stretch"/>
       </Style>

到此这篇关于对WPF中Expander控件美化的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • 基于C# wpf 实现Grid内控件拖动详情

    目录 一.如何实现? 1.注册鼠标事件 2.记录位置 3.跟随鼠标移动 4.恢复标识 二.示例 前言: 有一些业务场景中我们需要拖动控件,在Grid中就可以实现控件拖动,通过设置Margin属性即可,根据鼠标的移动,设置相应的Margin的Left.Top,当然有时也不是直接设置的,需要根据HorizontalAlignment.VerticalAlignment值有不同的计算方法. 一.如何实现? 1.注册鼠标事件 拖动的控件需要注册3个鼠标事件分别是,鼠标按下.鼠标移动.鼠标弹起. 以But

  • C# WPF数据绑定模板化操作的完整步骤

    目录 前言: 具体实例代码如下: 总结 前言: WPF数据绑定对于WPF应用程序来说尤为重要,本文将讲述使用MVVM模式进行数据绑定的四步走用法: 具体实例代码如下: public class NotifyPropertyObject : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; public void RaisePropertyChanged(string propert

  • 解析OpenXml Pptx的边框虚线转为WPF的边框虚线问题

    安装Openxml sdk 首先,我们先安装nuget的需要的有关的Openxml sdk,我们开源了解析pptx的Openxml拍平层,下面两种方式都可以安装: nuget包管理器控制台: Install-Package dotnetCampus.DocumentFormat.OpenXml.Flatten -Version 2.0.0 csproj引用: <PackageReference Include="dotnetCampus.DocumentFormat.OpenXml.Fla

  • C# wpf解决Popup弹出位置异常问题解决

    目录 问题描述 原因分析 解决方法 问题描述 使用Popup控件作为弹出框,使用相对位置弹出即Placement="Relative",在不同的设备中弹出的位置不一致.比如下面的例子. 使用如下代码: <Window x:Class="WpfApp1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http

  • C# wpf简单颜色板的实现

    目录 前言 一.如何实现? 1.使用ObjectDataProvider 2.定义转换器 3.绑定容器 二.使用示例 1.代码 2.显示效果 前言 wpf本身没有提供颜色板之类的控件,有些业务使用场景需要使用颜色板之类的控件,比如设置弹幕的颜色或设置文本的颜色等.这里提供一种颜色板的简单实现方法. 一.如何实现? 1.使用ObjectDataProvider ObjectDataProvider是wpf中xaml绑定.net任意t类型的媒介,通过ObjectDataProvider可以直接获取到

  • 详解WPF中的对象资源

    在WPF中,所有继承自FrameworkElement的元素都包含一个Resources属性,这个属性就是我们这篇要讲的资源. 这一篇讲解的资源是不是上一篇的程序集资源(那个是在编译过程中打包到程序集中),这个是资源是我们想在公共的地方写一个对象让其他元素重复使用. 先贴个例子: <Window x:Class="NETResource.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/pre

  • WPF自定义路由事件的实例教程

    目录 路由事件模型 [分析代码] [自定义路由事件] 总结 路由事件模型 传统的简单事件模型中,在消息激发是将消息通过事件订阅的然后交给事件的相应者,事件的相应者使用事件的处理器来做出相应,这样就存在一个问题,用户控件内部的事件就不能被外界订阅,因为事件的宿主必须能够直接访问到事件的响应者. 路由事件的事件拥有者和事件的相应者之间则没有直接的显式订阅关系,事件的拥有者则只负责激发事件,事件将有谁相应它并不知道,事件的响应者则有事件的监听器,针对事件进行监听,当有此类事件传递至此事件响应者就使用事

  • WPF开发技巧之花式控件功能扩展详解

    目录 No1. 自定义控件模板 No2. 重写控件 No3. 附加属性来试试 总结 文章默认你已经入门WPF了 ​ WPF日常开发,经常遇到默认的控件功能不满足需求,怎么办? No1. 自定义控件模板 ​ 平时开发中,经常遇到比较"俗"的需求,嫌弃控件默认的样子.怎么办?哈哈,那就整个容呗.....

  • C# wpf 通过HwndHost渲染视频的实现方法

    目录 前言 一.如何实现 二.使用方式 三.示例 总结 前言 日常开发中,特别是音视频开发,需要在界面上渲染视频,比如制作一个播放器.或者视频编辑工具.以及视频会议客户端.通常拿到的是像素格式数据,此时需要渲染到wpf窗口上就需要一定的方法,本文介绍一种通过hwnd渲染的方法,控件既能提供hwnd又能嵌入wpf窗口里. 一.如何实现 通过继承HwndHost并实现抽象方法即可作为一个带句柄的wpf控件在xaml中使用,代码如下: win32Api版本: class NativeHost : Hw

  • 对WPF中Expander控件美化

    示例图: Expander控件功能很常见, 一般用于系统左侧的菜单收缩面板. 主要的组成 一个头部(header) 和 一个 内容(content) 组成. <Expander ExpandDirection="Down" SnapsToDevicePixels="True" VerticalAlignment="Top" Style="{DynamicResource ExpanderStyle1}" > <

  • WPF自定义Expander控件样式实现酷炫Style

    首先, 看一下效果图. 点我看视频教程 实现思路 1.PS处理两张选中得特效背景, 一张为主选择得效果图, 另外一张为次选择项得效果图. ![](//files.jb51.net/file_images/article/202201/2022128103603692.jpg) ![](//files.jb51.net/file_images/article/202201/2022128103615294.jpg) 图片资源定义 <!--静态资源--> <ImageBrush x:Key=

  • WPF中TreeView控件的用法

    在WPF的TreeView使用方式和WinForm下有很大不同,那些展开某节点.获取父节点,判断某节点是否被选中等常用的操作在WinForm下都有相关函数,而在WPF中却不能轻易实现. 一种常规的方式是通过MVVM模式来将TreeViewItem节点中的IsSelect,IsExpanded等属性来双向绑定到要显示的节点数据中,然后直接通过节点数据的属性来实现相关操作. 但是,有的时候,当我们没有ViewModel层,但又想像WinFrom那样直接简单的获取或设置这些属性的时候,该如何办呢.其实

  • 在WPF中动态加载XAML中的控件实例代码

    本文实例讲述了在WPF中动态加载XAML中的控件的方法.分享给大家供大家参考,具体如下: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using S

  • 针对BootStrap中tabs控件的美化和完善(推荐)

    BootStrap中的tabs控件以其简单易用而很受广大开发者的欢迎.但是,它的样式比较单一,如何才能在其原有的基础上做出更加美观的效果呢,我一直在考虑这个问题.另外,Bootstrap中的tabs必须要单击每个选项卡才能实现切换,能否使用Jquery来控制其自动切换,让它过一段时间(如5秒钟)从一个选项卡切换到另一个呢?下面是我的实现过程,首先是tabs部分的html代码: <div class="tab" role="tabpanel"> <!

  • C#中WPF颜色对话框控件的实现

    在 C# WPF开发中颜色对话框控件(ColorDialog)用于对界面中的背景.文字…(拥有颜色属性的所有控件)设置颜色,例如设置标签控件的背景色. 颜色对话框的运行效果如下图所示: 标签背景色设置后如下: xml代码: <Window x:Class="WpfApp.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http

  • C# wpf Canvas中实现控件拖动调整大小的示例

    目录 前言 一.功能说明 二.如何实现? 1.继承Adorner 2.使用Thumb 3.实现拖动逻辑 三.完整代码 四.使用示例 总结 前言 我们做图片编辑工具.视频编辑工具.或者画板有时需要实现控件缩放功能,比如图片或图形可以拉伸放大或缩小,实现这种功能通常需要8个点,对应4条边和4个角,在wpf中通常可以使用装饰器实现. 一.功能说明 8个点方放置在控件的8个方位上,通过拖动这些点对控件进行拉伸或缩小,示意图如下: 二.如何实现? 1.继承Adorner 通过装饰器的方式添加8个点在控件上

  • WPF的ListView控件自定义布局用法实例

    本文实例讲述了WPF的ListView控件自定义布局用法.分享给大家供大家参考,具体如下: 概要: 以源码的形式贴出,免得忘记后,再到网上查资料.在VS2008+SP1环境下调试通过 引用的GrayscaleEffect模块,可根据参考资料<Grayscale Effect...>中的位置下载. 正文: 如何布局是在App.xaml中定义源码如下 <Application x:Class="CWebsSynAssistant.App" xmlns="http

  • 超炫酷的WPF实现Loading控件效果

    Win8系统的Loading效果还是很不错的,网上也有人用CSS3等技术实现,研究了一下,并打算用WPF自定义一个Loading控件实现类似的效果,并可以让用户对Loading的颗粒(Particle)背景颜色进行自定义,话不多说,直接上代码: 1.用VS2012新建一个WPF的用户控件库项目WpfControlLibraryDemo,VS自动生成如下结构: 2.删除UserControl1.xaml,并新建一个Loading的CustomControl(不是UserControl),如下图所示

  • Android Shape控件美化实现代码

    如果你对Android系统自带的UI控件感觉不够满意,可以尝试下自定义控件,我们就以Button为例,很早以前Android123就写到过Android Button按钮控件美化方法里面提到了xml的selector构造.当然除了使用drawable这样的图片外今天Android开发网谈下自定义图形shape的方法,对于Button控件Android上支持以下几种属性shape.gradient.stroke.corners等.  我们就以目前系统的Button的selector为例说下: <s

随机推荐