在 OxyPlot wpf 上绘制

Draw on OxyPlot wpf

我对点的绘图有疑问。当我开始在 oxyplot canvas 上画线时,没有任何反应。但是当我开始超越 oxyplot 的界限时,它就起作用了。为什么它的工作如此奇怪?如何解决?

<Window x:Class="WpfApplication18DrawProblem.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:oxy="http://oxyplot.org/wpf"
    xmlns:lib="clr-namespace:DrawToolsLib;assembly=DrawToolsLib"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <Canvas  Name="paintSurface" MouseDown="Canvas_MouseDown_1" MouseMove="Canvas_MouseMove_1" Grid.Column="1" Background="Transparent">
    </Canvas>
    <oxy:Plot Model="{Binding PlotModel}" Controller="{Binding PlotController}" Height="300" Width="300" Background="Transparent" Grid.Column="0"></oxy:Plot>
</Grid>

using OxyPlot;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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.Navigation;
using System.Windows.Shapes;
using DrawToolsLib;
namespace WpfApplication18DrawProblem
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
/// 
public class TestData
{
    public PlotModel PlotModel { get; set; }
    private OxyPlot.Series.LineSeries LS;
    public PlotController PlotController { get; set; }
    public TestData()
    {
        PlotController = new OxyPlot.PlotController();
        PlotController.UnbindAll();
        Random rnd = new Random();
        PlotModel = new PlotModel();
        LS = new OxyPlot.Series.LineSeries();
        for (int i = 0; i < 10; i++)
        {
            LS.Points.Add(new DataPoint(i, rnd.NextDouble()));
        }
        PlotModel.Series.Add(LS);
    }
    public void Update()
    {
        PlotModel.InvalidatePlot(true);
    }
}
public partial class MainWindow : Window
{
    Point currentPoint = new Point();
    TestData TD = new TestData();
    public MainWindow()
    {

        DataContext = TD;
        InitializeComponent();
    }
    private void Canvas_MouseDown_1(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
        if (e.ButtonState == MouseButtonState.Pressed)
            currentPoint = e.GetPosition(this);
    }

    private void Canvas_MouseMove_1(object sender, System.Windows.Input.MouseEventArgs e)
    {

        if (e.LeftButton == MouseButtonState.Pressed)
        {
            Line line = new Line();
            Ellipse Ell = new Ellipse()
            {
                Stroke = Brushes.Black,
                StrokeThickness = 2,
            };

            line.Stroke = SystemColors.WindowFrameBrush;
            line.X1 = currentPoint.X;
            line.Y1 = currentPoint.Y;
            line.X2 = e.GetPosition(this).X;
            line.Y2 = e.GetPosition(this).Y;

            currentPoint = e.GetPosition(this);

            paintSurface.Children.Add(line);
        }
        TD.Update();

    }
}
}

XAML 文件中元素的顺序在这里起作用。如果将绘图区域 after/on 放在 Canvas 区域的顶部,Canvas 的那部分将被隐藏以供用户输入。

在这种情况下,最直接的解决方案是调换 XAML 文件中 oxy:PlotCanvas 元素的顺序:

...
<Grid>
  <oxy:Plot Model="{Binding PlotModel}" Controller="{Binding PlotController}" Height="300" Width="300" Background="Transparent" Grid.Column="0" />
  <Canvas Name="paintSurface" MouseDown="Canvas_MouseDown_1" MouseMove="Canvas_MouseMove_1" Grid.Column="1" Background="Transparent" />
</Grid>