简体   繁体   中英

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
______ ........... _______
| RED | .......... | GREEN |
---------- .......... -----------

When I drag A on B OR B on A the text of both exchange

A .............. ........... B
______ ........... ... _____
| GREEN| .......... | 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

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.

<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

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.

Final Output.

在此输入图像描述

Good Luck.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM