WPF-自定义实现步骤条控件

原文:WPF-自定义实现步骤条控件

步骤条实现的效果:

步骤条控件是在listbox的基础上实现的。

一、

xaml代码:

  <Window.Resources>
        <convert1:StepListBarWidthConverter x:Key="StepListStepWidthConverter" />
        <ControlTemplate x:Key="NormalItemTemplate" TargetType="ListBoxItem">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition />
                    <RowDefinition Height="20" />
                </Grid.RowDefinitions>
                <ContentPresenter HorizontalAlignment="Center" Content="{TemplateBinding Content}" />
                <Grid Grid.Row="1" Margin="2">
                    <Ellipse
                        Width="10"
                        Height="10"
                        HorizontalAlignment="Center"
                        VerticalAlignment="Center"
                        Fill="#55DCF5" />
                </Grid>
            </Grid>
        </ControlTemplate>
        <Style x:Key="StepListBoxStyle" TargetType="ListBox">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ListBox">
                        <Grid>
                            <Rectangle
                                Width="510"
                                Height="4"
                                Margin="0,0,0,8"
                                HorizontalAlignment="Center"
                                VerticalAlignment="Bottom"
                                Fill="#55DCF5" />
                            <ItemsPresenter />
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <ControlTemplate x:Key="SelectedTemplate" TargetType="ListBoxItem">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition />
                    <RowDefinition Height="20" />
                </Grid.RowDefinitions>
                <ContentPresenter HorizontalAlignment="Center" Content="{TemplateBinding Content}" />
                <Grid Grid.Row="1" Margin="2">
                    <Ellipse
                        Width="8"
                        Height="8"
                        VerticalAlignment="Center"
                        Panel.ZIndex="3">
                        <Ellipse.Fill>
                            <SolidColorBrush Color="#FFFFFF" />
                        </Ellipse.Fill>
                    </Ellipse>
                    <Ellipse
                        Width="12"
                        Height="12"
                        VerticalAlignment="Center"
                        Panel.ZIndex="2">
                        <Ellipse.Fill>
                            <SolidColorBrush Color="#225BA7" />
                        </Ellipse.Fill>
                    </Ellipse>
                    <Ellipse
                        Width="16"
                        Height="16"
                        VerticalAlignment="Center"
                        Panel.ZIndex="1">
                        <Ellipse.Fill>
                            <SolidColorBrush Color="#FFFFFF" />
                        </Ellipse.Fill>
                    </Ellipse>
                </Grid>
            </Grid>
        </ControlTemplate>
        <Style x:Key="ListBoxItemStyle" TargetType="ListBoxItem">
            <Setter Property="Width" Value="{Binding RelativeSource={RelativeSource AncestorType=ListBox}, Converter={StaticResource StepListStepWidthConverter}}" />
            <Setter Property="FontSize" Value="16" />
            <Setter Property="FontFamily" Value="SimHei" />
            <Setter Property="Foreground" Value="#ACF1FE" />
            <Setter Property="Template" Value="{StaticResource NormalItemTemplate}" />
            <Style.Triggers>
                <Trigger Property="IsSelected" Value="True">
                    <Trigger.Setters>
                        <Setter Property="Template" Value="{StaticResource SelectedTemplate}" />
                        <Setter Property="FontSize" Value="20" />
                        <Setter Property="Foreground" Value="White" />
                        <Setter Property="FontFamily" Value="SimHei" />
                    </Trigger.Setters>
                </Trigger>
            </Style.Triggers>
        </Style>

    </Window.Resources>
    <StackPanel Background="SteelBlue">
        <ListBox
            Margin="0 200 0 0"
            x:Name="NavList"
            HorizontalAlignment="Center"
            BorderThickness="0"
            Foreground="#225BA7"
            IsEnabled="False"
            ItemContainerStyle="{StaticResource ListBoxItemStyle}"
            SelectedIndex="4"
            Style="{StaticResource StepListBoxStyle}">
            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel IsItemsHost="False" Orientation="Horizontal" />
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>
            <ListBoxItem>1</ListBoxItem>
            <ListBoxItem>2</ListBoxItem>
            <ListBoxItem>3</ListBoxItem>
            <ListBoxItem>4</ListBoxItem>
            <ListBoxItem>5</ListBoxItem>
            <ListBoxItem>6</ListBoxItem>

        </ListBox>
        <StackPanel HorizontalAlignment="Center" Orientation="Horizontal">
            <Button Click="Button_Click">下一步</Button>
            <Button Margin="100,0,0,0" Click="Button_Click_1">首页</Button>
        </StackPanel>
    </StackPanel>

各个样式模板介绍:StepListBoxStyle,整个步骤条控件的样式,矩形长条模板。

NormalItemTemplate,未被选中时单个步骤样式。

SelectedTemplate,被选中时单个步骤样式。

ListBoxItemStyle,通过样式和触发器使用模板。

二、需要固定步骤条总长度,根据项数设置步骤条步长,所以需要写个转换器,设置每项长度。

转换器代码:

    class StepListBarWidthConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            ListBox listBox = value as ListBox;
            if (listBox==null)
            {
                return Binding.DoNothing;
            }
            if (listBox.Items.Count == 0)
            {
                return 0;
            }
            return 510 / (listBox.Items.Count - 1);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return Binding.DoNothing;
        }
    }

使用的时候对Listbox的ItemSource和SelectedIndex进行绑定即可。

原文地址:https://www.cnblogs.com/lonelyxmas/p/11964841.html

时间: 2024-08-30 01:05:00

WPF-自定义实现步骤条控件的相关文章

WPF自定义控件第一 - 进度条控件

本文主要针对WPF新手,高手可以直接忽略,更希望高手们能给出一些更好的实现思路. 前期一个小任务需要实现一个类似含步骤进度条的控件.虽然对于XAML的了解还不是足够深入,还是摸索着做了一个.这篇文章介绍下实现这个控件的步骤,最后会放出代码.还请高手们给出更好的思路.同时也希望这里的思路能给同道中人一些帮助.话不多说,开始正题. 实现中的一些代码采用了网上现有的方案,代码中通过注释标记了来源,再次对代码作者一并表示感谢. 首先放一张最终效果图. 节点可以被点击 控件会根据绑定的集合数据生成一系列节

WPF 自定义TextBox带水印控件,可设置圆角

一.简单设置水印TextBox控件,废话不多说看代码: <TextBox TextWrapping="Wrap" Margin="10" Height="69" Visibility="Visible"> <TextBox.Style> <Style TargetType="TextBox"> <Style.Triggers> <MultiTrigger

Photoshop和WPF双剑配合,打造炫酷个性的进度条控件

现在如果想打造一款专业的App,UI的设计和操作的简便性相当重要.UI设计可以借助Photoshop或者AI等设计工具,之前了解到WPF设计工具Expression Blend可以直接导入PSD文件或者AI设计文件(当然不是全部特征支持),最近研究了一下,也废了一番周折,好在最后实现了预期的效果.下面将step by step用示例说明如何先用PS构建一个矢量图形模板,然后用Expression Blend导入PSD文件,并获取PATH的Data值,为打造一款炫酷的个性进度条控件构建美观UI.

WPF 10天修炼 - 内容控件

WPF内容控件 在WPF中,所有呈现在用户界面上的对象都称为用户界面元素.但是只有派生自System.Windows.Controls.Control类的对象才称为控件.内容控件通常是指具有Content属性的控件,Content属性并非定义在每个控件中,而是定义在基类System.Windows.Controls命名空间的ContentControl类中.注意:Content属性只接收单个内容元素. WPF内容控件分类 1.  直接继承ContentControl的控件 2.  继承Heade

自定义下拉刷新控件

一.功能效果 1.在很多app中,在信息展示页面,当我们向下拖拽时,页面会加载最新的数据,并有一个短暂的提示控件出现,有些会有加载进度条,有些会记录加载日期.条目,有些还带有加载动画.其基本实现原理都相仿,本文中将探讨其实现原理,并封装出一个简单的下拉刷新控件 2.自定义刷新工具简单的示例 二.系统提供的下拉刷新工具 1.iOS6.0以后系统提供了自己的下拉刷新的控件:UIRefreshControl .例如,refreshControl,作为UITableViewController中的一个属

使用VideoView自定义一个播放器控件

介绍 最近要使用播放器做一个简单的视频播放功能,开始学习VideoView,在横竖屏切换的时候碰到了点麻烦,不过在查阅资料后总算是解决了.在写VideoView播放视频时候定义控制的代码全写在Actvity里了,写完一看我靠代码好乱,于是就写了个自定义的播放器控件,支持指定大小,可以横竖屏切换,手动左右滑动快进快退.好了,下面开始. 效果图 效果图有点卡,我也不知道为啥..... VideoView介绍 这个是我们实现视频播放最主要的控件,详细的介绍大家百度就去看,这里介绍几个常用的方法. 用于

[转载]ExtJs4 笔记(8) Ext.slider 滚轴控件、 Ext.ProgressBar 进度条控件、 Ext.Editor 编辑控件

作者:李盼(Lipan)出处:[Lipan] (http://www.cnblogs.com/lipan/)版权声明:本文的版权归作者与博客园共有.转载时须注明本文的详细链接,否则作者将保留追究其法律责任. 本篇要登场的有三个控件,分别是滚轴控件.进度条控件和编辑控件. 一.滚轴控件 Ext.slider 1.滚轴控件的定义 下面我们定义三个具有代表意义滚轴控件,分别展示滚轴横向.纵向,以及单值.多值选择的特性: [html] <h1>滚轴控件</h1> <div class

android - 自定义(组合)控件 + 自定义控件外观

转载:http://www.cnblogs.com/bill-joy/archive/2012/04/26/2471831.html android - 自定义(组合)控件 + 自定义控件外观 Android自定义View实现很简单 继承View,重写构造函数.onDraw,(onMeasure)等函数. 如果自定义的View需要有自定义的属性,需要在values下建立attrs.xml.在其中定义你的属性. 在使用到自定义View的xml布局文件中需要加入xmlns:前缀="http://sc

自定义快速查找字母控件

效果图如下: 首先看看布局文件,自定义的控件中包含一个 ListView,用于显示具体的数据内容: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="fill_parent"     a