WPF Thumb 移动rectangle改变rectangle大小,旋转 rectangle mvvmlight提供实现代码
时间: 2023-07-26 07:36:35 浏览: 165
以下代码演示了如何使用WPF Thumb控件移动和调整大小Rectangle元素,并使用MVVMLight框架实现数据绑定。
首先,需要在XAML中定义Thumb和Rectangle元素,并将它们放置在Canvas中:
```xml
<Canvas>
<Thumb x:Name="moveThumb" DragDelta="MoveThumb_DragDelta"/>
<Thumb x:Name="resizeThumb" DragDelta="ResizeThumb_DragDelta"/>
<Rectangle Width="{Binding Width}" Height="{Binding Height}" Fill="Blue"/>
</Canvas>
```
然后,在视图模型中定义Width,Height和Rotation属性,并使用PropertyChanged事件实现数据绑定:
```c#
public class MainViewModel : ViewModelBase
{
private double _width = 100;
private double _height = 100;
private double _rotation = 0;
public double Width
{
get { return _width; }
set { Set(ref _width, value); }
}
public double Height
{
get { return _height; }
set { Set(ref _height, value); }
}
public double Rotation
{
get { return _rotation; }
set { Set(ref _rotation, value); }
}
// PropertyChanged event implementation
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
protected bool Set<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
{
if (Equals(storage, value)) return false;
storage = value;
OnPropertyChanged(propertyName);
return true;
}
}
```
在视图代码中,需要将Thumb元素与相应的操作连接起来:
```c#
public partial class MainWindow : Window
{
public MainViewModel ViewModel { get; set; }
public MainWindow()
{
InitializeComponent();
DataContext = ViewModel = new MainViewModel();
// Bind rectangle width and height to view model properties
BindingOperations.SetBinding(Rectangle, WidthProperty, new Binding("Width") { Source = ViewModel });
BindingOperations.SetBinding(Rectangle, HeightProperty, new Binding("Height") { Source = ViewModel });
// Bind move thumb position to rectangle position
BindingOperations.SetBinding(moveThumb, Canvas.LeftProperty, new Binding("Left") { Source = Rectangle });
BindingOperations.SetBinding(moveThumb, Canvas.TopProperty, new Binding("Top") { Source = Rectangle });
// Bind resize thumb position to rectangle right/bottom edges
BindingOperations.SetBinding(resizeThumb, Canvas.LeftProperty, new Binding("Left") { Source = Rectangle, Converter = new AddValueConverter(), ConverterParameter = ViewModel.Width });
BindingOperations.SetBinding(resizeThumb, Canvas.TopProperty, new Binding("Top") { Source = Rectangle, Converter = new AddValueConverter(), ConverterParameter = ViewModel.Height });
}
private void MoveThumb_DragDelta(object sender, DragDeltaEventArgs e)
{
ViewModel.Left += e.HorizontalChange;
ViewModel.Top += e.VerticalChange;
}
private void ResizeThumb_DragDelta(object sender, DragDeltaEventArgs e)
{
ViewModel.Width += e.HorizontalChange;
ViewModel.Height += e.VerticalChange;
}
}
public class AddValueConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is double && parameter is double)
{
return (double)value + (double)parameter;
}
return value;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
```
这样,当用户拖动或调整大小Thumb时,视图模型中的相应属性将被更新,并且Rectangle元素将自动更新大小和位置。同时,可以使用MVVMLight框架的PropertyChanged事件实现数据绑定,从而避免了手动实现INotifyPropertyChanged接口的繁琐过程。
阅读全文