首页 > 其他分享 >WPF 精修篇 拖拽 DragDrop

WPF 精修篇 拖拽 DragDrop

时间:2023-02-28 10:33:08浏览次数:28  
标签:UIElement void object private item DragDrop 精修 WPF data


WPF 实现拖拽

效果

WPF 精修篇 拖拽 DragDrop_Source

<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="197*"/>
<ColumnDefinition Width="209*"/>
<ColumnDefinition Width="111*"/>
</Grid.ColumnDefinitions>
<WrapPanel x:Name="accept" HorizontalAlignment="Left" Height="320" VerticalAlignment="Top" Width="207" Grid.Column="1" Margin="2,0,0,0" Background="#FFE7FFE5" Drop="WrapPanel_Drop" AllowDrop="True" />
<WrapPanel x:Name="send" HorizontalAlignment="Left" Height="320" VerticalAlignment="Top" Width="197" Background="#FFFFEDCD" MouseLeftButtonDown="WrapPanel_MouseLeftButtonDown">
<Label Content="Label1" Height="31" Width="51" Background="#FFDCA1A1" Margin="5"/>
<Label Content="Label2" Height="31" Width="51" Background="#FFDCA1A1" Margin="5"/>
<Label Content="Label3" Height="31" Width="51" Background="#FFDCA1A1" Margin="5"/>
<Label Content="Label4" Height="31" Width="51" Background="#FFDCA1A1" Margin="5"/>
<Label Content="Label5" Height="31" Width="51" Background="#FFDCA1A1" Margin="5"/>
<Label Content="Label6" Height="31" Width="51" Background="#FFDCA1A1" Margin="5"/>
</WrapPanel>

</Grid>
private void WrapPanel_Drop(object sender, DragEventArgs e)
{

var item = e.Data;
object data = item.GetData(item.GetFormats()[0]);
if (data is UIElement)
{
send.Children.Remove(data as UIElement);
accept.Children.Add(data as UIElement);
}

}

private void WrapPanel_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
object item = e.Source;
if (item is UIElement)
{
DragDrop.DoDragDrop(item as UIElement, item, DragDropEffects.Move);
}
}

accept 一方的控件 需要加上  AllowDrop="True" 允许接收drop的数据

标签:UIElement,void,object,private,item,DragDrop,精修,WPF,data
From: https://blog.51cto.com/u_14483572/6090360

相关文章

  • WPF 精修篇 依赖属性
    依赖属性使用场景1.希望可在样式中设置属性。2.希望属性支持数据绑定。3.希望可使用动态资源引用设置属性。4.希望从元素树中的父元素自动继承属性值。5.希望属性可进......
  • WPF下字体模糊的问题
    一直以来,发现WPF中的小字体下的文字变得比较模糊,比如:WPF与Winform字体显示比较:为了看到更清楚,我们放大点显示: 放得更大些:中文、日文等亚洲文字的显示也存在着类似的问题:在......
  • WPF中的文字修饰——上划线,中划线,基线与下划线
    我们知道,文字的修饰包括:空心字、立体字、划线字、阴影字、加粗、倾斜等。这里只说划线字的修饰方式,按划线的位置,我们可将之分为:上划线、中划线、基线与下划线。如图:从上至下......
  • 给WPF文字加多条修饰线
    这是上篇​​WPF中的文字修饰——上划线,中划线,基线与下划线​​效果图:XAML代码:<Pagexmlns="​​http://schemas.microsoft.com/winfx/2006/xaml/presentation​​​"xmlns......
  • WPF应用程序顶级标签一定是Window吗?
    WPF应用程序顶级标签一定是Window吗? 很多人误以为是。可是,答案却是否定的。我们不妨来测试一下。首先使用顶级标签为Window,这是最普通、也是最常见的情况。新建一个WPF应......
  • 距离北京奥运还有359天,发布WPF版本的北京2008标志(下)
    图片显示效果: XAML代码:<ViewboxWidth="463.548828"Height="370.816895"xmlns="​​​http://schemas.microsoft.com/winfx/2006/xaml/presentation​​​"xmlns:x=......
  • WPF中,Grid与Table的区别(英文)
    HowisGridDifferentfromTable?TableandGridsharesomecommonfunctionality,buteachisbestsuitedfordifferentscenarios.(1)GridderivesfromthePanel......
  • WPF 获取拖拽网页图片链接
    在浏览器里拖拽一个元素,我只获取图片链接privatevoidGrid_PreviewDragOver(objectsender,DragEventArgse){e.Effects=DragDropEffects.A......
  • 从.net Framework4.6WPF升级到.netcore3.1/net5/6/7.0版本
    因项目需要,需将.netFramework4.6WPF升级到.netcore3.1/net5.0/6.0/7.0版本,通过很多办法解决,开始搞得一头雾水。终于,找到了办法。1、首先下载upgrade-assistant工具(.net升级......
  • WPF知识点备忘录——控件模板
    模板<Application.Resources><ResourceDictionary><!--将画刷等从模板拆分出来,方便重用--><RadialGradientBrushRadiusX="1"R......