WPF Thumb 实现rectangle 移动
时间: 2023-08-12 13:33:48 浏览: 180
要使用 WPF Thumb 实现 Rectangle 移动,可以按照以下步骤进行操作:
1. 在 XAML 中创建 Rectangle 和 Thumb 控件,如下所示:
```xml
<Grid>
<Rectangle Width="100" Height="100" Fill="Red" Margin="50"/>
<Thumb Width="10" Height="10" DragDelta="Thumb_DragDelta"/>
</Grid>
```
2. 在代码中实现 Thumb 的 DragDelta 事件处理程序,用于控制 Rectangle 的移动:
```csharp
private void Thumb_DragDelta(object sender, DragDeltaEventArgs e)
{
var thumb = sender as Thumb;
var rectangle = thumb.Parent as Rectangle;
if (rectangle != null)
{
Canvas.SetLeft(rectangle, Canvas.GetLeft(rectangle) + e.HorizontalChange);
Canvas.SetTop(rectangle, Canvas.GetTop(rectangle) + e.VerticalChange);
}
}
```
在事件处理程序中,首先获取 Thumb 的父级 Rectangle 控件,然后使用 Canvas.SetLeft 和 Canvas.SetTop 方法来改变 Rectangle 的位置。
3. 运行程序,拖动 Thumb 控件可以移动 Rectangle 控件。
注意,如果使用其他容器控件,如 Grid 或 StackPanel,需要将 Canvas.SetLeft 和 Canvas.SetTop 替换为相应容器控件的布局属性,如 Grid.Column 和 Grid.Row。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044736.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![7z](https://img-home.csdnimg.cn/images/20241231044736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)