Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. drag image

drag image

Scheduled Pinned Locked Moved WPF
csharpcsswpf
13 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R RongNK

    In window form i can create 1 image and drag it from 1 place to another by: Image_MouseDown, Image_MouseMove and Image_MouseUp but in WPF i make 1 grid(create 5 row and 5 column) and i add child (image) to row 2 and column 2 then 1 make same events but i can't drag it. please make a solution . Thank.Sorry for my english !

    C Offline
    C Offline
    Christian Graus
    wrote on last edited by
    #2

    It works the same way, the only thing is, the way that things are laid out in WPF is a little different. What have you tried ?

    Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

    R 1 Reply Last reply
    0
    • C Christian Graus

      It works the same way, the only thing is, the way that things are laid out in WPF is a little different. What have you tried ?

      Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

      R Offline
      R Offline
      RongNK
      wrote on last edited by
      #3

      The grid have 5 ColumnDefinitions and 5 RowDefinitions. If image i can get current Column and Row by GetValue(Grid.RowProperty) and GetValue(Grid.ColumnProperty) . But i don't know how to get current Column and Row of Mouse. If know i can SetValue. Thank !

      C 1 Reply Last reply
      0
      • R RongNK

        The grid have 5 ColumnDefinitions and 5 RowDefinitions. If image i can get current Column and Row by GetValue(Grid.RowProperty) and GetValue(Grid.ColumnProperty) . But i don't know how to get current Column and Row of Mouse. If know i can SetValue. Thank !

        C Offline
        C Offline
        Christian Graus
        wrote on last edited by
        #4

        Oh, you want to know what column and row the mouse is over ? I'd imagine you'd need to turn the rows and columns into pixel positions to calculate that, but then, if you caught the mouse move event in each grid square, you could just track which square control got the event last.....

        Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

        R 1 Reply Last reply
        0
        • C Christian Graus

          Oh, you want to know what column and row the mouse is over ? I'd imagine you'd need to turn the rows and columns into pixel positions to calculate that, but then, if you caught the mouse move event in each grid square, you could just track which square control got the event last.....

          Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

          R Offline
          R Offline
          RongNK
          wrote on last edited by
          #5

          Can you show me how to "caught the mouse move event in each grid square". Thank :-O

          M 1 Reply Last reply
          0
          • R RongNK

            Can you show me how to "caught the mouse move event in each grid square". Thank :-O

            M Offline
            M Offline
            Mark Salsbery
            wrote on last edited by
            #6

            The grid doesn't really have grid squares. It has column/row definitions that define the grid squares. These definitions are used to layout elements "in" the grid squares. So put elements in the grid squares, and subscribe to events on those elements.

            Mark Salsbery Microsoft MVP - Visual C++ :java:

            C 1 Reply Last reply
            0
            • M Mark Salsbery

              The grid doesn't really have grid squares. It has column/row definitions that define the grid squares. These definitions are used to layout elements "in" the grid squares. So put elements in the grid squares, and subscribe to events on those elements.

              Mark Salsbery Microsoft MVP - Visual C++ :java:

              C Offline
              C Offline
              Christian Graus
              wrote on last edited by
              #7

              Thanks for explaining that a bit better than I did :-)

              Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

              M 1 Reply Last reply
              0
              • C Christian Graus

                Thanks for explaining that a bit better than I did :-)

                Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

                M Offline
                M Offline
                Mark Salsbery
                wrote on last edited by
                #8

                I've got your back. Gives you more time to focus on NullReferenceException issues. ;P

                Mark Salsbery Microsoft MVP - Visual C++ :java:

                R 1 Reply Last reply
                0
                • M Mark Salsbery

                  I've got your back. Gives you more time to focus on NullReferenceException issues. ;P

                  Mark Salsbery Microsoft MVP - Visual C++ :java:

                  R Offline
                  R Offline
                  RongNK
                  wrote on last edited by
                  #9

                  I have a grid gr and divided it into 5 row, 5 column and placed a Image in it

                  Image tem = new Image();
                  tem.Source = new BitmapImage(new Uri("..."));
                  gr.Children.Add(tem);
                  tem.SetValue(Grid.ColumnProperty,0);
                  tem.SetValue(Grid.RowProperty,0);

                  tem.MouseLeftButtonDown += new MouseButtonEventHandler(tem_MouseLeftButtonDown);
                  tem.MouseLeftButtonUp+= new MouseButtonEventHandler(tem_MouseLeftButtonUp);
                  tem.MouseMove += new MouseButtonEventHandler(tem_MouseMove);

                  Now, i want to make tem_MouseLeftButtonDown, tem_MouseLeftButtonUp, tem_MouseMove to move Image from (row=i,column=j) to (row=Mouse point,column=Mouse point), in window form i can but in wpf i can't. Sorry for my english. Thank :-O

                  M 2 Replies Last reply
                  0
                  • R RongNK

                    I have a grid gr and divided it into 5 row, 5 column and placed a Image in it

                    Image tem = new Image();
                    tem.Source = new BitmapImage(new Uri("..."));
                    gr.Children.Add(tem);
                    tem.SetValue(Grid.ColumnProperty,0);
                    tem.SetValue(Grid.RowProperty,0);

                    tem.MouseLeftButtonDown += new MouseButtonEventHandler(tem_MouseLeftButtonDown);
                    tem.MouseLeftButtonUp+= new MouseButtonEventHandler(tem_MouseLeftButtonUp);
                    tem.MouseMove += new MouseButtonEventHandler(tem_MouseMove);

                    Now, i want to make tem_MouseLeftButtonDown, tem_MouseLeftButtonUp, tem_MouseMove to move Image from (row=i,column=j) to (row=Mouse point,column=Mouse point), in window form i can but in wpf i can't. Sorry for my english. Thank :-O

                    M Offline
                    M Offline
                    Mark Salsbery
                    wrote on last edited by
                    #10

                    The Grid control, being a Panel element, is suited for layout of child elements. For what you are doing, it would be much simpler to use another simpler panel like a Canvas and keep track of the cell coordinates yourself. That's just my opinion. You could do it with a Grid, but when I picture the code in my head, it looks way messier than it would be using a canvas.

                    Mark Salsbery Microsoft MVP - Visual C++ :java:

                    C 1 Reply Last reply
                    0
                    • M Mark Salsbery

                      The Grid control, being a Panel element, is suited for layout of child elements. For what you are doing, it would be much simpler to use another simpler panel like a Canvas and keep track of the cell coordinates yourself. That's just my opinion. You could do it with a Grid, but when I picture the code in my head, it looks way messier than it would be using a canvas.

                      Mark Salsbery Microsoft MVP - Visual C++ :java:

                      C Offline
                      C Offline
                      Christian Graus
                      wrote on last edited by
                      #11

                      Yes, I was thinking that, too. I am assuming he wants the image to jump between cells, not move smoothly, but even then, it makes more sense to track positions than use an actual grid, IMO.

                      Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

                      M 1 Reply Last reply
                      0
                      • C Christian Graus

                        Yes, I was thinking that, too. I am assuming he wants the image to jump between cells, not move smoothly, but even then, it makes more sense to track positions than use an actual grid, IMO.

                        Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

                        M Offline
                        M Offline
                        Mark Salsbery
                        wrote on last edited by
                        #12

                        Agreed. I was thinking mostly of the hit-testing being messy, but looking again, I see that WPF drag and drop could be used with drop targets in each cell to let the framework do a lot of that. That may even be simpler than using a canvas :)

                        Mark Salsbery Microsoft MVP - Visual C++ :java:

                        1 Reply Last reply
                        0
                        • R RongNK

                          I have a grid gr and divided it into 5 row, 5 column and placed a Image in it

                          Image tem = new Image();
                          tem.Source = new BitmapImage(new Uri("..."));
                          gr.Children.Add(tem);
                          tem.SetValue(Grid.ColumnProperty,0);
                          tem.SetValue(Grid.RowProperty,0);

                          tem.MouseLeftButtonDown += new MouseButtonEventHandler(tem_MouseLeftButtonDown);
                          tem.MouseLeftButtonUp+= new MouseButtonEventHandler(tem_MouseLeftButtonUp);
                          tem.MouseMove += new MouseButtonEventHandler(tem_MouseMove);

                          Now, i want to make tem_MouseLeftButtonDown, tem_MouseLeftButtonUp, tem_MouseMove to move Image from (row=i,column=j) to (row=Mouse point,column=Mouse point), in window form i can but in wpf i can't. Sorry for my english. Thank :-O

                          M Offline
                          M Offline
                          Mark Salsbery
                          wrote on last edited by
                          #13

                          Drag & drop is another alternative you could consider... Drag & Drop Explained End to End[^]

                          Mark Salsbery Microsoft MVP - Visual C++ :java:

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups