Scrolling to the Previously Selected ListBox item - c#

I am having trouble setting the Selected Item of a Listbox I am populating and adding to the LayoutRoot's children in code.
I am creating the ListBox over when going back to the page, so I am saving a variable which will tell me what the selected item was before the user clicked.
I tried setting SelectedIndex, but that did not seem to work. That selects the item and calls SelectionChanged, but the item does not come into focus.
I also tried the combination of MyListBox.ScrollIntoView(MyListBox.Items[MyListBox.SelectedIndex]) and MyListBox.UpdateLayout(), but that did not seem to work either. The item does not seem to come into focus.

Try MyListBox.SelectedItem.EnsureVisible().
(If it doesn't have SelectedItem (ListBox on the desktop CLR doesn't), then use SelectedItems[0] instead)
E: Okay, looks like Windows Phone doesn't support that. However, it does support MyListBox.EnsureVisible(MyListBox.SelectedItems[0])

I ended up fixing this by Adding the UserControl that had the ListBox into the Page's XAML, and the ListBox into the UserControl's XAML.
Then, I was able to use the ScrollIntoView(MyListBox.Items[mySavedSelectedItem]);
I simply saved this value when the user made the selection.
mySavedSelectedItem = ((ListBox)sender).SelectedIndex;

Related

Maintaining combobox text after selecting a bound item in WPF / C#

I have a WPF ComboBox which I'm using to search names. I use the MVVM pattern and it's all pretty simple:
The ComboBox Text property is bound to a "SearchString" property on the VM. The ComboBox ItemsSource is bound to a "SearchResult" property on the VM. This is a list of objects displayed using a DataTemplate. The ComboBox also triggers a "PreviewKeyDown" event and pressing enter selects the first item in the result set, with up and down arrow keys traversing the results. The SelectedItem is bound to the DataContext for a GroupBox. This part works really nicely.
When an item is selected in the ComboBox, WPF is automatically attempting to replace "Text" with my SelectedItem. This causes my results set to get emptied and "Text" to revert to an empty string.
The behaviour I'd like is that when an item is selected, the text in the ComboBox remains exactly the same, so that my user can continue to traverse the result set using up and down arrows.
Is there an elegant way to achieve this? I think that nothing will be added to the question with a code snippet, but happy to supply if wanted.
The behaviour I'd like is that when an item is selected, the text in the ComboBox remains exactly the same, so that my user can continue to traverse the result set using up and down arrows.
That's a requirement that's not built into the ComboBox control. If you want something like that, you'll have to build it yourself (and subclassing most likely won't help, you'll have to build most of a ComboBox from scratch for this).
In fact you shouldn't be binding the current item like that either, you should use the SelectedItem property, which doesn't do any conversions or copying, exactly what you want for an internal binding.

Selected ListViewItem is not highlighted on TabPage, after selected TabPage changed forth and back

I am currently facing a strange behavior of the C#.Net ListView when used on a TabControl's TabPage.
Background:
I have said TabControl with three TabPages, the ListView is on the first one.
Scenario:
I select one Item in the ListView and its "Selected" property is set to "true"
I change to another TabPage
I change back to the first TabPage with the ListView containing the selected Item
Result:
The former selected Item in the ListView is not highlighted any longer. Iterating over all Items shows that its "Selected" property is still set to "true".
As soon as I click into the ListView control, I can see the selected Item being highlighted for a short moment, before loosing its highlighting again due to me having clicked somwhere else and therefore deselecting it.
But neither one of Refresh, Invalidate, RedrawItems, Focus or Select on the ListView leads to the selected Item being highlighted again.
Has anyone of you experienced the same issue and found a proper way to solve this?
My current solution is to clear the List of SelectedItems on the ListView as soon as the corresponding TabPage is re-focused, hence deselecting every Item and forcing the user to re-select what he wants to be selected.
But since I also noticed that ListBoxes can savely store their selection and display it properly, I would wish for the same feature on the ListView.
Thanks,
Thomas

WinRT XAML ComboBox opens in middle of list

So one particular behavior of the XAML combobox in WinRT is causing me a huge headache, because my client sees it as a defect, and doesn't care if it's the behavior of the control, he wants it changed. However, I cannot find anything that tells how to change it. The behavior I'm speaking of is that when nothing is selected the ComboBox popup opens displaying the ItemsSource in the middle of the list. I have a sorted list of countries, with the exception of US, UK, CAN being at the top. These 3 items are the most often selected items and the client wants them on top rather than having to scroll through the list to find them. That's easy enough, but because the list opens in the middle, you still have to scroll quite a bit to get to them. Is there some property I'm missing that turns this behavior off? I was able to finally convince them that the CarouselPanel wasn't a defect, but this one isn't going to fly.
Thanks in advance!
UPDATE:
So this combobox is databound through a ViewModel. in this instance, the ViewModel has no value (it is an empty string) for that particular property and so the Combobox shows empty, which is fine and desirable. When you click on the Combobox to select a value, it displays the list in the middle of the available values. this is the behavior that is undesirable. it should be showing the 1st value in the list at the top!
Well, one would think that the out of the box Combobox (there is no other built in dropdown control) would be able to work like any other combobox control in any other MS technology to date, but of course this is MS, so why should things be consistent. At any rate, I ended up having to create a "blank" entry and pre-select that item if the value in the VM is empty, and then write code in the setter of that property to ignore if "blank" item if it is selected. It's kludgy and wreaks of code smell, but it works
When you set the SelectedItem property to an object, the ComboBox attempts to make that object the currently selected one in the list. If the object is found in the list, it is displayed in the edit portion of the ComboBox and the SelectedIndex property is set to the corresponding index. If the object does not exist in the list, the SelectedIndex property is left at its current value.

Detecting when a tab item was added to WPF Tab control

I am trying to synchronize the selected tab item of a WPF tab control with the last item that was added.
Since there is no such property as e.g. IsSynchedWithLastAddedItem, I am trying to detect when an item was added in order to point the SelectedItem at the last added one.
I cannot find the event that gets raised - either on the tab control or its Items, when a TabItem was added.
I am sure something like it must exist though, so I hope someone can help me out.
var view=CollectionViewSource.GetDefaultView(m_tabControl.ItemsSource);
view.CollectionChanged+=(o,e)=>{/*Here your code*/};
If you work directly with the Items-collection, the same technique will work also. Get the default CollectionViewSource for this collection.
var view=CollectionViewSource.GetDefaultView(m_tabControl.Items);
view.CollectionChanged+=(o,e)=>{/*Here your code*/};
As Timores wrote, for the m_tabControl.Items-property, you can attach a handler directly. The same is also true for most ItemsSource-references, but there you have to check yourself for the INotifyCollectionChanged-interface.
I have not tested it. Make a comment if it does not work.
The Items collection is of type ItemCollection, which derives from CollectionView which implements INotifyCollectionChanged. So you could listen to CollectionChanged and get to know when an item is added.
Don't know how to do that in XAML, though.

How to make a TreeView's and ListBox's SelectedItem stay in sync?

I'm certain this has come up before, but I haven't been able to find the answer.
I made a basic ViewModel that contains a list of People (an array of Person) with a property called SelectedPerson, which naturally points to the currently selected Person in the list of People. I also have a ListBox and a TreeView that are databound to the ViewModel's People list.
What I'd like to do is to keep the ListBox's SelectedValue and TreeView's SelectedItem in sync with with the ViewModel's SelectedPerson. The idea is that no matter how the SelectedPerson is modified (through a control, through code, etc), all the controls should update themselves properly. I can get it to work with two ListBoxes, which is nice, but I can't get it to work with a ListBox and a TreeView because the TreeView's SelectedItem is readonly and apparently unavailable through XAML.
Where should I look to get ideas on making this work?
Also note that I'm trying to make this work in pure XAML. No code-behind as XAML files in my application can be loaded and changed dynamically.
Thanks!
You can Use Selector.IsSyncronizedWithCurrentItem.
You can bind both thye listbox and treeview to the same datasource and make sure that the IsSyncronized parameter is set to true. Then any changes to the current item in one will be reflected in the other.
More information can be found here:
link text
I asked around and the best solution I could find was here
http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/cc73893a-3383-4328-a002-ed8fb002a19d
It works for me but it's not the most optimal solution at this point.

Categories

Resources