You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于Helix Toolkit实现支持平移缩放与MVVM的2D线条视图

使用Helix Toolkit实现支持MVVM的2D平移缩放线条视图工具

核心实现思路

借助HelixToolkit.Wpf的HelixViewport2D控件实现原生平移、缩放交互,通过MVVM模式分离线条数据与视图:用Model封装线条属性,ViewModel管理线条集合,View通过数据绑定渲染图形,完全遵循关注点分离原则。

步骤1:安装依赖

通过NuGet安装HelixToolkit.Wpf包:

Install-Package HelixToolkit.Wpf

步骤2:定义线条数据模型(Model)

实现INotifyPropertyChanged确保属性变更同步到UI:

using System.ComponentModel;
using System.Runtime.CompilerServices;

public class LineModel : INotifyPropertyChanged
{
    private double _startX;
    private double _startY;
    private double _endX;
    private double _endY;
    private System.Windows.Media.Brush _stroke = System.Windows.Media.Brushes.Black;

    public double StartX
    {
        get => _startX;
        set { _startX = value; OnPropertyChanged(); }
    }

    public double StartY
    {
        get => _startY;
        set { _startY = value; OnPropertyChanged(); }
    }

    public double EndX
    {
        get => _endX;
        set { _endX = value; OnPropertyChanged(); }
    }

    public double EndY
    {
        get => _endY;
        set { _endY = value; OnPropertyChanged(); }
    }

    public System.Windows.Media.Brush Stroke
    {
        get => _stroke;
        set { _stroke = value; OnPropertyChanged(); }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

步骤3:实现ViewModel

管理线条集合,示例中初始化测试数据,可扩展动态添加/删除线条的逻辑:

using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;

public class MainViewModel : INotifyPropertyChanged
{
    private ObservableCollection<LineModel> _lines;

    public ObservableCollection<LineModel> Lines
    {
        get => _lines;
        set { _lines = value; OnPropertyChanged(); }
    }

    public MainViewModel()
    {
        Lines = new ObservableCollection<LineModel>
        {
            new LineModel { StartX = 0, StartY = 0, EndX = 100, EndY = 100 },
            new LineModel { StartX = 100, StartY = 0, EndX = 0, EndY = 100 },
            new LineModel { StartX = 50, StartY = 0, EndX = 50, EndY = 100 }
        };
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

步骤4:编写View(XAML)

使用HelixViewport2D开启平移缩放,通过ItemsControl绑定线条集合,用Path渲染每条线条:

<Window x:Class="Helix2DLineView.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:helix="clr-namespace:HelixToolkit.Wpf;assembly=HelixToolkit.Wpf"
        xmlns:local="clr-namespace:Helix2DLineView"
        Title="2D Line View" Height="600" Width="800">
    <Window.DataContext>
        <local:MainViewModel/>
    </Window.DataContext>
    <Grid>
        <!-- 开启鼠标滚轮缩放、左键拖拽平移 -->
        <helix:HelixViewport2D ZoomMode="MouseWheel" PanMode="LeftMouseButton" Background="White">
            <ItemsControl ItemsSource="{Binding Lines}">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Path Stroke="{Binding Stroke}" StrokeThickness="2">
                            <Path.Data>
                                <LineGeometry>
                                    <LineGeometry.StartPoint>
                                        <MultiBinding Converter="{local:DoubleToPointConverter}">
                                            <Binding Path="StartX"/>
                                            <Binding Path="StartY"/>
                                        </MultiBinding>
                                    </LineGeometry.StartPoint>
                                    <LineGeometry.EndPoint>
                                        <MultiBinding Converter="{local:DoubleToPointConverter}">
                                            <Binding Path="EndX"/>
                                            <Binding Path="EndY"/>
                                        </MultiBinding>
                                    </LineGeometry.EndPoint>
                                </LineGeometry>
                            </Path.Data>
                        </Path>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </helix:HelixViewport2D>
    </Grid>
</Window>

辅助:坐标转换转换器

将Model的X/Y数值转为Point类型供LineGeometry使用:

using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;

namespace Helix2DLineView
{
    public class DoubleToPointConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            if (values.Length >= 2 && values[0] is double x && values[1] is double y)
            {
                return new Point(x, y);
            }
            return new Point(0, 0);
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

交互说明

  • 平移:按住鼠标左键拖拽视图
  • 缩放:滚动鼠标滚轮
  • 若需代码控制视图(如一键适配所有线条),可通过绑定HelixViewport2D的ZoomToFit方法或修改ViewportZoom、ViewportOffset属性实现。

内容的提问来源于stack exchange,提问作者user16918900

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 21:13:41