WPF SelectedItem and Scrolling - wpf

I have a WPF DataGrid. The DataGrid is bound to an IList. The list has many many items so the DataGrid MaxHeight is set to a predefined value and the DataGrid automatically displays a scroll bar. The selection mode and unit are set to "single full row".
The form has a button called "New" that adds an item to the list that the datagrid is bound to.After "New" is clicked, the new item should be the new selected item. I do this via : dataGrid.SelectedItem = newItem;
This works ..but not as well as I expect it to.
I also want the dataGrid to scroll down to the newly selected item .. Any idea on how to do this ?
Regards,
MadSEB

After you set the SelectedItem, make sure to call DataGrid.ScrollIntoView with the new item. This will make sure that the new item is visible on screen.

Related

How to add a specific item as the last item of listview

I have a ListView control which is bound to a ObservableCollection, I want to add a specific button at the end of the last item, so that when user click the button, clear up the colltions, then hide the button. Also, if the collection is empty, don't like the button display. After searching, I found many solutions on how to change the last item's style, but in my scenario, the button isn't the element of the ItemsSource. Anyone can help?
Thanks
Add the button to the template so it appears on every element in the ListView. Then use a trigger to show/hide the button based on whether the item is the last item in the list.
One way to do this is to expose the last item in your list via a property on your ViewModel. The trigger should compare the DataContext of the template, which is the current item, against the LastItem. Success should set the visibility of the button.
I hope this helps.

Drag a not selected item from list with an already selected item does not come into drag info on mouse down event?

In WPF ItemsControl, dragging an item (not selected) with an already selected item with control key pressed, does not give the clicked item into selected items list of the items control. Therefor the second item (which wasn't selected but dragged) never gets dropped as it is not in the selected list of items control.
The DragInfo object is being created on drag source's mouse left button down event.
Instead of using the selected item, use InputHitTest in the MouseDown event to find the element that was clicked, and use this element's DataContext to create the DragInfo object.
I'm not sure if this is what you want, but you can handle OnPreviewMouseleftButtonDown event and add the dragged (but not selected) item to SelectedItems.

Silverlight tabcontrol, how to Keep the current selected tab selected

I have a tabcontrol which i put inside a dataform that is bound to a collection. if I am on page 1 of the dataform and i select tab 2 of the tabcontrol, when i navigate to page 2 of the dataform the tabcontrol default the selected tab to the first tab. I am using MVVM. So, my question is how do i keep whatever tab was selected to stay selected when I am paging thru the dataform records. Thanks.
Its unclear to what exactly is your tabcontrol selected tab bound.
if its bound to a some value on object from collection that you are paging through its quite obvious it wont be saved, because it would mean whenever you change tab index you should update entire collection to same index, so when you tab to next item it has same tabindex as previous item had.
You can do this exactly I guess. or post more code so we can find a better solution.

Datagrid Row Selection On Button Click In WPF

I am having a Datagrid that gets populated with values from a DataTable. In my program i have four buttons: Goto First, Last, Next and Previous, as the name name indicates i have to select the rows based on the selection made using these buttons. Everything seems well if i use the below code to get the row (for example first row).
DataGridRow row =(DataGridRow)userControl.m_DataGrid.ItemContainerGenerator.ContainerFromIndex(0);
row.IsSelected = true;
But the code throws null value when there is more rows than the height of the Datagrid(When scrollbar comes into picture).
Please help me out of this issue. I think this is because of the view problem.
Due to virtualization the containers are only created when the object is in view, so you could first scroll the item into view using the respective method, wait for the creation of the container and then select it.
As this is rather messy i would suggest binding the IsSelected to a property on your item using a style for DataGridRow (set it as ItemContainerStyle). Then you can just set the property to true and scroll the item into view if need be.

Have TreeView update its SelectedItem when programmatically changed

In WPF I have a TreeView control where a particular item can be selected either by the user selecting the item directly in the tree view or by clicking on a screen control. The tree view is displaying a list of elements that are being displayed on a user defined form, basically a form designer application.
Here is my problem. When the user clicks on a screen control it calls a method that returns the TreeViewItem that represents the element. It then sets the IsSelected property to true for this element. It correctly changes the visual indicator in the TreeView and it raised the SelectedItemChanged event in the TreeView. This is all good.
However, it appears that somewhere behind the scenes the TreeView still thinks the previous item is selected. Here is why I have come to this conclusion. If I select ElementA by clicking on it in the TreeView is it correctly selected. If I then select ElementB by clicking on the screen control and programmatically setting the IsSelected property for the ElementB TreeViewItem it appears to have selected it correctly. Now if I select ElementA again by clicking on it in the TreeView it does nothing. The SelectedItemChanged event is not raised and the reverse selection box that indicates the selected item stays on ElementB. If I click on ElementB in the TreeView it also does not raise the SelectedItemChanged event, however it does not appear to update the internal flag since if I then click on ElementA on the TreeView it processes it correctly and raises the event.
The only workaround that I have found for this is in the SelectedItemChanged event handler to call the Focus method for the now selected TreeViewItem. If I do this I get the expected behaviour when I select screen controls and programmatically change the selected TreeViewItem.
This is not an acceptable solution though as it creates focus change flicker. When I select items on my form window the focus goes to the TreeView control and then back to the form, causing flicker and slight delay.
Anyone have any indeas.
Update
As requested here is some code. Here is my method of my Explorer window which is the manager of the TreeView in question.
public bool SelectItemByName(String controlName)
{
bool fReturn = false;
TreeViewItem tviToSelect = FindItemByName(_tviMaster, controlName);
if (tviToSelect != null && _tviSelectedItem != tviToSelect)
{
tviToSelect.IsSelected = true;
// Make sure the selected item is visible in the TreeView by expanding all of the parent nodes
ExpandAllParents(tviToSelect);
tviToSelect.BringIntoView();
fReturn = true;
}
return fReturn;
}
Every element has a unique identifier that I use as a cross reference between different areas of the interface. When you click a screen control it uses its identifier to find the cooresponding TreeViewItem in the TreeView. Then this code sets it as selected.
Then in my SelectedItemChanged event handler I had to include the following line.
_tviSelectedItem.Focus();
This fixes my initial issue but introducing the unwant screen flicker.
To recap, I select ElementA in the TreeView directly, then select one or more other elements in the form designer which in turn calls SelectItemByName to programatically set the selected item. All visual indicators show that this worked. In the TreeView the highlighted item changes to the new item that is selected. After selecting any number of elements through the form designer interface if you select ElementA by clicking on it directly in the TreeView it does nothing. It does not get highlighted and it does not fire the SelectedItemChanged event. If you inspect the SelectedItem and SelectedValue properties of the TreeView they all correctly coorespond to the item that was programmatically selected. However, the control somewhere appears to think that ElementA is still selected and doesn't recognize that the selection is changing.
I cannot believe that other people haven't run into this. It appears to be a significant flaw in the TreeView contol in WPF. Not sure if WinForms has the same issue or not.
Each TreeViewItem has an IsSelected property, and I suspect the old one isn't getting set to false. Try setting it to false whenever you set the new item to true.
var currentItem = treeView.ItemContainerGenerator
.ContainerFromItem(treeView.SelectedItem) as TreeViewItem;
currentItem.IsSelected = false;
If that doesn't work, try setting focus on the newly selected item at the same time as when you select it. Don't forget that WPF also has two focus scopes: Logical Focus and Keyboard Focus. You may need to set both.
treeViewItem.Focus(); // Sets Logical Focus
Keyboard.Focus(treeViewItem); // Sets Keyboard Focus

Resources