C# wpf使用ListBox實(shí)現(xiàn)尺子控件的示例代碼
前言
尺子在客戶端開發(fā)中有一定的應(yīng)用場(chǎng)景,比如厘米尺、白板的畫線尺、視頻剪輯的時(shí)間尺。一般可以采用用戶控件通過自繪的方式實(shí)現(xiàn),但今天我要講一個(gè)不一樣的方法,不使用自定義控件也不用用戶控件,只需要ListBox即能實(shí)現(xiàn)一把尺子。
一、如何實(shí)現(xiàn)?
1、設(shè)置橫向ListBox
我們實(shí)現(xiàn)一把水平的尺子,所以需要讓ListBox橫向顯示
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Horizontal"></VirtualizingStackPanel>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
2、Item設(shè)為刻度樣式
一個(gè)Item就是一個(gè)刻度,我們通過ItemTemplate的方式設(shè)置樣式。
<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、綁定數(shù)據(jù)源
由于ListBox是基于數(shù)據(jù)集合來顯示控件的,我們通過綁定數(shù)據(jù)源讓其顯示刻度。
<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});
}
}
}
}
三、效果預(yù)覽

總結(jié)
以上就是今天要講的內(nèi)容,本文僅僅簡(jiǎn)單介紹了ListBox實(shí)現(xiàn)尺子控件的方法,很容易實(shí)現(xiàn)。而且因?yàn)槭褂昧颂摂M化容器理論上性能很好,就算是幾百萬刻度繪制也估計(jì)不會(huì)卡頓。所以在此基礎(chǔ)上可以進(jìn)行一定的拓展,比如利用dpi實(shí)現(xiàn)物理尺子,以及實(shí)現(xiàn)時(shí)間尺的縮放功能等。總的來說,這是一個(gè)易于實(shí)現(xiàn)且拓展性也不錯(cuò)的尺子實(shí)現(xiàn)方案。更多相關(guān)C# wpf尺子內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
用C#獲取硬盤序列號(hào),CPU序列號(hào),網(wǎng)卡MAC地址的源碼
用C#獲取硬盤序列號(hào),CPU序列號(hào),網(wǎng)卡MAC地址的源碼...2007-03-03
C#創(chuàng)建安全的字典(Dictionary)存儲(chǔ)結(jié)構(gòu)
本文主要對(duì)存儲(chǔ)結(jié)構(gòu)字典(Dictionary)的一些常用方法進(jìn)行簡(jiǎn)單的說明,并闡述了如何創(chuàng)建安全的字典(Dictionary)存儲(chǔ)結(jié)構(gòu)。希望對(duì)大家有所幫助2016-12-12
C# 使用PictureBox實(shí)現(xiàn)圖片按鈕控件的示例步驟
這篇文章主要介紹了C# 使用PictureBox實(shí)現(xiàn)圖片按鈕控件的示例步驟,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下2021-02-02

