Cannot clear selection of ListView - wpf

In my listview, when I select on of the item I will show a new view and set the listview to not select any item by set SelectedItem to be null. But the listview still select the old item that I selected. According to this link, I have set
IsSynchronizedWithCurrentItem="True"
But it still same. My item list is compose from the ItemViewModel that inherited from MvxViewModel

Are you using ListView(ListBox) or any custom control?
A class inherited from MS ListBox (such as ListView) has a static method to unselect all selected items:
ListBox.UnselectAll()
To unselect only one item you can cast selected item to ListBoxItem object and call:
ListBoxItem item = (ListBoxItem)obj;
item.IsSelected = false;
Have you tried it in your code behind? Or you want to achieve this declaratively by XAML markup?

Related

ListBox Mvvm - Hilight selected item

I've a simple listbox bound to ICollectionView.
When I execute something like MyCollection.MoveCurrentToPosition(index) I can see selected item event fired by my listbox, however I can't see any item hilighted.
How can I hilight a listbox item when CurrentItem of my collection view changes?

CurrentCell is changed to a row that isn't SelectedItem in Datagrid

I have a DataGrid that is part of a DataTemplate that is assigned to the ContentTemplate of a TabControl. The TabControl's ItemsSource is bound to a collection and, as such, the DataContext for the DataGrid changes to a new collection once each Tab is selected. Currently, there are bindings for ItemsSource and SelectedItem on the DataGrid.
When I move through the tabs the DataGrid is able to keep the selected row synchronized properly but the problem I'm having is that CurrentCell is always set to the first column and first row regardless of what SelectedItem equals.
I've tried setting the CurrentCell property when the DataContext changes for the DataGrid but the DataGrid always resets it back to the first row and first column. Does anyone know how I can accomplish keeping the CurrentCell on the same Row as SelectedItem when DataContext changes?
This worked for me (where dg is the DataGrid):
DataGridCellInfo cellInfo = new DataGridCellInfo(dg.SelectedItem, dg.Columns[0]);
dg.CurrentCell = cellInfo;

Set focus to List item on Listview

I have a Listview her ItemsSource property is binded to ViewModel, when i add a new item to List (ObservableCollection) i need set focus to the new item.
Bind the SelectedItem property of the ListView to a property in your ViewModel and when you add a new item to the collection set the property in your ViewModel to reference that new item and the binding will take care of the rest.

How Do I Change WPF Listview SelectedItem Font Color With ItemSource Bound?

I have WPF window containing a listview that has it's itemsource set to a collection of objects. When I access SelectedItem or SelectedItems[] or Items[], I get the my object back that's bound to that item, not the ListViewItem item itself. I have no idea how to select a row and change it's color since I can't access the item itself, like a winform listviewitem.
ListView derives from ItemsControl which exposes the ItemContainerGenerator property. This object allows you to map a bound entity to its ItemContainer (the item your are looking for) and back.

WPF Accessing Items inside Listbox which has a UserControl as ItemTemplate

I have Window that has a ListBox
ListBox(MyListBox) has a DataTable for its DataContext
ListBox's ItemSource is : {Binding}
Listbox has a UserControl(MyUserControl) as DataTemplate
UserControl has RadioButtons and TextBoxes (At first They're filled with values from DataTable and then user can change them)
Window has one Submit Button
What I want to do is, when user clicks the submit button
For each ListBox Item, get the values form UserControl's TextBoxes and RadioButtons.
I was using that method for this job :
foreach(var element in MyListBox.Items)
{
var border = MyListBox.ItemContainerGenerator.ContainerFromItem(element)as FrameworkElement;
MyUserControl currentControl = VisualTreeHelper.GetChild(VisualTreeHelper.GetChild(VisualTreeHelper.GetChild(myBorder,0) as Border,0)as ContentPresenter,0)as MyUserControl;
//And use currentControl
}
I realised nothing when using 3-5 items in Listbox. But when I used much more items, I saw that "var border" gets "null" after some elements looped in foreach function.
I found the reason here :
ListView.ItemContainerGenerator.ContainerFromItem(item) return null after 20 items
So what can I do now? I want to access all items and get their values sitting on user controls.
Thanks
You should use objects who implement INotifyPropertyChanged and bind an ObservableCollection of it to the ItemSource
And then you can get all the list of items.
Here some quick links from MSDN to get more informations
How to: Implement Property Change Notification
Binding Sources Overview
You should google for some tutorials about this.
Zied's post is a solution for this problem. But I did the following for my project:
I realised that there's no need to use UserControl as DataTemplate in my project. So I removed ListBox's DataTemplate.
I removed MyListBox.DataContext = myDataTable and used this:
foreach(DataRow dr in myDataTable.Rows)
{
MyUserControl muc = new MyUserControl(dr);
myListBox.Items.Add(muc);
}
I took DataRow in my UserControl's constructor and did what I want.
And at last I could access my UserControls in ListBox by using :
foreach(MyUserControl muc in
myListBox)
{
//do what you want
}
Easy huh? :)

Resources