How can I get the current position of the mouse while dragging over an element with AllowDrop = false? I need the current mouse position because I want my adorner to follow my mouse while dragging.
The DragOver event doesn't work since it only fires when dragging above an element with AllowDrop = true.
EDIT:
Some Context: I have an ItemsControl and want to drag & drop the items to reorder them. Reordering works, however my adorner doesn't follow the mouse.
Related
In the UserControl I made it is possible to add Rectangles by clicking. Those rectangles are saved in a List. Now I want to make it possible for the User to move those Rectangles that are drawn.
First I tried to add the MouseDown, MouseMove and MouseUp event to the Rectangle but that doesn't work because Drawing Rectangle is a Struct and not a control. I already did a Testproject and accomplished to move a button I put in in the UserControl by Designer. I tried with the code I got from Drag and Drop Function with Drawing Rectangle C# .net - Forms but this example isn't about Rectangles. It is about Controls and I don't know how to use this idea for Rectangles because
rectangle.MouseDown += delegate(object sender, MouseEventArgs e)
{
//do something
}
doesn't work. Any Ideas how to drag and drop Rectangles that are added dynamically?
You should add the MouseDown event to the UserControl. Then loop through the list and make a method to see if the coordinates are inside of a Rectangle. If it is, then hook and move it.
So you should add a bool value to, which is set to true inside the MouseDown event and set to false inside the MouseUp event. Then in the MouseMove event, check if the mouseDown value is true. If it is and a Rectangle was found, move it's position.
I would like for the Width attribute to dynamically expand until it reaches 150, and after this, user should be able to expand it further via drag (MaxWidth won't allow this).
Width="150*"
has similar functionality, but in the opposite direction(minimum width and expand as needed). I want to have "expand as needed" and maximum expansion width.
Is there any way this can be done using XAML?
Put a line on the edge that you want the user to be able to drag. Set the Cursor property to "SizeWE" on that line so that the user knows he can drag it. From that line, handle the mouse down, mouse move, and mouse up events. In the MouseDown call CaptureMouse() from the line control and save the mouse position. On mouse move, get the new mouse position, calculate the change from the mouse down position, and increase the width of the control that you were constraining to 150 by the amount of the mouse move in the right direction and then reset the original mouse position to the current mouse position. On MouseUp, call ReleaseMouse() from the line control.
Hope this helps.
You can programmatically set the width depending on the size of the item. Bind the width to a GridLength and then you can let the user control it, or control it yourself up to 150
private GridLength _marquee1Width = new GridLength(150, GridUnitType.Pixel);
You can set this to GridUnitType.Pixel, GridUnitType.Auto, or GridUnitType.Star
I'm trying to show some movable controls in silverlight. I've a grid, and dynamically I've to add some controls(I'm now trying with Thumb). And user can move those controls within the grid(in the space specified for the grid). I'm not saying about the Drag and Drop controls. Actually the controls are to move as a user press mouse left button on it and starts to drag it.
Please help. Thanks in advance.
What you describe is drag & drop only. You need to implement this.
handle the left mouse click, mouse move & left mouse up events for each of the controls you want to allow to be moved.
in the left mouse click event handler:
set a flag "drag_on" to True
in the mouse move event handler
check if "drag_on" is true, if false, return.
if drag_on is true, then set the control position (x, y) same as the mouse position.
you will get the mouse position from the parameter of the event handler
in the mouse up event, set drag_on to false. Also set the control position to that of mouse position.
Note:
you will need to transform the mouse position to the grid.
Get Absolute Position of element within the window in wpf
instead of setting the control position to the mouse position, you can also try setting the mouse position as the center of the control (homework for you).
There are some LibraryBarItems in a LibraryBar control, when I drag horizontally, all the LibraryBarItems move.
But I need to disable this drag event, this means that when I drag the LibraryBar horizontally, nothing happens.
p.s. Actually I want to implement the drop event in horizontal direction rather than in Vertical direction.
I'd suggest adding a transparent visual item over the top of the control to capture input and only allow it through if want it to get through.
Greetings,
I need to be able to drag and drop items that's contained in a Border.
So far I managed to find the border on the MouseLeftButtonDown event.
Now I wish for the item to move with the mouse when I have my mousebutton down.
I assume this can be done by simple settinga bool "dragging" to true when the item is clicked and then handle the moving in the MouseMove event.
But I can't seem to figure out how to move the item. Border doesn't have a property as Position or Location. Is there any way I can achieve what I want?
Perhaps there are controls for it that I dont know of?
Bit more background information:
I'm showing multiple columns (each column is a new canvas) with rows in it. Each row and canvas represent a cell. In a some cells I have a border containing a textblock with information. Upon clicking this border I wish for it to be bound to my mouse and move where I move my mouse.
I would recommend you use the Silverlight Toolkit which contains a framework for doing this sort of drag and drop work. Once installed open the documentation and lookup the PanelDragDropTarget control.