C# wpf使用ListBox實現(xiàn)尺子控件的示例代碼
前言
尺子在客戶端開發(fā)中有一定的應用場景,比如厘米尺、白板的畫線尺、視頻剪輯的時間尺。一般可以采用用戶控件通過自繪的方式實現(xiàn),但今天我要講一個不一樣的方法,不使用自定義控件也不用用戶控件,只需要ListBox即能實現(xiàn)一把尺子。
一、如何實現(xiàn)?
1、設置橫向ListBox
我們實現(xiàn)一把水平的尺子,所以需要讓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、綁定數(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});
}
}
}
}
三、效果預覽

總結
以上就是今天要講的內容,本文僅僅簡單介紹了ListBox實現(xiàn)尺子控件的方法,很容易實現(xiàn)。而且因為使用了虛擬化容器理論上性能很好,就算是幾百萬刻度繪制也估計不會卡頓。所以在此基礎上可以進行一定的拓展,比如利用dpi實現(xiàn)物理尺子,以及實現(xiàn)時間尺的縮放功能等??偟膩碚f,這是一個易于實現(xiàn)且拓展性也不錯的尺子實現(xiàn)方案。更多相關C# wpf尺子內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
C#中Winform 實現(xiàn)Ajax效果自定義按鈕
這篇文章主要介紹了C#中Winform 實現(xiàn)Ajax效果自定義按鈕的相關資料,需要的朋友可以參考下2017-12-12
C#中的應用程序接口介紹及實現(xiàn),密封類與密封方法
今天小編就為大家分享一篇關于C#中的應用程序接口介紹及實現(xiàn),密封類與密封方法,小編覺得內容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-10-10
C# WPF如何反射加載Geometry幾何圖形數(shù)據(jù)圖標
這篇文章主要介紹了C# WPF如何反射加載Geometry幾何圖形數(shù)據(jù)圖標,幫助大家更好的理解和學習使用c#,感興趣的朋友可以了解下2021-03-03

