none
Hit Test

    Question

  • Hey guys, I have 3 grids with textblocks inside them which the user can drag around on the screen...on two of the grids i have set an option for the user to click on a textblock and be able to change the integer value...i want the user to be able to drag one grid on top of another at any given location and once this happens i need a calculation to be performed... for example if i have a 6x6 grid with textblocks which have 1s inside of it and another 3x3 grid with 1s inside of it once one is dragged over the other i want the result to be placed in my thrid grid so if a 1 falls ontop of another 1 a 2 is placed in the third grid...thank you all

    Wednesday, October 06, 2010 4:53 PM

Answers

  • In your public Handle_MouseUp method, use the VisualTreeHelper to find out where the item is dropped,

    Point point = e.GetPosition(null);
    
    var uiElements = VisualTreeHelper.FindElementsInHostCoordinates(point, LayoutRoot);


    where point is the mouse position and LayoutRoot is the root element of the app or the currentpage.

    Once you have a list of UI Elements, loop through the list and see if its dropped on your grid.

     if (uiElements != null && uiElements.Count() > 0)
                    {
                        foreach (UIElement element in uiElements)
                        {
                            if (element is Grid && (element as Grid).Name == "Grid1")
                            {
                              //do something
                            }
                        }//end for
                    }//end if


    Thursday, October 07, 2010 5:57 AM
  • Add the following namespace directives,

    using System.Linq;
    using System.Xml.Linq;

    Don't forget to mark this as answer too..Cool
    Wednesday, October 20, 2010 2:42 AM

All replies

  • If you want to proceed with manual drag and drop implementation and not to use any of the drag and drop frameworks like the one from silverlight toolkit or Infragistics or ComponentOne, 

    You need to wire up the MouseLeftButtonDown, MouseMove events to manually track the dragging.

    For hit test, to know where the element is being dragged, or dropped, you need to use the VisualTreeHelper.

    More information on VisualTreeHelper,

    http://msdn.microsoft.com/en-us/library/system.windows.media.visualtreehelper(VS.95).aspx

    http://blogs.msdn.com/b/kmahone/archive/2009/03/29/visualtreehelper.aspx


    Thursday, October 07, 2010 4:13 AM
  • this is what i got going on so far...i am using only 2 grids here...i got the mousemove, mousedown and mouseup working, but i dont have the slightest clue on how to start using the treeview helper :(


    using System;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Ink;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;

    namespace SilverlightApplication1
    {
        public partial class MainPage : UserControl
        {
            public MainPage()
            {
                // Required to initialize variables
                InitializeComponent();
            }

            private void BTNsetGrid_Click(object sender, System.Windows.RoutedEventArgs e)
            {
                int UserValue;
                UserValue = Convert.ToInt32(TextSetGridSize.Text);

                var Grid1 = new Grid();
                var Grid2 = new Grid();

                //Create Grid1

                for (int i = 0; i < UserValue; i++)
                {
                    ColumnDefinition cd = new ColumnDefinition();
                    RowDefinition rd = new RowDefinition();
                    Grid1.ColumnDefinitions.Add(cd);
                    Grid1.RowDefinitions.Add(rd);
                    Grid1.Margin = new Thickness(50, 100, 0, 0);
                    Grid1.Background = new SolidColorBrush(Colors.Gray);
                    Grid1.MouseLeftButtonDown += new MouseButtonEventHandler(Handle_MouseDown);
                    Grid1.MouseMove += new MouseEventHandler(Handle_MouseMove);
                    Grid1.MouseLeftButtonUp += new MouseButtonEventHandler(Handle_MouseUp);
                    

                    //Create txt1

                    for (int j = 0; j < UserValue; j++)
                    {
                        TextBlock txt1 = new TextBlock();
                        txt1.Text = "0";
                        txt1.Height = 30;
                        txt1.Width = 30;
                        txt1.MouseLeftButtonDown += new MouseButtonEventHandler(b1_Click);
                        txt1.SetValue(Grid.RowProperty, i);
                        txt1.SetValue(Grid.ColumnProperty, j);
                        Grid1.Children.Add(txt1);
                    }
                }

                Canvas.Children.Add(Grid1);
                
                
                
                //Create Grid2
                for (int i = 0; i < UserValue; i++)
                {
                    ColumnDefinition cd = new ColumnDefinition();
                    RowDefinition rd = new RowDefinition();
                    Grid2.ColumnDefinitions.Add(cd);
                    Grid2.RowDefinitions.Add(rd);
                    Grid2.Background = new SolidColorBrush(Colors.Blue);
                    Grid2.Margin = new Thickness(800, 100, 0, 0);
                    Grid2.MouseLeftButtonDown += new MouseButtonEventHandler(Handle_MouseDown);
                    Grid2.MouseMove += new MouseEventHandler(Handle_MouseMove);
                    Grid2.MouseLeftButtonUp += new MouseButtonEventHandler(Handle_MouseUp);

                    //Create txt2

                    for (int j = 0; j < UserValue; j++)
                    {
                        TextBlock txt2 = new TextBlock();
                        txt2.Text = "0";
                        txt2.Width = 30;
                        txt2.Height = 30;

                        txt2.SetValue(Grid.RowProperty, i);
                        txt2.SetValue(Grid.ColumnProperty, j);
                        Grid2.Children.Add(txt2);
                    }

                }

                Canvas.Children.Add(Grid2);
                
            }
            
            //Setting Click on Grid1
            private void b1_Click(Object sender, RoutedEventArgs e)
            {
                TextBlock txt1 = sender as TextBlock;

                if (txt1 != null)
                {
                    if (txt1.Text.ToString() == "0")
                    {
                        txt1.Text = "1";
                    }
                    else
                    {
                        txt1.Text = "0";
                    }
                }
            }

            bool isMouseCaptured;
            double mouseVerticalPosition;
            double mouseHorizontalPosition;

            public void Handle_MouseDown(object sender, MouseEventArgs args)
            {
                Grid item = sender as Grid;
                mouseVerticalPosition = args.GetPosition(null).Y;
                mouseHorizontalPosition = args.GetPosition(null).X;
                isMouseCaptured = true;
                item.CaptureMouse();
                
            }

            public void Handle_MouseMove(object sender, MouseEventArgs args)
            {
                Grid item = sender as Grid;
                if (isMouseCaptured)
                {

                    // Calculate the current position of the object.
                    double deltaV = args.GetPosition(null).Y - mouseVerticalPosition;
                    double deltaH = args.GetPosition(null).X - mouseHorizontalPosition;
                    double newTop = deltaV + (double)item.GetValue(Canvas.TopProperty);
                    double newLeft = deltaH + (double)item.GetValue(Canvas.LeftProperty);

                    // Set new position of object.
                    item.SetValue(Canvas.TopProperty, newTop);
                    item.SetValue(Canvas.LeftProperty, newLeft);

                    // Update position global variables.
                    mouseVerticalPosition = args.GetPosition(null).Y;
                    mouseHorizontalPosition = args.GetPosition(null).X;
                }
            }

            public void Handle_MouseUp(object sender, MouseEventArgs args)
            {
                Grid item = sender as Grid;
                isMouseCaptured = false;
                item.ReleaseMouseCapture();
                mouseVerticalPosition = -1;
                mouseHorizontalPosition = -1;
            }


        }
    }

    Thursday, October 07, 2010 5:35 AM
  • oops

    Thursday, October 07, 2010 5:35 AM
  • In your public Handle_MouseUp method, use the VisualTreeHelper to find out where the item is dropped,

    Point point = e.GetPosition(null);
    
    var uiElements = VisualTreeHelper.FindElementsInHostCoordinates(point, LayoutRoot);


    where point is the mouse position and LayoutRoot is the root element of the app or the currentpage.

    Once you have a list of UI Elements, loop through the list and see if its dropped on your grid.

     if (uiElements != null && uiElements.Count() > 0)
                    {
                        foreach (UIElement element in uiElements)
                        {
                            if (element is Grid && (element as Grid).Name == "Grid1")
                            {
                              //do something
                            }
                        }//end for
                    }//end if


    Thursday, October 07, 2010 5:57 AM
  • thank you for your post...for some reason 

    uiElements.Count is giving me errors
    Wednesday, October 13, 2010 1:31 PM
  • What error? Is the Collection null or empty?

    Thursday, October 14, 2010 3:11 AM
  • just cant find the count part...

    Saturday, October 16, 2010 3:19 PM
  • Here is my code...i know i am an idiot i am sorry for that...uielements.count is getting underlined


    using System;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Ink;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;

    namespace SilverlightApplication1
    {
        public partial class MainPage : UserControl
        {
            public MainPage()
            {
                // Required to initialize variables
                InitializeComponent();
            }

            private void BTNsetGrid_Click(object sender, System.Windows.RoutedEventArgs e)
            {
                int UserValue;
                UserValue = Convert.ToInt32(TextSetGridSize.Text);
                

                var Grid1 = new Grid();
                var Grid2 = new Grid();

                //Create Grid1

                for (int i = 0; i < UserValue; i++)
                {
                    ColumnDefinition cd = new ColumnDefinition();
                    RowDefinition rd = new RowDefinition();
                    Grid1.ColumnDefinitions.Add(cd);
                    Grid1.RowDefinitions.Add(rd);
                    Grid1.Margin = new Thickness(50, 100, 0, 0);
                    Grid1.Background = new SolidColorBrush(Colors.Gray);
                    

                    Grid1.MouseLeftButtonDown += new MouseButtonEventHandler(Handle_MouseDown);
                    Grid1.MouseMove += new MouseEventHandler(Handle_MouseMove);
                    Grid1.MouseLeftButtonUp += new MouseButtonEventHandler(Handle_MouseUp);
                    

                    //Create txt1

                    for (int j = 0; j < UserValue; j++)
                    {
                        TextBlock txt1 = new TextBlock();
                        txt1.Text = "0";
                        txt1.TextAlignment = TextAlignment.Center;
                        txt1.Height = 30;
                        txt1.Width = 30;
                        txt1.MouseLeftButtonDown += new MouseButtonEventHandler(b1_Click);
                        txt1.SetValue(Grid.RowProperty, i);
                        txt1.SetValue(Grid.ColumnProperty, j);
                        Grid1.Children.Add(txt1);
                    }
                }

                Canvas.Children.Add(Grid1);
            
        


                
                
                
                //Create Grid2
                for (int i = 0; i < UserValue; i++)
                {
                    ColumnDefinition cd = new ColumnDefinition();
                    RowDefinition rd = new RowDefinition();
                    Grid2.ColumnDefinitions.Add(cd);
                    Grid2.RowDefinitions.Add(rd);
                    Grid2.Background = new SolidColorBrush(Colors.Blue);
                    Grid2.Margin = new Thickness(800, 100, 0, 0);
                    Grid2.MouseLeftButtonDown += new MouseButtonEventHandler(Handle_MouseDown);
                    Grid2.MouseMove += new MouseEventHandler(Handle_MouseMove);
                    Grid2.MouseLeftButtonUp += new MouseButtonEventHandler(Handle_MouseUp);

                    //Create txt2

                    for (int j = 0; j < UserValue; j++)
                    {
                        TextBlock txt2 = new TextBlock();
                        txt2.Text = "0";
                        txt2.Width = 30;
                        txt2.Height = 30;
                        txt2.TextAlignment = TextAlignment.Center;
                        txt2.SetValue(Grid.RowProperty, i);
                        txt2.SetValue(Grid.ColumnProperty, j);
                        Grid2.Children.Add(txt2);
                    }

                }

                Canvas.Children.Add(Grid2);
                
            }
            
            //Setting Click on Grid1
            private void b1_Click(Object sender, RoutedEventArgs e)
            {
                TextBlock txt1 = sender as TextBlock;

                if (txt1 != null)
                {
                    if (txt1.Text.ToString() == "0")
                    {
                        txt1.Text = "1";
                    }
                    else
                    {
                        txt1.Text = "0";
                    }
                }
            }

            bool isMouseCaptured;
            double mouseVerticalPosition;
            double mouseHorizontalPosition;

            public void Handle_MouseDown(object sender, MouseEventArgs args)
            {
                Grid item = sender as Grid;
                mouseVerticalPosition = args.GetPosition(null).Y;
                mouseHorizontalPosition = args.GetPosition(null).X;
                isMouseCaptured = true;
                item.CaptureMouse();
                
            }

            public void Handle_MouseMove(object sender, MouseEventArgs args)
            {
                Grid item = sender as Grid;
                if (isMouseCaptured)
                {

                    // Calculate the current position of the object.
                    double deltaV = args.GetPosition(null).Y - mouseVerticalPosition;
                    double deltaH = args.GetPosition(null).X - mouseHorizontalPosition;
                    double newTop = deltaV + (double)item.GetValue(Canvas.TopProperty);
                    double newLeft = deltaH + (double)item.GetValue(Canvas.LeftProperty);

                    // Set new position of object.
                    item.SetValue(Canvas.TopProperty, newTop);
                    item.SetValue(Canvas.LeftProperty, newLeft);

                    // Update position global variables.
                    mouseVerticalPosition = args.GetPosition(null).Y;
                    mouseHorizontalPosition = args.GetPosition(null).X;
                }
            }

            public void Handle_MouseUp(object sender, MouseEventArgs args)
            {
                Grid item = sender as Grid;
                isMouseCaptured = false;
                item.ReleaseMouseCapture();
                mouseVerticalPosition = -1;
                mouseHorizontalPosition = -1;

                Point point = args.GetPosition(null);

                var uiElements = VisualTreeHelper.FindElementsInHostCoordinates(point, Canvas);

                if (uiElements != null && uiElements.Count > 0)
                {
                    foreach (UIElement element in uiElements)
                    {
                        if (element is Grid && (element as Grid).Name == "Grid1")
                        {
                            //left blank on purpose for now
                        }
                    }
                }
            }




        }
    }

    Sunday, October 17, 2010 10:06 AM
  • Hi, here the 'Count' is not a property. It's a method.

    In case of var which is a IEnumerable the Count is available as a Method, for List types Count is available as a property.

    So instead of writing 'uiElements.Count' use 'uiElements.Count()'.


    Tuesday, October 19, 2010 3:38 AM
  • thanks for your reply, you are a good person for trying to help me out, much appricated...but still getting the same error both ways :(

    here is the code...

    using System;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Ink;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;

    namespace SilverlightApplication1
    {
        public partial class MainPage : UserControl
        {
            public MainPage()
            {
                // Required to initialize variables
                InitializeComponent();
            }

            private void BTNsetGrid_Click(object sender, System.Windows.RoutedEventArgs e)
            {
                int UserValue;
                UserValue = Convert.ToInt32(TextSetGridSize.Text);
                

                var Grid1 = new Grid();
                var Grid2 = new Grid();

                //Create Grid1

                for (int i = 0; i < UserValue; i++)
                {
                    ColumnDefinition cd = new ColumnDefinition();
                    RowDefinition rd = new RowDefinition();
                    Grid1.ColumnDefinitions.Add(cd);
                    Grid1.RowDefinitions.Add(rd);
                    Grid1.Margin = new Thickness(400, 100, 0, 0);
                    Grid1.Background = new SolidColorBrush(Colors.Gray);


    //                Grid1.MouseLeftButtonDown += new MouseButtonEventHandler(Handle_MouseDown);
    //                Grid1.MouseMove += new MouseEventHandler(Handle_MouseMove);
    //                Grid1.MouseLeftButtonUp += new MouseButtonEventHandler(Handle_MouseUp);
                    

                    //Create txt1

                    for (int j = 0; j < UserValue; j++)
                    {
                        TextBlock txt1 = new TextBlock();
                        txt1.Text = "0";
                        txt1.TextAlignment = TextAlignment.Center;
                        txt1.Height = 30;
                        txt1.Width = 30;
                        txt1.MouseLeftButtonDown += new MouseButtonEventHandler(b1_Click);
                        txt1.SetValue(Grid.RowProperty, i);
                        txt1.SetValue(Grid.ColumnProperty, j);
                        Grid1.Children.Add(txt1);
                    }
                }

                Canvas.Children.Add(Grid1);
            
        


                
                
                
                //Create Grid2
                for (int i = 0; i < UserValue; i++)
                {
                    ColumnDefinition cd = new ColumnDefinition();
                    RowDefinition rd = new RowDefinition();
                    Grid2.ColumnDefinitions.Add(cd);
                    Grid2.RowDefinitions.Add(rd);
                    Grid2.Background = new SolidColorBrush(Colors.Blue);
                    Grid2.Margin = new Thickness(800, 100, 0, 0);
                    Grid2.MouseLeftButtonDown += new MouseButtonEventHandler(Handle_MouseDown);
                    Grid2.MouseMove += new MouseEventHandler(Handle_MouseMove);
                    Grid2.MouseLeftButtonUp += new MouseButtonEventHandler(Handle_MouseUp);

                    //Create txt2

                    for (int j = 0; j < UserValue; j++)
                    {
                        TextBlock txt2 = new TextBlock();
                        txt2.Text = "0";
                        txt2.Width = 30;
                        txt2.Height = 30;
                        txt2.TextAlignment = TextAlignment.Center;
                        txt2.SetValue(Grid.RowProperty, i);
                        txt2.SetValue(Grid.ColumnProperty, j);
                        Grid2.Children.Add(txt2);
                    }

                }

                Canvas.Children.Add(Grid2);
                
            }
            
            //Setting Click on Grid1
            private void b1_Click(Object sender, RoutedEventArgs e)
            {
                TextBlock txt1 = sender as TextBlock;

                if (txt1 != null)
                {
                    if (txt1.Text.ToString() == "0")
                    {
                        txt1.Text = "1";
                    }
                    else
                    {
                        txt1.Text = "0";
                    }
                }
            }

            bool isMouseCaptured;
            double mouseVerticalPosition;
            double mouseHorizontalPosition;

            public void Handle_MouseDown(object sender, MouseEventArgs args)
            {
                Grid item = sender as Grid;
                mouseVerticalPosition = args.GetPosition(null).Y;
                mouseHorizontalPosition = args.GetPosition(null).X;
                isMouseCaptured = true;
                item.CaptureMouse();
                
            }

            public void Handle_MouseMove(object sender, MouseEventArgs args)
            {
                Grid item = sender as Grid;
                if (isMouseCaptured)
                {

                    // Calculate the current position of the object.
                    double deltaV = args.GetPosition(null).Y - mouseVerticalPosition;
                    double deltaH = args.GetPosition(null).X - mouseHorizontalPosition;
                    double newTop = deltaV + (double)item.GetValue(Canvas.TopProperty);
                    double newLeft = deltaH + (double)item.GetValue(Canvas.LeftProperty);

                    // Set new position of object.
                    item.SetValue(Canvas.TopProperty, newTop);
                    item.SetValue(Canvas.LeftProperty, newLeft);

                    // Update position global variables.
                    mouseVerticalPosition = args.GetPosition(null).Y;
                    mouseHorizontalPosition = args.GetPosition(null).X;
                }
            }

            public void Handle_MouseUp(object sender, MouseEventArgs args)
            {
                Grid item = sender as Grid;
                isMouseCaptured = false;
                item.ReleaseMouseCapture();
                mouseVerticalPosition = -1;
                mouseHorizontalPosition = -1;
                
                Point point = args.GetPosition(null);

                var uiElements = VisualTreeHelper.FindElementsInHostCoordinates(point, Canvas);
                
                if (uiElements != null && uiElements.Count() > 0)
                    {
                        foreach (UIElement element in uiElements)
                        {
                            if (element is Grid && (element as Grid).Name == "Grid1")
                            {
                              //left blank on purpose
                            }
                        }
                    }
                
                
            }

        }
    }


    here is the error...

    System.Collections.Generic.IEnumerable<System.Windows.UIElement>

    does not contain a definiton for Count and no extension method Count accepting a first argument of type

    System.Collection.Generic.IEnumerable<System.Windows.UIElement> could be found (are you missing a using directive or an assembly reference?)

    Tuesday, October 19, 2010 1:49 PM
  • Add the following namespace directives,

    using System.Linq;
    using System.Xml.Linq;

    Don't forget to mark this as answer too..Cool
    Wednesday, October 20, 2010 2:42 AM
  • thanks again you have been a great help...any reason why nothing happens on the hit of the two grids with this code...just testing now this isnt what i want to happen but keeping it simple until i get it to work the way i like...

    using System;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Ink;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using System.Linq;
    using System.Xml.Linq;

    namespace SilverlightApplication1
    {
        public partial class MainPage : UserControl
        {
            public MainPage()
            {
                // Required to initialize variables
                InitializeComponent();
            }

            private void BTNsetGrid_Click(object sender, System.Windows.RoutedEventArgs e)
            {
                int UserValue;
                UserValue = Convert.ToInt32(TextSetGridSize.Text);
                

                var Grid1 = new Grid();
                var Grid2 = new Grid();

                //Create Grid1

                for (int i = 0; i < UserValue; i++)
                {
                    ColumnDefinition cd = new ColumnDefinition();
                    RowDefinition rd = new RowDefinition();
                    Grid1.ColumnDefinitions.Add(cd);
                    Grid1.RowDefinitions.Add(rd);
                    Grid1.Margin = new Thickness(400, 100, 0, 0);
                    Grid1.Background = new SolidColorBrush(Colors.Gray);


    //                Grid1.MouseLeftButtonDown += new MouseButtonEventHandler(Handle_MouseDown);
    //                Grid1.MouseMove += new MouseEventHandler(Handle_MouseMove);
    //                Grid1.MouseLeftButtonUp += new MouseButtonEventHandler(Handle_MouseUp);
                    

                    //Create txt1

                    for (int j = 0; j < UserValue; j++)
                    {
                        TextBlock txt1 = new TextBlock();
                        txt1.Text = "0";
                        txt1.TextAlignment = TextAlignment.Center;
                        txt1.Height = 30;
                        txt1.Width = 30;
                        txt1.MouseLeftButtonDown += new MouseButtonEventHandler(b1_Click);
                        txt1.SetValue(Grid.RowProperty, i);
                        txt1.SetValue(Grid.ColumnProperty, j);
                        Grid1.Children.Add(txt1);
                    }
                }

                Canvas.Children.Add(Grid1);
            
        


                
                
                
                //Create Grid2
                for (int i = 0; i < UserValue; i++)
                {
                    ColumnDefinition cd = new ColumnDefinition();
                    RowDefinition rd = new RowDefinition();
                    Grid2.ColumnDefinitions.Add(cd);
                    Grid2.RowDefinitions.Add(rd);
                    Grid2.Background = new SolidColorBrush(Colors.Blue);
                    Grid2.Margin = new Thickness(800, 100, 0, 0);
                    Grid2.MouseLeftButtonDown += new MouseButtonEventHandler(Handle_MouseDown);
                    Grid2.MouseMove += new MouseEventHandler(Handle_MouseMove);
                    Grid2.MouseLeftButtonUp += new MouseButtonEventHandler(Handle_MouseUp);

                    //Create txt2

                    for (int j = 0; j < UserValue; j++)
                    {
                        TextBlock txt2 = new TextBlock();
                        txt2.Text = "0";
                        txt2.Width = 30;
                        txt2.Height = 30;
                        txt2.TextAlignment = TextAlignment.Center;
                        txt2.SetValue(Grid.RowProperty, i);
                        txt2.SetValue(Grid.ColumnProperty, j);
                        Grid2.Children.Add(txt2);
                    }

                }

                Canvas.Children.Add(Grid2);
                
            }
            
            //Setting Click on Grid1
            private void b1_Click(Object sender, RoutedEventArgs e)
            {
                TextBlock txt1 = sender as TextBlock;

                if (txt1 != null)
                {
                    if (txt1.Text.ToString() == "0")
                    {
                        txt1.Text = "1";
                    }
                    else
                    {
                        txt1.Text = "0";
                    }
                }
            }

            bool isMouseCaptured;
            double mouseVerticalPosition;
            double mouseHorizontalPosition;

            public void Handle_MouseDown(object sender, MouseEventArgs args)
            {
                Grid item = sender as Grid;
                mouseVerticalPosition = args.GetPosition(null).Y;
                mouseHorizontalPosition = args.GetPosition(null).X;
                isMouseCaptured = true;
                item.CaptureMouse();
                
            }

            public void Handle_MouseMove(object sender, MouseEventArgs args)
            {
                Grid item = sender as Grid;
                if (isMouseCaptured)
                {

                    // Calculate the current position of the object.
                    double deltaV = args.GetPosition(null).Y - mouseVerticalPosition;
                    double deltaH = args.GetPosition(null).X - mouseHorizontalPosition;
                    double newTop = deltaV + (double)item.GetValue(Canvas.TopProperty);
                    double newLeft = deltaH + (double)item.GetValue(Canvas.LeftProperty);

                    // Set new position of object.
                    item.SetValue(Canvas.TopProperty, newTop);
                    item.SetValue(Canvas.LeftProperty, newLeft);

                    // Update position global variables.
                    mouseVerticalPosition = args.GetPosition(null).Y;
                    mouseHorizontalPosition = args.GetPosition(null).X;
                }
            }

            public void Handle_MouseUp(object sender, MouseEventArgs args)
            {
                Grid item = sender as Grid;
                isMouseCaptured = false;
                item.ReleaseMouseCapture();
                mouseVerticalPosition = -1;
                mouseHorizontalPosition = -1;
                
                Point point = args.GetPosition(null);

                var uiElements = VisualTreeHelper.FindElementsInHostCoordinates(point, Canvas);
                
                if (uiElements != null && uiElements.Count() > 0)
                    {
                        foreach (UIElement element in uiElements)
                        {
                            if (element is Grid && (element as Grid).Name == "Grid1")
                            {
                              test.Text= "1";
                            }
                        }
                    }
                
                
            }


    //        private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
    //        {
    //            Canvas.Children.RemoveAt();
    //        }


        }
    }

    Wednesday, October 20, 2010 6:08 AM
  • Sorry, I couldn't understand. Can you explain what's happening and what you need?

    Thursday, October 21, 2010 6:46 AM
  • thanks again for your reply...the end result i am after is having 3 grids on the canvas...grid1, grid2, grid3....grid1 has numbers in it...and grid 2 will have numbers in it...when grid 3 is dragged and released ontop of a part in grid1 i want grid2 to update with the result of the calculation of the two numbers..

    Thursday, October 21, 2010 6:59 AM
  • ok its working thanks a bunch...is there any way now of targeting if a specific cell within the grid was landed on?

    Tuesday, October 26, 2010 4:35 PM
  • Not directly, but a workaround approach is to have a transparent rectangle or rectangle with the same background color as grid. And identify on which rectangle the object is dropped.

    Wednesday, October 27, 2010 2:12 AM
  • i have set it up so each cell has its own grid...if you can point me in the right direction in order to get this hit test working for each cell...heres the code...

    using System;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Ink;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using System.Linq;
    using System.Xml.Linq;
    using System.Windows.Media.Effects;


    namespace SilverlightApplication1
    {
        public partial class MainPage : UserControl
        {
            public MainPage()
            {
                // Required to initialize variables
                InitializeComponent();
            }
            private void BTNsetGrid_Click(object sender, System.Windows.RoutedEventArgs e)
            {
                int UserValueRow, UserValueCol;
                UserValueRow = Convert.ToInt32(txtRow.Text);
                UserValueCol = Convert.ToInt32(txtCol.Text);
                if ((UserValueRow <= 10) && (UserValueCol <= 10))
                {
                    Grid Grid1 = new Grid();
                    Grid Grid2 = new Grid();
                    for (int i = 0; i < UserValueRow; i++)
                    {
                        Grid1.Margin = new Thickness(50, 100, 0, 0);                    
                        Grid1.Background = new SolidColorBrush(Colors.Gray);
                        RowDefinition rd = new RowDefinition();
                        Grid1.RowDefinitions.Add(rd);
                        for (int j = 0; j < UserValueCol; j++)
                        {
                            ColumnDefinition cd = new ColumnDefinition();
                            Grid1.ColumnDefinitions.Add(cd);                        
                            TextBlock txt1 = new TextBlock();
                            txt1.Text = "0";
                            txt1.TextAlignment = TextAlignment.Center;
                            txt1.Height = 30;
                            txt1.Width = 30;                        
                            txt1.MouseLeftButtonDown += new MouseButtonEventHandler(b1_Click);
                            Grid g = new Grid();
                            g.Children.Add(txt1);
                            g.MouseLeftButtonDown += new MouseButtonEventHandler(b2_Click);
                            
                            
                            g.SetValue(Grid.RowProperty, i);
                            g.SetValue(Grid.ColumnProperty, j);
                            Grid1.Children.Add(g);
                        }
                    }
                    Canvas.Children.Add(Grid1);
                    for (int i = 0; i < UserValueRow; i++)
                    {
                        Grid2.Background = new SolidColorBrush(Colors.DarkGray);
                        Grid2.Margin = new Thickness(650, 100, 0, 0);
                        RowDefinition rd = new RowDefinition();
                        Grid2.RowDefinitions.Add(rd);
                        for (int j = 0; j < UserValueCol; j++)
                        {
                            ColumnDefinition cd = new ColumnDefinition();
                            Grid2.ColumnDefinitions.Add(cd);
                            TextBlock txt1 = new TextBlock();
                            txt1.Text = "0";
                            txt1.TextAlignment = TextAlignment.Center;
                            txt1.Height = 30;
                            txt1.Width = 30;
                            txt1.SetValue(Grid.RowProperty, i);
                            txt1.SetValue(Grid.ColumnProperty, j);
                            Grid2.Children.Add(txt1);
                        }
                    }
                    Canvas.Children.Add(Grid2);
                }
                else
                {
                    MessageBox.Show("Values must not be greather than 10");
                }
            }
            private void btnSetObj(object sender, System.Windows.RoutedEventArgs e)
            {
                int UserValueObjRow;
                int UserValueObjCol;
                UserValueObjRow = Convert.ToInt32(txtObjRow.Text);
                UserValueObjCol = Convert.ToInt32(txtObjCol.Text);
                if ((UserValueObjRow <= 10) && (UserValueObjCol <= 10))
                {
                    Grid GridObj = new Grid();
                    //Create Object Grid
                    for (int i = 0; i < UserValueObjRow; i++)
                    {
                        GridObj.Margin = new Thickness(450, 300, 0, 0);
                        GridObj.Background = new SolidColorBrush(Colors.Yellow);
                        RowDefinition rd = new RowDefinition();
                        GridObj.RowDefinitions.Add(rd);
                        GridObj.MouseLeftButtonDown += new MouseButtonEventHandler(Handle_MouseDown);
                        GridObj.MouseMove += new MouseEventHandler(Handle_MouseMove);
                        GridObj.MouseLeftButtonUp += new MouseButtonEventHandler(Handle_MouseUp);
                        for (int j = 0; j < UserValueObjCol; j++)
                        {
                            ColumnDefinition cd = new ColumnDefinition();
                            GridObj.ColumnDefinitions.Add(cd);
                            TextBlock txt1 = new TextBlock();
                            txt1.Text = "0";
                            txt1.TextAlignment = TextAlignment.Center;
                            txt1.Height = 30;
                            txt1.Width = 30;
                            txt1.MouseLeftButtonDown += new MouseButtonEventHandler(b1_Click);
                            Grid g2 = new Grid();
                            g2.Children.Add(txt1);
                            g2.MouseLeftButtonDown += new MouseButtonEventHandler(b4_Click);
                            g2.SetValue(Grid.RowProperty, i);
                            g2.SetValue(Grid.ColumnProperty, j);
                            GridObj.Children.Add(g2);
                        }
                    }
                    Canvas.Children.Add(GridObj);
                }
                else
                {
                    MessageBox.Show("Values must not be greater than 10");
                }
            }        
            //Setting Click on Grid    
            private void b1_Click(Object sender, RoutedEventArgs e)
            {
                TextBlock txt1 = sender as TextBlock;
                
                
                                
                if (txt1 != null)
                {
                        if (txt1.Text.ToString() == "0")
                        {
                            txt1.Text = "1";
                            
                        }
                        else
                        {
                            txt1.Text = "0";
                            
                        }
                    
                }
            }
            
            private void b2_Click(object sender, RoutedEventArgs e)
            {            
                Grid g = sender as Grid;
                Grid g2 = sender as Grid;
                if (g != null)
                {
                    g.Background = new SolidColorBrush(Colors.White);
                    g.MouseLeftButtonDown += new MouseButtonEventHandler(b3_Click);
                }
            }
            
            private void b3_Click(object sender, RoutedEventArgs e)
            {
                Grid g = sender as Grid;

                if (g != null)
                {
                    g.Background = new SolidColorBrush(Colors.Gray);
                    g.MouseLeftButtonDown += new MouseButtonEventHandler(b2_Click);
                }

            }
            private void b4_Click(object sender, RoutedEventArgs e)
            {
                Grid g2 = sender as Grid;
                if (g2 != null)
                {
                    g2.Background = new SolidColorBrush(Colors.White);
                    g2.MouseLeftButtonDown += new MouseButtonEventHandler(b5_Click);
                }
            }
            private void b5_Click(object sender, RoutedEventArgs e)
            {
                Grid g2 = sender as Grid;
                if (g2 != null)
                {
                    g2.Background = new SolidColorBrush(Colors.Yellow);
                    g2.MouseLeftButtonDown += new MouseButtonEventHandler(b4_Click);
                }
            }

            bool isMouseCaptured;
            double mouseVerticalPosition;
            double mouseHorizontalPosition;
            public void Handle_MouseDown (Object sender,MouseButtonEventArgs e)
            {
                Grid item = sender as Grid;
                mouseVerticalPosition = e.GetPosition(null).Y;
                mouseHorizontalPosition = e.GetPosition(null).X;
                isMouseCaptured = true;
                item.CaptureMouse();
            }
            public void Handle_MouseMove(Object sender,MouseEventArgs e)
            {
                Grid item = sender as Grid;
                if (isMouseCaptured)
                {
                    // Calculate the current position of the object.
                    double deltaV = e.GetPosition(null).Y - mouseVerticalPosition;
                    double deltaH = e.GetPosition(null).X - mouseHorizontalPosition;
                    double newTop = deltaV + (double)item.GetValue(Canvas.TopProperty);
                    double newLeft = deltaH + (double)item.GetValue(Canvas.LeftProperty);
                    // Set new position of object.
                    item.SetValue(Canvas.TopProperty, newTop);
                    item.SetValue(Canvas.LeftProperty, newLeft);
                    // Update position global variables.
                    mouseVerticalPosition = e.GetPosition(null).Y;
                    mouseHorizontalPosition = e.GetPosition(null).X;
                }
            }
            public void Handle_MouseUp (Object sender,MouseButtonEventArgs e)
            {
                Grid item = sender as Grid;
                isMouseCaptured = false;
                item.ReleaseMouseCapture();
                mouseVerticalPosition = -1;
                mouseHorizontalPosition = -1;
            }


        }
    }

    Tuesday, November 16, 2010 7:56 AM