wpf 在WPF中使用MVVM拖动鼠标时绘制矩形

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16434356/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 08:44:45  来源:igfitidea点击:

Draw rectangle when mouse dragged using MVVM in WPF

c#.netwpfmvvmmvvm-foundation

提问by Narendra

Below is my xaml. I have an image inside a canvas. I want to draw rectangle on the image when mouse is dragged on the image. I did it successfully in WPF. But now I want to do it in MVVM. Instead of having the event handlers in code behind I want to have them in my ViewModel. I am using MVVM Foundationfor implementing MVVM. Below is a link to MVVM Foundation. http://mvvmfoundation.codeplex.com/

下面是我的xaml。我在画布中有一个图像。我想在图像上拖动鼠标时在图像上绘制矩形。我在 WPF 中成功做到了。但现在我想在 MVVM 中做到这一点。我希望在我的 ViewModel 中拥有它们,而不是在后面的代码中包含事件处理程序。我正在使用MVVM Foundation来实现 MVVM。以下是 MVVM 基金会的链接。http://mvvmfoundation.codeplex.com/

Any help is highly appreciated.

任何帮助都受到高度赞赏。

XAML

XAML

<Canvas Name="cnvImage">
        <Image Height="348" HorizontalAlignment="Left" Margin="12,39,0,0" Name="imgPreview" 
               Stretch="Fill" VerticalAlignment="Top" Width="443" 
               Source="/Images/CapturedImage.png" 
                MouseDown="imgCamera_MouseDown" MouseMove="imgCamera_MouseMove" MouseUp="imgCamera_MouseUp" />
    </Canvas>

Code written in code behind

写在代码后面的代码

// This is the rectangle to be shown when mouse is dragged on camera image.
private Point startPoint;
private Rectangle rectSelectArea;


/// <summary>
/// 
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void imgCamera_MouseDown(object sender, MouseButtonEventArgs e)
{
    startPoint = e.GetPosition(cnvImage);

    // Remove the drawn rectanglke if any.
    // At a time only one rectangle should be there
    if (rectSelectArea != null)
        cnvImage.Children.Remove(rectSelectArea);

    // Initialize the rectangle.
    // Set border color and width
    rectSelectArea = new Rectangle
    {
        Stroke = Brushes.LightBlue,
        StrokeThickness = 2
    };

    Canvas.SetLeft(rectSelectArea, startPoint.X);
    Canvas.SetTop(rectSelectArea, startPoint.X);
    cnvImage.Children.Add(rectSelectArea);
}

/// <summary>
/// 
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void imgCamera_MouseMove(object sender, MouseEventArgs e)
{
    if (e.LeftButton == MouseButtonState.Released || rectSelectArea == null)
        return;

    var pos = e.GetPosition(cnvImage);

    // 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;

    rectSelectArea.Width = w;
    rectSelectArea.Height = h;

    Canvas.SetLeft(rectSelectArea, x);
    Canvas.SetTop(rectSelectArea, y);
}

/// <summary>
/// 
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void imgCamera_MouseUp(object sender, MouseButtonEventArgs e)
{
    rectSelectArea = null;
}

I need to know what do I need to write in my viewmodel and accordingly what changes are required in XAML.

我需要知道我需要在我的视图模型中写什么,以及相应地在 XAML 中需要哪些更改。

Thanks in advance.

提前致谢。

采纳答案by Sebastian Edelmeier

A very neat way of implementing resizing can be found in this article / project. If you use the DesignerItemStyle implemented there, you can add binding support like so :

这篇文章/项目中可以找到一种非常巧妙的调整大小的方法。如果您使用在那里实现的 DesignerItemStyle,您可以像这样添加绑定支持:

<Rectangle Style="{StaticResource DesignerItemStyle}"
           Canvas.Left="{Binding Path=Leftoffset, Mode=TwoWay}"
           Canvas.Top="{Binding Path=Topoffset, Mode=TwoWay}"
           Width="{Binding Path=Width, Mode=TwoWay}"
           Height="{Binding Path=Height, Mode=TwoWay}">    

That leaves the drag to resize stuff in pure XAML and uses standard WPF means to get the values to the underlying ViewModels.

这留下了在纯 XAML 中调整内容大小的阻力,并使用标准 WPF 方法将值获取到基础 ViewModel。

回答by Rakesh Patil

Just Refer the link given below Visit Code project!

只需参考下面给出的访问代码项目的链接!

http://www.codeproject.com/Articles/148503/Simple-Drag-Selection-in-WPF?

http://www.codeproject.com/Articles/148503/Simple-Drag-Selection-in-WPF