C# wpf使用ListBox实现尺子控件的示例代码
作者:CodeOfCC 发布时间:2023-02-23 04:43:30
标签:C#,wpf,尺子
前言
尺子在客户端开发中有一定的应用场景,比如厘米尺、白板的画线尺、视频剪辑的时间尺。一般可以采用用户控件通过自绘的方式实现,但今天我要讲一个不一样的方法,不使用自定义控件也不用用户控件,只需要ListBox即能实现一把尺子。
一、如何实现?
1、设置横向ListBox
我们实现一把水平的尺子,所以需要让ListBox横向显示
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Horizontal"></VirtualizingStackPanel>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
2、Item设为刻度样式
一个Item就是一个刻度,我们通过ItemTemplate的方式设置样式。
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Width="10" Height="46" Orientation="Vertical" Background="Transparent">
<TextBlock x:Name="text" Margin="0,6,0,6" HorizontalAlignment="Center" FontSize="16" Text="{Binding Number}" Foreground="#ffffff" Visibility="{Binding NumberVisibility}"></TextBlock>
<Line x:Name="line" HorizontalAlignment="Center" Height="20" Width="5" X1="2.5" Y1="0" X2="2.5" Y2="25" StrokeThickness="1" Stroke="#aaaaaa"></Line>
</StackPanel>
</ListBox.ItemTemplate>
3、绑定数据源
由于ListBox是基于数据集合来显示控件的,我们通过绑定数据源让其显示刻度。
<ListBox ItemsSource="{Binding Chips}">
public class RulerChip
{
public double Number { get; set; }
public Visibility NumberVisibility { get; set; }
}
public List<RulerChip> Chips { get; set; }=new List<RulerChip>();
二、完整代码
MainWindow.xaml
<Window x:Class="WpfApp7.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp7"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<ListBox Background="#333333" Height="50" ItemsSource="{Binding Chips}" ScrollViewer.HorizontalScrollBarVisibility="Disabled" >
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<ContentPresenter
Content="{TemplateBinding Content}"
ContentStringFormat="{TemplateBinding ContentStringFormat}"
ContentTemplate="{TemplateBinding ContentTemplate}" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Horizontal"></VirtualizingStackPanel>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Width="10" Height="46" Orientation="Vertical" Background="Transparent">
<TextBlock x:Name="text" Margin="0,6,0,6" HorizontalAlignment="Center" FontSize="16" Text="{Binding Number}" Foreground="#ffffff" Visibility="{Binding NumberVisibility}"></TextBlock>
<Line x:Name="line" HorizontalAlignment="Center" Height="20" Width="5" X1="2.5" Y1="0" X2="2.5" Y2="25" StrokeThickness="1" Stroke="#aaaaaa"></Line>
</StackPanel>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding NumberVisibility}" Value="Hidden">
<Setter TargetName="line" Property="Y1" Value="3" />
</DataTrigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="line" Property="Stroke" Value="RoyalBlue" />
<Setter TargetName="text" Property="Foreground" Value="RoyalBlue" />
</Trigger>
</DataTemplate.Triggers>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Window>
MainWindow.xaml.cs
using System.Collections.Generic;
using System.Windows;
namespace WpfApp7
{
public class RulerChip
{
public double Number { get; set; }
public Visibility NumberVisibility { get; set; }
}
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public List<RulerChip> Chips { get; set; }=new List<RulerChip>();
public MainWindow()
{
InitializeComponent();
DataContext = this;
for (int i = 0; i < 100; i++)
{
Chips.Add(new RulerChip() { Number=i/10.0, NumberVisibility = (i%10==0)?Visibility.Visible:Visibility.Hidden});
}
}
}
}
三、效果预览
来源:https://blog.csdn.net/u013113678/article/details/125814664


猜你喜欢
- 本文较为详细的分析了vc提示unexpected end of file found的原因。分享给大家供大家参考。具体分析如下:预编译出错,
- 最近在做项目开始,涉及到服务器与安卓之间的接口开发,在此开发过程中发现了安卓与一般浏览器不同,安卓在每次发送请求的时候并不会带上上一次请求的
- 今天看到EOE问答里面有这“[Android 界面]NotificationManager 如何使用Bitmap做图标”这样一个问题,在论坛
- < application /> :应用的声明。 这个元素包含了子元素,这些子元素声明了应用的组件,元素的属性将会影响应用下的所
- 在Android开发中,我们经常会需要在Android界面上弹出一些对话框,比如询问用户或者让用户选择。这些功能我们叫它Android Di
- 这篇文章主要介绍了Mybatis 缓存原理及失效情况解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的
- 前言请求http的Demo是个人亲测过,目前该方式已经在线上运行着。因为是http请求,所有发送post 和get 请求的demo都有在下方
- 这篇文章主要介绍发送验证码和校验验证码的功能,用到一个第三方平台Bmob,那Bmob是什么呢?Bmob可以开发一个云存储的移动应
- 如果在类路径上添加了Spring Boot Security依赖项,则Spring Boot应用程序会自动为所有HTTP端点提供基本身份验证
- 相信大家都有这样的一个需求,选择相应开始时间和结束时间,对数据进行筛选,下面就将使用TimePickerView实现这么一个功能。一、先导入
- http请求绕过Filter的实现实例场景:两个web服务器,A当做服务端,B为客户端,B通过Hessian远程访问A。A上加了sessio
- 一、问题描述上周不是搭了个SpringBoot整合sharding-jdbc分库分表的架子么,组里老哥不让我把开发环境的配置文件放到reso
- 定义一个对象应该对其他对象保持最少的了解。问题由来类与类之间的关系越密切,耦合度越大,当一个类发生改变时,对另一个类的影响也越大。解决方案尽
- 本文以实例形式详细讲述了dotnetcharting控件的用法。分享给大家供大家参考。具体用法分析如下:dotnetcharting 是一个
- 数据层测试事务回滚pom.xml导入对应的一些坐标,mysql,Mp,等<dependency> &
- 发现问题最近在项目中刚刚修改一个功能,代码正准备验证,启动Idea的debug模式,运行项目,发现启动失败,查看日志发现定时任务被重复执行,
- 1:在很多APP里点击返回键,都可以看到返回键由亮变为暗2:实现方法也是很简单的(1)新建一个页面<RelativeLayout &n
- 本文实例为大家分享了Android seekbar实现可拖动进度条的具体代码,供大家参考,具体内容如下SeekBar通过滑块的位置来标识数值
- 我们经常在项目中使用的线程池,但是是否关心过线程池的关闭呢,可能很多时候直接再项目中直接创建线程池让它一直运行当任务执行结束不在需要了也不去
- Java注解的Excel导出依赖: <dependency> &