您可以使用两个画布 - 画布内的画布。外层画布的大小是固定的 (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>