如何固定从右到左绘制矩形

时间:2018-11-20 09:12:14

标签: c# wpf canvas

绘制矩形时要移到右侧

Right Image

当我在左侧绘制矩形时,它会变成这样

Left Image

这是我的代码:

ObservableCollection<RectItem> RectItems = new ObservableCollection<RectItem>();

public void MouseDownEvent(object sender, MouseButtonEventArgs e)
{
 startPoint = Mouse.GetPosition((IInputElement)sender);
}

public void MouseMoveEvent(object sender, MouseEventArgs e)
{

  if (e.LeftButton == MouseButtonState.Released) return;if (selected) return;if (_isDragging) return;var pos = Mouse.GetPosition((IInputElement)sender);

        // Set the position of rectangle
        var x = Math.Min(pos.X, startPoint.X);
        var y = Math.Min(pos.Y, startPoint.Y);

        // Set the dimenssion of the rectangle
        var w = Math.Max(pos.X, startPoint.X) - x;
        var h = Math.Max(pos.Y, startPoint.Y) - y;


        if (RectItems.Count != 0)
        {
            var exist = RectItems.Where(xx => xx.X == x).LastOrDefault();
            if (exist != null)
            {

                exist.Height = h;
                exist.Width = w;
            }
            else
            {
                RectItems.Add(new RectItem
                {
                    X = x,
                    Y = y,
                    Height = h,
                    Width = w,
                    id = RectItems.Count() + 1,
                });
            }
        }
        else
        {
            RectItems.Add(new RectItem
            {
                X = x,
                Y = y,
                Height = h,
                Width = w,
                id = RectItems.Count() + 1,
            });
         }

}         public void PreviewMouseLeftButtonUp(object sender,MouseButtonEventArgs e)         {             StopDragging();             e.Handled = true;         }

0 个答案:

没有答案