C# wpf 使用ListBox实现尺子控件


前言

尺子在客户端开发中有一定的应用场景,比如厘米尺、白板的画线尺、视频剪辑的时间尺。一般可以采用用户控件通过自绘的方式实现,但今天我要讲一个不一样的方法,不使用自定义控件也不用用户控件,只需要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实现物理尺子,以及实现时间尺的缩放功能等。总的来说,这是一个易于实现且拓展性也不错的尺子实现方案。

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
使用 MVVM 模式实现 WPF ListBox 的右键菜单功能,可以按照以下步骤进行: 1. 在 ViewModel 中创建一个 ICommand 属性,用于处理右键菜单命令,例如: ``` public ICommand ContextMenuItemCommand { get; set; } ``` 2. 在 ViewModel 的构造函数中初始化 ContextMenuItemCommand 属性,并定义命令执行的方法,例如: ``` public ViewModel() { ContextMenuItemCommand = new RelayCommand<string>(ExecuteContextMenuItemCommand); } private void ExecuteContextMenuItemCommand(string command) { switch (command) { case "Copy": // 处理复制命令 break; case "Cut": // 处理剪切命令 break; case "Paste": // 处理粘贴命令 break; default: break; } } ``` 3. 在 XAML 中将 ContextMenuItemCommand 属性绑定到 ListBox 控件的 ContextMenu 中的 MenuItem 控件的 Command 属性,例如: ``` <ListBox> <ListBox.ContextMenu> <ContextMenu> <MenuItem Header="复制" Command="{Binding ContextMenuItemCommand}" CommandParameter="Copy" /> <MenuItem Header="剪切" Command="{Binding ContextMenuItemCommand}" CommandParameter="Cut" /> <MenuItem Header="粘贴" Command="{Binding ContextMenuItemCommand}" CommandParameter="Paste" /> </ContextMenu> </ListBox.ContextMenu> </ListBox> ``` 4. 运行程序并在 ListBox 上右键单击以查看菜单。 这样,当用户在 ListBox 上右键单击时,将会显示一个包含复制、剪切和粘贴菜单项的上下文菜单,并且当用户选择其中的菜单项时,会触发 ViewModel 中定义的 ContextMenuItemCommand 命令,并执行相应的命令处理方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CodeOfCC

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值