WPF Thumb控件移动缩放 mvvmlight
时间: 2023-09-28 17:12:02 浏览: 127
使用MVVM Light框架实现WPF Thumb控件的移动和缩放,可以通过绑定ViewModel的属性来实现控件的位置和大小的更新。
首先,在ViewModel中定义控件的位置和大小的属性。
```csharp
public class MainWindowViewModel : ViewModelBase
{
private double _left;
private double _top;
private double _width = 100;
private double _height = 100;
public double Left
{
get { return _left; }
set { Set(ref _left, value); }
}
public double Top
{
get { return _top; }
set { Set(ref _top, value); }
}
public double Width
{
get { return _width; }
set { Set(ref _width, value); }
}
public double Height
{
get { return _height; }
set { Set(ref _height, value); }
}
}
```
然后,在View中绑定Thumb控件的DragDelta事件和ViewModel的属性。
```xaml
<Grid>
<Rectangle Width="{Binding Width}" Height="{Binding Height}" Fill="Blue">
<Rectangle.RenderTransform>
<TransformGroup>
<TranslateTransform X="{Binding Left}" Y="{Binding Top}"/>
<ScaleTransform/>
</TransformGroup>
</Rectangle.RenderTransform>
</Rectangle>
<Thumb Width="10" Height="10" Canvas.Right="-5" Canvas.Bottom="-5"
DragDelta="Thumb_DragDelta"/>
</Grid>
```
在代码中,Thumb控件的DragDelta事件更新ViewModel的Left、Top、Width和Height属性。
```csharp
private void Thumb_DragDelta(object sender, DragDeltaEventArgs e)
{
var viewModel = DataContext as MainWindowViewModel;
viewModel.Left += e.HorizontalChange;
viewModel.Top += e.VerticalChange;
viewModel.Width = Math.Max(viewModel.Width + e.HorizontalChange, 0);
viewModel.Height = Math.Max(viewModel.Height + e.VerticalChange, 0);
}
```
这样就可以使用MVVM Light框架实现WPF Thumb控件的移动和缩放了。
阅读全文