Silverlight tabcontrol, how to Keep the current selected tab selected - silverlight

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.

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.

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

Silverlight 4 - Instantiate Objects Not Yet Visible

I have a Silverlight 4 application that uses multiple tabs. On Tab 1 I allow the user to select items from a grid, right click and select a context menu item that sends the items to a different grid on Tab 2. However, if the user has not clicked on Tab 2 yet, the grid I am trying to add items to does not exist yet.
What is the most elegant way to make sure that all objects on the second grid are instantiated even if the user has not selected the tab yet?
Thanks,
-Scott
Assuming you are adding items to data grid of some sort then the way to go is to add your items to an ObservableCollection and then data bind the grid to the collection.
That way you don't have to worry about whether the grid is actually visible or not and it will update itself when it does become visible.
Instead of trying to prepopulate the grid on tab2 with data that does not yet exist, you could use the MVVM pattern.
Tab1 could be view1 of viewModel1. Tab2 could be view2 for viewModel2. When view1 updates viewModel1, ViewModel1 updates ViewModel2, which, in turn, updates view2. Then, you only need to set bindings for the visibility, isEnabled, and ItemsSource properties.

silverlight combobox - highlight a few items in the popup list

I'm wondering if I can make fake sections in the popup menu:
The rule would be, if the 5th character of the displayed item is different from the 5th char of the previous item in the menu, it has to be highlighted
What do you think?
Thanks!
To achieve this would be a hack.
Normally the items that appear in the popup part of a combo box will be an instantiated data template, and each gets its own data item and has no clue or knowledge of the other items in the list, so you couldn't use a converter or anything else to achieve this behavior.
What you could do though is inject (attach) your own control into the popup part of the combo box, and take over the rendering of the data items. How you do this will depend upon which combo box you are using (i.e. MS or some other vendor's) and would be a whole new question.
Would that be easier if I were to create my own combobox as follow:
a TextBox associated with a Button that when pushed would popup a datagrid in which I could implement this conditional formatting?

WPF SelectedItem and Scrolling

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.

Resources