C# wpf使用ListBox实现尺子控件的示例代码
作者:CodeOfCC 时间:2023-02-23 04:43:30
前言
尺子在客户端开发中有一定的应用场景,比如厘米尺、白板的画线尺、视频剪辑的时间尺。一般可以采用用户控件通过自绘的方式实现,但今天我要讲一个不一样的方法,不使用自定义控件也不用用户控件,只需要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
标签:C#,wpf,尺子
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
从零开始学springboot整合feign跨服务调用的方法
2023-05-15 18:30:22
![](https://img.aspxhome.com/file/2023/5/81895_0s.png)
Windows10系统下JDK1.8的下载安装及环境变量配置的教程
2022-03-18 18:14:03
![](https://img.aspxhome.com/file/2023/7/62507_0s.jpg)
JavaWeb框架MVC设计思想详解
2022-09-09 06:43:50
![](https://img.aspxhome.com/file/2023/2/78012_0s.jpg)
谈谈你可能并不了解的java枚举
2023-11-09 21:08:55
![](https://img.aspxhome.com/file/2023/1/59021_0s.png)
java中如何执行xshell命令
2021-10-06 16:22:18
Spring5路径匹配器PathPattern解析
2021-07-09 00:12:28
IDEA不编译除了.java之外的文件的解决办法(推荐)
2023-09-16 15:50:44
![](https://img.aspxhome.com/file/2023/0/58570_0s.png)
初步编写IDEA\\AndroidStudio翻译插件的方法
2022-12-14 19:30:30
![](https://img.aspxhome.com/file/2023/9/96029_0s.png)
Java关键字、标识符、常量、变量语法详解
2023-11-11 20:49:48
![](https://img.aspxhome.com/file/2023/4/67624_0s.png)
Java如何设置PDF文档背景色详解
2022-07-08 08:17:19
![](https://img.aspxhome.com/file/2023/7/77947_0s.png)
面试中遇到的java逃逸分析问题
2021-12-08 11:58:28
Java实现飞机航班管理系统的思路详解
2022-08-20 19:48:42
![](https://img.aspxhome.com/file/2023/6/78386_0s.jpg)
Java 集合框架之List 的使用(附小游戏练习)
2023-11-24 10:33:40
![](https://img.aspxhome.com/file/2023/7/59947_0s.png)
struts2实现文件上传显示进度条效果
2021-11-19 16:16:21
java中String.intern()方法功能介绍
2023-11-25 23:49:21
Springboot Mybatis Plus自动生成工具类详解代码
2022-09-17 12:01:57
flutter中使用流式布局示例详解
2023-08-24 23:49:06
![](https://img.aspxhome.com/file/2023/5/86765_0s.jpg)
C# 文件上传下载(Excel导入,多线程下载)功能的实现代码
2021-12-09 20:16:34
两天没解决的问题chatgpt用了5秒搞定隐藏bug
2023-11-18 22:54:08
![](https://img.aspxhome.com/file/2023/5/59245_0s.jpg)
Linux下用java -jar运行可执行jar包的方法教程
2022-11-24 18:01:46