[英]How can I do Exchange data of two labels on drag and drop in C# WPF?
I want to do something like I have two Labels 我想做一些像我有两个标签的事情
A .............. ........... B A .............. ........... B.
______ ........... _______ ______ ........... _______
| | RED | RED | .......... | .......... | GREEN | 绿色|
---------- .......... ----------- ---------- .......... -----------
When I drag A on B OR B on A the text of both exchange 当我在两个交易所的文本拖动一个在B或B
A .............. ........... B A .............. ........... B.
______ ........... ... _____ ______ ............ _____
| | GREEN| GREEN | .......... | .......... | RED | RED |
---------- ............... --------- ---------- ............... ---------
I have done a little of it 我做了一点
main window 主窗口
When I drag and drop the text from the code comes on the drop label 当我拖放代码中的文本时,会在drop标签上显示
When I drag red on green: 当我在绿色上拖动红色时:
My Code: 我的代码:
private void Label_MouseDown(object sender, MouseButtonEventArgs e)
{
Label lblFrom = e.Source as Label;
if (e.LeftButton == MouseButtonState.Pressed)
DragDrop.DoDragDrop(lblFrom, lblFrom.Content, DragDropEffects.Copy);
}
private void Label_QueryContinueDrag(object sender, QueryContinueDragEventArgs e)
{
Label lblFrom = e.Source as Label;
if (!e.KeyStates.HasFlag(DragDropKeyStates.LeftMouseButton))
lblFrom.Content = "RED";
}
private void Label_Drop(object sender, DragEventArgs e)
{
string draggedText = (string)e.Data.GetData(DataFormats.StringFormat);
Label toLabel = e.Source as Label;
toLabel.Content = draggedText;
}
}
Here is how I achieved it. 这就是我实现它的方式。
Below is my XAML. 以下是我的XAML。
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Label Width="50" Height="50" Background="Red" Content="Red" HorizontalAlignment="Center" VerticalAlignment="Center" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" MouseDown="Label_MouseDown" Drop="Label_Drop" AllowDrop="True"/>
<Label Width="50" Height="50" Background="Green" Content="Green" HorizontalAlignment="Center" VerticalAlignment="Center" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Grid.Column="1" MouseDown="Label_MouseDown" Drop="Label_Drop" AllowDrop="True"/>
</Grid>
Below are my CodeBehind Events 以下是我的CodeBehind活动
Label DraggingLabel;
private void Label_MouseDown(object sender, MouseButtonEventArgs e)
{
DraggingLabel = sender as Label;
if (e.LeftButton == MouseButtonState.Pressed)
DragDrop.DoDragDrop(DraggingLabel, DraggingLabel.Content, DragDropEffects.Copy);
}
private void Label_Drop(object sender, DragEventArgs e)
{
Label originalsource = e.OriginalSource as Label;
Label lblToDrop = sender as Label;
string fromContent = lblToDrop.Content.ToString();
lblToDrop.Content = (string)e.Data.GetData(DataFormats.StringFormat);
DraggingLabel.Content = fromContent;
}
So basically i created a global Label DraggingLabel to use it at Label_Drop to interchange the Text. 所以基本上我创建了一个全局Label DraggingLabel来在Label_Drop中使用它来交换Text。
Final Output. 最终产出。
Good Luck. 祝好运。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.