简体   繁体   English

在WPF中进行选择性缩放和平移

[英]Selective zoom and pan in WPF

I need to implement zooming and panning around X-axis in my bus schedule graph viewer (and, possibly, editor) written in C#/WPF. 我需要在以C#/ WPF编写的公交时刻表图形查看器(可能还有编辑器)中实现围绕X轴的缩放和平移。 I could use simple transforms, but note that when the distance between points gets larger, the size of the points remains the same. 我可以使用简单的变换,但是请注意,当点之间的距离变大时,点的大小将保持不变。 Also, the names of the bus stations should remain on the fixed positions on the left, no matter how I pan the graph: 此外,无论我如何平移图表,公交车站的名称都应保留在左侧的固定位置:

变焦前变焦后

With the current approach, all the visuals are rendered on a single ItemsControl with Canvas as an ItemsPanel and several DataTemplates, one for each type of shape (point, segment, time split line, station line). 使用当前方法,所有视觉效果都呈现在单个ItemsControl上,其中Canvas作为ItemsPanel和几个DataTemplates呈现,每种形状对应一种形状(点,线段,时间分割线,桩号线)。 So, every shape is binded to according ViewModel, which has PosX and PosY properties, provided to Canvas: 因此,每个形状都根据提供给Canvas的具有PosX和PosY属性的ViewModel进行绑定:

<ItemsControl.ItemContainerStyle>
     <Style>
          <Setter Property="Canvas.Left"
                  Value="{Binding Path=PosX, UpdateSourceTrigger=PropertyChanged}" />
          <Setter Property="Canvas.Top"
                  Value="{Binding Path=PosY, UpdateSourceTrigger=PropertyChanged}" />
     </Style>
</ItemsControl.ItemContainerStyle>

When I need to pan or zoom, I call OnPropertyChanged("PosX") for viewmodel of each shape. 当我需要平移或缩放时,可以为每种形状的viewmodel调用OnPropertyChanged(“ PosX”)。 Then the property is recalculated with new values of PanX and ZoomX properties of entire graph: 然后,使用整个图形的PanX和ZoomX属性的新值重新计算该属性:

public double PosX
    {
        get
        {
            return _scheduleGraphViewModel.ZoomX * _shedulePointModel.PlanTime + _scheduleGraphViewModel.PanX;
        }
    }

The problem is that this works much slower than I was hoping. 问题是,这比我希望的要慢得多。 On 1000+ points it is almost unusable. 在1000+点上几乎是不可用的。 Profiler tells me that the bottleneck is inside OnPropertyChanged method. Profiler告诉我瓶颈在OnPropertyChanged方法内部。

I assume that the entire approach is wrong, but I cannot find or think out better solution. 我认为整个方法是错误的,但是我找不到或想出更好的解决方案。

Separate your graph a structure like this: 将您的图形分离成这样的结构:

ParentGrid1
    StationNames
    ParentGrid2        // Apply transformation on this
        LinesInGraph
        PointsInGraph
            Point1     // Apply inverse transformation on each of these
            Point2
            Point3
            ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM