Click here to Skip to main content
15,889,867 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to drag and drop control within grid wpf with mouse ?

XML
<Window x:Class="Animation_Move.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" >
<Grid>
    <Grid Name="Grm" Width="500" Height="500" removed="#FF14831E">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="100"/>
            <ColumnDefinition Width="100"/>
            <ColumnDefinition Width="100"/>
            <ColumnDefinition Width="100"/>
            <ColumnDefinition Width="100"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="100"/>
            <RowDefinition Height="100"/>
            <RowDefinition Height="100"/>
            <RowDefinition Height="100"/>
            <RowDefinition Height="100"/>
        </Grid.RowDefinitions>
        <Image Name="Soldier" Grid.Row="1" Grid.Column="1" Source="Soldier-Red.png" Width="26" Height="34" ></Image>
    </Grid>

</Grid>

I need to shift control from the first row to the second row.Is this possible with mouse? i need to drag and drop image control.
Posted

1 solution

View the answer .thank a lot @Mediator

C#
Point _anchorPoint;
       Point _currentPoint;
       bool _isInDrag;
       private readonly TranslateTransform _transform = new TranslateTransform();

       private void root_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
       {
           var element = sender as FrameworkElement;
           _anchorPoint = e.GetPosition(null);
           if (element != null) element.CaptureMouse();
           _isInDrag = true;
           e.Handled = true;
       }

       private void root_MouseMove(object sender, MouseEventArgs e)
       {
           if (!_isInDrag) return;
           _currentPoint = e.GetPosition(null);

           _transform.X += _currentPoint.X - _anchorPoint.X;
           _transform.Y += (_currentPoint.Y - _anchorPoint.Y);
           RenderTransform = _transform;
           _anchorPoint = _currentPoint;
       }

       private void root_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
       {
           if (!_isInDrag) return;
           var element = sender as FrameworkElement;
           if (element != null) element.ReleaseMouseCapture();
           _isInDrag = false;
           e.Handled = true;
       }
 
Share this answer
 
Comments
Holger Boskugel 19-Dec-14 12:19pm    
I would use existing Drag&Drop functionality of WPF (http://msdn.microsoft.com/de-de/library/ms742859.aspx). For use of this span an empty label over the whole grid (Grid.ColumnSpan="5" Grid.RowSpan="5") while grid is not able to handle Drag&Drop.

<Label Grid.ColumnSpan="5" Grid.RowSpan="5"/>

Give the grid AllowDrop = True. On Drop event calculate position based on grid and place your image at the related field in grid.
Member 11983787 8-Feb-16 13:55pm    
Why I have an error 'Transform is not valid for Window'.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900