WPF Thumb 移动rectangle改变rectangle大小,旋转 rectangle mvvmlight提供实现代码
时间: 2023-07-26 08:36:35 浏览: 143
好的,下面是一个简单的代码示例,演示了如何使用 WPF Thumb 控件实现拖动、改变大小和旋转矩形,以及如何使用 MVVMLight 框架来实现数据绑定。该示例中,我们使用了一个名为 RectangleViewModel 的 ViewModel 类来管理矩形的属性。
XAML 代码:
```
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApp1"
Title="MainWindow" Height="450" Width="800">
<Window.DataContext>
<local:RectangleViewModel/>
</Window.DataContext>
<Canvas>
<!-- 矩形 -->
<Rectangle Width="{Binding Width}"
Height="{Binding Height}"
Fill="LightBlue"
Canvas.Left="{Binding X}"
Canvas.Top="{Binding Y}">
<Rectangle.RenderTransform>
<RotateTransform Angle="{Binding Angle}" CenterX="0.5" CenterY="0.5"/>
</Rectangle.RenderTransform>
</Rectangle>
<!-- 拖动 Thumb -->
<Thumb Canvas.Left="{Binding X}"
Canvas.Top="{Binding Y}"
Width="10" Height="10"
DragDelta="Thumb_DragDelta"/>
<!-- 上边缘改变大小 Thumb -->
<Thumb Canvas.Left="{Binding X}"
Canvas.Top="{Binding Y}"
Width="10" Height="10"
Cursor="SizeNS"
DragDelta="ResizeTop_DragDelta"/>
<!-- 下边缘改变大小 Thumb -->
<Thumb Canvas.Left="{Binding X}"
Canvas.Top="{Binding Y}"
Width="10" Height="10"
Cursor="SizeNS"
DragDelta="ResizeBottom_DragDelta"/>
<!-- 左边缘改变大小 Thumb -->
<Thumb Canvas.Left="{Binding X}"
Canvas.Top="{Binding Y}"
Width="10" Height="10"
Cursor="SizeWE"
DragDelta="ResizeLeft_DragDelta"/>
<!-- 右边缘改变大小 Thumb -->
<Thumb Canvas.Left="{Binding X}"
Canvas.Top="{Binding Y}"
Width="10" Height="10"
Cursor="SizeWE"
DragDelta="ResizeRight_DragDelta"/>
<!-- 左上角改变大小 Thumb -->
<Thumb Canvas.Left="{Binding X}"
Canvas.Top="{Binding Y}"
Width="10" Height="10"
Cursor="SizeNWSE"
DragDelta="ResizeTopLeft_DragDelta"/>
<!-- 右上角改变大小 Thumb -->
<Thumb Canvas.Left="{Binding X}"
Canvas.Top="{Binding Y}"
Width="10" Height="10"
Cursor="SizeNESW"
DragDelta="ResizeTopRight_DragDelta"/>
<!-- 左下角改变大小 Thumb -->
<Thumb Canvas.Left="{Binding X}"
Canvas.Top="{Binding Y}"
Width="10" Height="10"
Cursor="SizeNESW"
DragDelta="ResizeBottomLeft_DragDelta"/>
<!-- 右下角改变大小 Thumb -->
<Thumb Canvas.Left="{Binding X}"
Canvas.Top="{Binding Y}"
Width="10" Height="10"
Cursor="SizeNWSE"
DragDelta="ResizeBottomRight_DragDelta"/>
<!-- 旋转 Thumb -->
<Thumb Canvas.Left="{Binding X}"
Canvas.Top="{Binding Y}"
Width="10" Height="10"
Cursor="Hand"
DragDelta="Rotate_DragDelta"/>
</Canvas>
</Window>
```
C# 代码:
```
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
namespace WpfApp1
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Thumb_DragDelta(object sender, DragDeltaEventArgs e)
{
RectangleViewModel viewModel = DataContext as RectangleViewModel;
viewModel.X += e.HorizontalChange;
viewModel.Y += e.VerticalChange;
}
private void ResizeTop_DragDelta(object sender, DragDeltaEventArgs e)
{
RectangleViewModel viewModel = DataContext as RectangleViewModel;
viewModel.Y += e.VerticalChange;
viewModel.Height -= e.VerticalChange;
}
private void ResizeBottom_DragDelta(object sender, DragDeltaEventArgs e)
{
RectangleViewModel viewModel = DataContext as RectangleViewModel;
viewModel.Height += e.VerticalChange;
}
private void ResizeLeft_DragDelta(object sender, DragDeltaEventArgs e)
{
RectangleViewModel viewModel = DataContext as RectangleViewModel;
viewModel.X += e.HorizontalChange;
viewModel.Width -= e.HorizontalChange;
}
private void ResizeRight_DragDelta(object sender, DragDeltaEventArgs e)
{
RectangleViewModel viewModel = DataContext as RectangleViewModel;
viewModel.Width += e.HorizontalChange;
}
private void ResizeTopLeft_DragDelta(object sender, DragDeltaEventArgs e)
{
RectangleViewModel viewModel = DataContext as RectangleViewModel;
viewModel.X += e.HorizontalChange;
viewModel.Width -= e.HorizontalChange;
viewModel.Y += e.VerticalChange;
viewModel.Height -= e.VerticalChange;
}
private void ResizeTopRight_DragDelta(object sender, DragDeltaEventArgs e)
{
RectangleViewModel viewModel = DataContext as RectangleViewModel;
viewModel.Width += e.HorizontalChange;
viewModel.Y += e.VerticalChange;
viewModel.Height -= e.VerticalChange;
}
private void ResizeBottomLeft_DragDelta(object sender, DragDeltaEventArgs e)
{
RectangleViewModel viewModel = DataContext as RectangleViewModel;
viewModel.X += e.HorizontalChange;
viewModel.Width -= e.HorizontalChange;
viewModel.Height += e.VerticalChange;
}
private void ResizeBottomRight_DragDelta(object sender, DragDeltaEventArgs e)
{
RectangleViewModel viewModel = DataContext as RectangleViewModel;
viewModel.Width += e.HorizontalChange;
viewModel.Height += e.VerticalChange;
}
private void Rotate_DragDelta(object sender, DragDeltaEventArgs e)
{
RectangleViewModel viewModel = DataContext as RectangleViewModel;
double centerX = viewModel.X + viewModel.Width / 2;
double centerY = viewModel.Y + viewModel.Height / 2;
Point mousePoint = Mouse.GetPosition(this);
Point centerPoint = new Point(centerX, centerY);
double angle = Math.Atan2(mousePoint.Y - centerPoint.Y, mousePoint.X - centerPoint.X) * 180 / Math.PI;
viewModel.Angle = angle;
}
}
public class RectangleViewModel : ViewModelBase
{
private double _x;
public double X
{
get { return _x; }
set
{
if (_x != value)
{
_x = value;
RaisePropertyChanged("X");
}
}
}
private double _y;
public double Y
{
get { return _y; }
set
{
if (_y != value)
{
_y = value;
RaisePropertyChanged("Y");
}
}
}
private double _width = 100;
public double Width
{
get { return _width; }
set
{
if (_width != value)
{
_width = value;
RaisePropertyChanged("Width");
}
}
}
private double _height = 100;
public double Height
{
get { return _height; }
set
{
if (_height != value)
{
_height = value;
RaisePropertyChanged("Height");
}
}
}
private double _angle;
public double Angle
{
get { return _angle; }
set
{
if (_angle != value)
{
_angle = value;
RaisePropertyChanged("Angle");
}
}
}
}
public class ViewModelBase : System.ComponentModel.INotifyPropertyChanged
{
public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
}
}
}
```
这个示例演示了如何使用 WPF Thumb 控件实现矩形的拖动、改变大小和旋转,以及如何使用 MVVMLight 框架实现数据绑定。请注意,这只是一个简单的示例,可以根据您的需要进行修改和扩展。
阅读全文