Wpf parent child relationship - wpf

So i need to know that if a wpf window has got say 3 grids and we have a user control that we can drag. So when i drag it over the grids. Does the parent/child relationship changes every time i drag it over different grid?

I would say no, however that depends on your Drag/Drop implementation
Usually when dragging an item, you're actually just dragging a placeholder of that item in the Adorner Layer. The actual item may or may not get removed from the original parent. The item doesn't actually get added to the new parent until you release the mouse and the Drop method occurs.
When I implemented some custom drag/drop in the past, the item got removed from the parent control on Drag, and on Drop it got added to a new parent (either a valid drop target, or back to the original parent)

Related

WPF Drag events Is Handled By Parent also

I am trying to implement form designer. In which I have a panel that contain another element and so on.Each dragable/dropable element will be an UserControl. I have created a common behavior for FrameworkElements and is applied for each dragable/dropable element. The problem is, when I drag over an element inside another element say a textBox Inside a panel, the dragover/dragenter events (drag events) are fired on both the parent and child. How to avoid firing events on the parent/container.

How do I get a UserControl or View to refresh, redraw, repaint, etc.?

I have a situation whereby codebehind in a UserControl populates a Grid object with child Grids containing 1 ColumnDefinition and N Row Definitions. Each row definition contains various user controls.
When the code is done creating the control layout, something is being left over in that controls that have been removed are still appearing on top of the controls that should be there.
I think all I need to do is somehow force a repaint of the screen but how is that done?
I've tried UpdateLayout, InvalidateArrange, InvalidateMeasure, etc etc, nothing will force the layout to refresh.
You don't need any call anything to trigger a re-draw, only to trigger re-layouting and that usually only if you write your own panels.
If you see something, it's in the visual tree.

Show parent / child rows in same datagrid

looking for some guidance on how best to approach this problem.
I've got a requirement to create a data grid that shows some parent rows and on each parent row we have a expand / collapse button. When clicked this button will cause some related child rows to be displayed under the parent row.
I've implemented this by using a rowdetailstemplate on the parent data grid that when expanded shows another datagrid that then displays the related child rows. This earlier question Displaying hierarchal parent child data in WPF DataGrid shows the details..
This works ok but has the problem that each parent row effectively has a seperate datagrid for the children, so things like navigating with the keyboard don't work too well, as the child rowdetails area is skipped over, the only thing we navigate is the parent.
Other problems are when we select child rows with the mouse we end up with several rows that are highlighted as selected. This is understandable as each grid is a seperate control.
The aim is to treat the child rows the same as the parent rows, in terms of selecting , navigating etc.
Does anybody have any pointers or guidance as to how to solve this problem?
Thanks
WPF DataGrid cannot be used for hierarchical tree view style representation that easily. There is a TreeListView control but it isnt as sophisticated WPF datagrid in terms of virtualization, editing, cell \ row selection etc. But given that your data grid will be readonly and your selection is row-wise you can apply styles to this control to make it look like its a WPF data grid.

WPF ListBox setting focus to first item after losing focus

Problem:
I'm having trouble managing focus and item selection in a WPF ListBox with an embedded ListBox and some custom selection management in the code behind.
Background:
The "parent" ListBox is assigned a DataTemplate containing a "child" ListBox. I have added code to navigate using the up and down arrow keys from the parent ListBoxItem to the child items and from the child items back to the parent items.
When navigating back to a parent item which is not the first item in the first level list box the focus is always set to the first item and it is selected even though it is set in the program to a non-first item (e.g. parent item #2). I have traced through the code and the focus and selection is set to a non-first item but then another event to select and set the focus to the first item is always received.
There was another case on stackoverflow which was similar to what I am experiencing. The case is wpf listview lost the focus I tried all of the answers and responses in this case and none of them worked.
Does anyone have insight into why I can't programmatically select a non-first element in the parent ListBox?
Try using ListBox.IsSynchronizedWithCurrentItem = true then working with the list's collection view (if you're not explicitly creating one, you can use CollectionViewSource.GetDefaultView) to set the current item (ICollectionView.MoveCurrentTo()).
Selectors have a much more preferable method of dealing with selected items when working with the collection view, rather than SelectedItem.
I have been struggling with the Focus in ListBoxes too. Perhaps the answers to these two questions will help you further. Especially the trick with setting the focus on a Background thread is very very useful.
Deselect item in ListBox
Delay change of focus

Incorrect item gets selected when starting a drag on an item in an ItemsControl

I am implementing a DragDrop framework for WPF (which incidentally you can find here).
I have a problem in that when the user MouseDowns on an ItemsControl we don't know immediately if they intended to click the item to select it, or to begin a drag. If the user clicks an item and then quickly moves the cursor, another item other than the clicked item can become selected before we determine that a drag has started (especially if clicking on the item freezes the UI for a short time to load data etc).
I think this problem didn't exist in WinForms as dragging the mouse with the button held down didn't cause another item to become selected - the selection was made only on the item on which the click occurred.
In the PreviewMouseDown event I can set the e.Handled property to prevent another item becoming selected, which works fine if the user actually did intend to start a drag, but then they can't actually select the item.
Anyone know how to handle this?
Store/use the point at which they originally moused-down, and use that to resolve the item to drag.

Resources