当前位置:网站首页>C# wpf 使用ListBox实现尺子控件
C# wpf 使用ListBox实现尺子控件
2022-07-17 22:57:00 【CodeOfCC】
前言
尺子在客户端开发中有一定的应用场景,比如厘米尺、白板的画线尺、视频剪辑的时间尺。一般可以采用用户控件通过自绘的方式实现,但今天我要讲一个不一样的方法,不使用自定义控件也不用用户控件,只需要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});
}
}
}
}
三、效果预览

总结
以上就是今天要讲的内容,本文仅仅简单介绍了ListBox实现尺子控件的方法,很容易实现。而且因为使用了虚拟化容器理论上性能很好,就算是几百万刻度绘制也估计不会卡顿。所以在此基础上可以进行一定的拓展,比如利用dpi实现物理尺子,以及实现时间尺的缩放功能等。总的来说,这是一个易于实现且拓展性也不错的尺子实现方案。
边栏推荐
- 天勤第九章课后习题代码
- Tianqin Chapter 9 after class exercise code
- A - trees on the level
- 最大堆与堆排序和优先队列
- 【微服务】 微服务学习笔记三:利用Feign替换RestTemplate完成远程调用
- Face technology: the picture of unclear people is repaired into a high-quality and high-definition image framework (with source code download)
- Several points to be analyzed in the domestic fpga/dsp/zynq scheme
- SQL wrong questions set of Niuke brush questions
- 009 面试题 SQL语句各部分的执行顺序
- MMRotate从零开始训练自己的数据集
猜你喜欢

Li Hongyi machine learning -- return to July 13, 2022

08_服务熔断Hystrix

State machine exercise

csrf防护机制

Leetcode 1275. Trouver le vainqueur de "Jingzi"

How to read and save point cloud data with numpy

Achieve the effect of software login account by authorizing wechat ~ ~ unfinished

Leetcode 1296. Divide the array into a set of consecutive numbers (solved)

ICML2022 | 几何多模态对比表示学习

【Renesas RA6M4开发板之I2C读取mpu6050】
随机推荐
2034: [蓝桥杯2022初赛] 修剪灌木
Read the paper: temporary graph networks for deep learning on dynamic graphs
分布式事务总结
08_服务熔断Hystrix
[code attached] how to realize handwritten digit recognition with hog+svm
[cute new problem solving] sum of four numbers
Several points to be analyzed in the domestic fpga/dsp/zynq scheme
LeetCode 912.排序
Chang'an chain learning research - storage analysis wal mechanism
Unix ls
Single channel 6Gsps 12 bit AD acquisition and single channel 6Gsps 16 bit Da (ad9176) output sub card based on vita57.1 standard
Istio XDS configuration generation implementation
人脸技术:不清楚人照片修复成高质量高清晰图像框架(附源代码下载)
C - Matrix Chain Multiplication(栈的应用)
微信小程序9-发布代码
Integrated video processing card based on Fudan micro FPGA + Huawei hisilic hi3531dv200 + Mji innovative MCU
Icml2022 | géométrie multimodale Contrastive Representation Learning
Knapsack problem
csrf防护机制
kibana 使用json文档数据