Drag and Drop aimlessly in WPF Desktop applications - c#

I'm finding it extremely difficult to drag and drop any control (and I plan to allow ANY control) to be dragged and dropped in my app without having a Drop target or whatever. I just want to be able to drag any control from, say, here... to "over there somewhere". But this doesn't seem possible. Is this possible? You can implement drag and drop in WinForms with just a few lines of code, how do we have this same drag and drop support in WPF?

What you're describing in the question and the clarifying comments is more about repositioning elements than dragging and dropping.
This type of activity is fairly easy in WPF, the only consideration is the Layout Panel that houses the elements. Because WPF tries to provide more structure around Layout (with Grids, StackPanels, etc.) you need a panel that will allow for free-form positioning of elements. This calls for the Canvas.
So if you have a Canvas with elements on it (positioned with the Canvas.Left and Canvas.Top for example), you can use the MouseDown, MouseMove and MouseUp to allow users to drag/reposition elements across the canvas.

Related

Toolbar on right side of the screen in wpf

I am trying to make a WPF application in which there is a drag able toolbar on the screen through which user can select different type of tools and use it on canvas. Also can someone tell me how to make a transparent canvas. A good example would be from this picture in which there is drag able toolbar on right side of the picture. And the user can also work on any window he wants. I am new to WPF and I am trying to find a solution to my problem for quite a while now. Can someone guide me in the right direction...
Background DP default value is null. When a Background is set to null the displayed Color is White and mouse interactions don't work. You need to set Canvas's, ListBox's and ListBoxItem's Background to Transparent to see the Background of the Parent Control
<Canvas Background="Transparent"/>
Rather than a toolbar, think of that as a window.
I would take a look at avalondock.
A transparent canvas:
<Canvas Background="Transparent"
As an aside, this is a couple of steps on from wpf beginner level.
If you take a look at this:
https://imgur.com/4n7vH8k
The user is designing a game scenario. They drag units out the panel on the left ( it's a treeview ) onto the canvas on the right. Only this is not just a plain canvas, it's an itemscontrol with a canvas as the itemspresenter. That allows me to template out the units from a collection of unit view models. It's using the mvvm pattern.
If you've not come across this technique it's a bit strange. Here's a couple of samples to take a look at which templates out different viewmodels into different UI objects. https://1drv.ms/u/s!AmPvL3r385QhgooJ94uO6PopIDs4lQ
https://1drv.ms/u/s!AmPvL3r385QhgooLcGrZOSwFswa-Dg
The datatype is used to decide what you get in the view for what viewmodel.
You have a bit of a complication in that you decided you want a dockable panel usable in multiple windows. Maybe that's not a must have.
Drag drop in my app isn't actually drag drop and relies on an odd aspect of a canvas. They don't clip, so you can add something to a canvas but have it positioned off the canvas.
As you start dragging a unit, it's viewmodel is added to the collection bound to my units itemscontrol. That templates it into the canvas as a piece. I work out the location compared to the canvas, so it'll have a negative X aspect which I bind to Canvas.Left.
Each unit is inside a thumb. A thumb is the rectangle you drag up and down on or side to side on a slider. They are designed for dragging. This way dragging is smooth and I can easily have a unit under the cursor that's being dragged.

Seamlessly passing drag&drop actions from children controls to parent ScatterViewItem

I am using Surface 2.0 SDK and my application uses dynamically created custom controls which consist of a ScatterViewItem which holds a Grid with a Menu inside each column.
When initiating a drag&drop motion on any area inside the ScatterViewItem including the Grid, or the margins around the Menu, the ScatterViewItem is being dragged along nicely together with its contents.
However, when trying to initiate such a motion anywhere inside the Menu control, nothing happens. It seems like the necessary events are being swallowed by it.
I have tried extending the Menu class to create a custom control and use a RoutedEvent to handle captured mouse clicks within the ScatterViewItem, but can't quite find a way to seamlessly handle drag events and pass them on to the parent ScatterViewItem
Can anyone share some knowledge on how to implement this?

Panels in same area in winform

I currently have a windows form that I am using as a "holder" for many panels. All of these panels are being used like pages, so say I have a default panel, a details panel etc. They all cover the exact same area and i am using a top menu to have buttons to show/hide the correct panel.
The problem I have with this is that I cannot select one panel and edit it or simply bring it to the front to edit it in "design" mode, I currently have to keep sending the top panel to the back until I get the one I need. This is okay now, but eventually my program will have over 20 panel type pages, and this will get tedious.
Is there a better way to manage this, or is there an easier way than using panels?
All these panels are used like pages
Above line in your question suggests that you should be using TabPage instead of Panels.
Therefore, You can use a TabControl, and add TabPage to it both at design time as well as at runtime easily, and add controls of your choice to the different TabPage(s).
According to wikipedia:-
Tab is one that allows multiple documents to be contained within a single window, using tabs as a navigational widget for switching between sets of documents. which is essentially what you want to do.
As shown in the picture
You can then use Button to switch b/w different TabPage or show/hide TabPage.
It will not only solve your problem of designing at design time, it will also provide a "clean" user interface. Hope that helps!
Check this:-
http://en.wikipedia.org/wiki/Tab_(GUI)
Use a TabControl instead. It will allow you to switch an active panel also in design time.
If you want to keep the panel method there is a way of working around the problem of one panel becoming an element of another. Drag the panel outside the boundary of the other panel (so that it is no longer an element of the other panel) and then re-position it using the arrow keys on your keyboard (i.e. not by dragging it into position over the other panel using the mouse). It seems like the panel will only become an element of another panel if it is dragged into position over the top of another panel using the mouse.

scrolling overlay in silverlight

I am using C#, Silverlight, WP7.
I have been going over Metro Grid Helper (see this link) as I'm also interested in doing an overlay on an existing app. Basically I want to highlight an area, which would add a Rectangle on the overlay.
The difference is that I would like the overlay to scroll with the page, so that the Rectangle would stay in place over an item (like a TextBlock) underneath. And I am confused as to how to do it.
Any suggestions as to what methods or properties or events I should look into for this?
Thanks in advance.
As I am still learning how everything works in Silverlight, I figured this one out today.
The MetroGridHelper class that is linked in the original question adds the overlay objects to be children of the main Grid of the page. This means that no matter what the other controls are for the application (like a Pivot, Panorama, or ScrollViewer), the overlay objects will not move.
Thus, to get the objects to scroll (or swipe with a Pivot or Panorama), the objects need to be the children of the control. So inside the main Grid, look for the control and make that the parent of your objects.

C# WinForms Move elements between panels

I need to create a form with two panels:
1. Destination
2. Source
On the source panel there will be picture boxes. I need to be able to move it from source to point at destination panel with mouse.
I have a problem connected with different coordinates of the panels.
Please, help with advice or an idea what to do.
Moving those controls requires changing their Parent property. That's not easy to do, there is no good time to do this while the user is dragging with the mouse. You'll also get the effect of the panel clipping the control, you cannot display it on both with half of the control on one and the other half on the other panel. And yes, you have to change the Location property of the control when you change the parent or it will jump.
Punt the problem, don't use two panels. It only has to look like a panel, easily done by drawing one in the form's Paint method (or OnPaint override, better). Use e.Graphics.DrawRectangle or FillRectangle.

Categories

Resources