I've got a Treeview with a number of items in it. Each item is the part name of a 3D object displayed in another control. I want to be able to move my mouse over the items and identify the item that I am over. This is so I can then pass the item id to the other control where I can highlight the part.
Note that I can already do this with the click event, but I need to do it without any clicking now.
How do I do this in the Mousemove event of the TreeView?
You have the class "System.Windows.Input.Mouse" that could provide you some information like DirectlyOver
But it could return you the top level of the UIElement so the TreeviewItSelf.
I thinks that another solution might be to test recursively the mouse position relatively to the item.
using Mouse.GetPosition(UIELEMENT) if the position return is negative in one of the coordinate or upper thant the control size that means your are not in the HitTestRectangle of the control.
You could also use VisualTreeHelper.FindElementsInHostCoordinates using a rect of size 1 at the relative position about you treeview (Mouse.GetPosition(TREEVIEW)) and you give in second parameter all the treeview itself.
Related
The questions says it all.
How can I move a control, say a PictureBox between multiple panels, or betwween a panel and a flow layout panel.
I'm aware I can drag and drop controls between multiple panels and such, however this is does not make the control visually movable between the containers. The mouse only changes to a diferent cursor and after you drag to the other control and release the mouse button the control appears on the other container. I require the control to be visually movable.
Can someone provide a simple example, so I can extract the idea to apply to my situation.
NOTE: Runtime of course.
Assuming you need it runtime:
You can save the control as bitmap using Control.SaveToBitmap method
Create cursor from image.
Set the current cursor which we created from control.
Once drag and drop completed reset the cursor.
Let's take a simple example of dragging a button around.
Suppose you have two types of container controls:
1) an X-Y layout
2) a flow layout (assume left to right)
When you click to drag the button, record the x-offset and y-offset from the click to the top left corner of the control. As well, record the index of the control within the Controls collection.
As the mouse moves, first check if the mouse has changed container controls.
If so, then remove the button from its current parent and add it to the new parent.
If the button is added to a flow control, then you need to calculate the new index. To do this, calculate the distance from the mouse to the closest edge of a bounding box of all other controls. Then if the mouse is left of the center of that control, insert to that control's index minus 1, otherwise insert to the right of that control (index + 1).
If the button is added to an X-Y layout, then the index doesn't matter that much. You can simple just set the button's location relative to the mouse plus the x-offset, and y-offset.
As the mouse is dragging, you will need to force the controls to refresh. I think calling Invalidate() on the container control should be sufficient.
This should give you the basic idea that you could use to start coding something.
I have a DataRepeater with some shapes on the ItemTemplate that I want to toggle on and off based on data in each Item. With every other control, I have done similar things using e.DataRepeaterItem.Controls["whatever"] in the DrawItem event, however this doesn't work with a shape as shapes are held in a ShapeContainer within the ItemTemplate. Trying to access the shape using ShapeContainer.Shapes.get_item(int index) results in a null reference error. As best as I can figure, the shapes in the container have not been initialized at the time of the DrawItem event.
So, what is the best way to modify a shape in an Item if I can't do it when each Item is drawn?
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.
Basically I am trying to implement a feature where if the user presses a key, I want to find out the item under the mouse cursor.
So I don't use Mouse events but Keyboard events which doesn't give me a ListViewItem of course.
I just don't know in what space I need to get the mouse position and convert it into the control's space.
Any ideas?
If you know which ListView control you are interested in, the following method will do the trick:
private ListViewItem GetItemFromPoint(ListView listView, Point mousePosition)
{
// translate the mouse position from screen coordinates to
// client coordinates within the given ListView
Point localPoint = listView.PointToClient(mousePosition);
return listView.GetItemAt(localPoint.X, localPoint.Y);
}
// call it like this:
ListViewItem item = GetItemFromPoint(myListView, Cursor.Position);
A keyboard action that depends on the mouse position sounds a little unorthodox. Keyboard actions should normally effect some item that is highlighted/focused/selected on the screen, either selected by previous keyboard actions or by a previous mouse click on that item.
Just something to bear in mind, or you'll wind up with a "unique" (confusing) user interaction.
I have a form with 2 tree views, the user can drag and drop a node from one to another. After a node has been dragged and dropped, I change the color[highlight] of the source node in the Drag-Drop event handles.The color of the node changes fine.
But when the users hovers the mouse over the source tree view after that, it flickers and the highlighting I had done disappears, reverting to the original color.
I'm not handling any other event, I don't reload the treeview and I'm not changing the color.
From my understanding of the MSDN documentation, I don't see any Refresh or Repaint type events.
Simply call TreeView.Invalidate() method to force tree view to repaint.