0

I would like to draw a graph of a stock on a canvas.
The canvas has an initil size lets say 300 (height) x 1000 (Width).

The stock has been trading for more days that can fit into the visual part of the Canvas
I would like to drag (scroll) the visual part of the canvas around and see the non visual parts that are hidden "behind". i.e see trading days that are not initially displyed.

Would appreciate any ideas or pointers to get me on the right path.

4

1 回答 1

0

您可以使用两个画布 - 画布内的画布。外层画布的大小是固定的 (1000 x 300),但内层画布的大小将与您想要的一样大。将ClipToBounds外部画布的属性设置为True.

C#:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Stock
{
    /// <summary>
    /// Interaction logic for ListBox.xaml
    /// </summary>
    public partial class Chart : Window
    {
        private Point _capturePoint;
        private double _captureLeft;
        private double _captureTop;

        public Chart()
        {
            InitializeComponent();

        }

        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            base.OnMouseLeftButtonDown(e);

            _capturePoint = e.GetPosition(chart);
            _captureLeft = Canvas.GetLeft(chart);
            _captureTop = Canvas.GetTop(chart);

            if (_capturePoint.X > 0 && _capturePoint.X < chart.RenderSize.Width &&
                _capturePoint.Y > 0 && _capturePoint.Y < chart.RenderSize.Height) 
            {
                CaptureMouse();
            }

        }

        protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
        {
            base.OnMouseLeftButtonUp(e);
            ReleaseMouseCapture();
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);

            if (Mouse.Captured == this)
            {
                var currentPoint = e.GetPosition(chart);
                var diff = Point.Subtract(currentPoint, _capturePoint);

                Canvas.SetTop(chart, _captureTop + diff.Y);
                Canvas.SetLeft(chart, _captureLeft + diff.X);
            }
        }
    }
}

XAML:

<Window x:Class="Stock.Chart"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="ListBox" Height="300" Width="300">
        <Canvas Height="300" Width="1000" x:Name="viewport" ClipToBounds="True">
            <Canvas x:Name="chart" Background="Green" Width="2000" Height="1000" Canvas.Top="20" Canvas.Left="10" >

            </Canvas>
        </Canvas>
</Window>
于 2012-10-22T01:33:42.270 回答