When you sort a List View why cant you sort it again there after?
The listbox holds Check boxes which are dynamically added.
(XAML)
<ListView Name="Main_List_View" />
(C#)
//The Dictionary list is being populated before this loop is run
Dictionary<string,string> list = new Dictionary<string,string>();
foreach(var item in list)
{
CheckBox box = new CheckBox();
box.Name = item.Key;
box.Content = item.Value;
Main_List_View.Items.Add(box);
}
Cool I loaded the list view with items now I sort them.
(C#)
Main_List_View.Items.SortDescriptions.Add(new SortDescription("Content", ListSortDirection.Ascending));
The list is now sorted in Ascending successfully in Runtime.
But As soon as I Sort the same listview to Descending with a button Nothing Happens what could it be the reason?
(C#)
//Button event here
Main_List_View.Items.SortDescriptions.Add(new SortDescription("Content", ListSortDirection.Descending));
Before you add Descending SortDescription, clear the available SortDescriptions.
Main_List_View.Items.SortDescriptions.Clear();
Main_List_View.Items.SortDescriptions.Add(new SortDescription("Content", ListSortDirection.Descending));
Related
I have a list that is bound to a datagrid, a property of the items is a boolean and is bound to a checkbox in the datagrid.
How can I allow only one checkbox to be selected?
For example, if one checkbox is selected, then the other checkboxes should be unselected.
What I have tried so far (in the ViewModel, Update is the boolean property):
var update = item.Update;
Items.ForEach(x => x.Update = false);
Items = new List<Item>(Items);
item.Update = update;
But this not efficient and it throws an Exception that the list was modified (collection was modified; enumeration operation may not execute).
Is there an efficient way to get the job done?
Edit: I'm using Binding to bind the list, the list is from type List<>, and the items of the list implement INotifyPropertyChanged
I've commented the issue that you have in this code:
var update = item.Update;
Items.ForEach(x => x.Update = false);
Items = new List<Item>(Items); //This creates a new, empty Items list
item.Update = update; //this item relates to an item in the collection that no longer exists
You should have ObservableCollection<Items> Items so that if you add/remove items from your list the Binding will follow and Item implementing INotifyPropertyChanged. As ObservableCollection doesn't support ForEach your code becomes:
var update = item.Update;
foreach(var element in Items)
{
element.Update=false;
}
item.Update = update;
How to add ListView selected items in other ListView?
Example project:
ExpTreeLib
Put this form ListView2 and add lv.selectedItems.
Thanks in advance.
You can use this method to add the items
ListView list = new ListView();
ListViewItem item = new ListViewItem();
// add them
list.Items.Add(item);
This would be enough, now you can use a simple loop for each element to add it to that list.
foreach (ListViewItem item in ListView.SelectedItems) {
// create a new list, or use currently present list
// add each item to it using .Add() method.
}
I have a ListBox which is made up of Grid Items in Multiple SelectionMode in Silverlight 3.0.
When I use ListBox.SelectedIndex it only returns the first item which is selected.
I would like to be able see all of the selected items such that it would return all of the selected item indexes' such as; 2, 5, and 7, etc.
Any help?
Cheers,
Turtlepower.
You can find the selected indexes by iterating through SelectedItems and finding the objects in the Items property, like this:
List<int> selectedItemIndexes = new List<int>();
foreach (object o in listBox.SelectedItems)
selectedItemIndexes.Add(listBox.Items.IndexOf(o));
Or if you prefer linq:
List<int> selectedItemIndexes = (from object o in listBox.SelectedItems select listBox.Items.IndexOf(o)).ToList();
How can I select multiple items from the listbox in a Windows Phone 7 application?
e.g
listboxName.SelectedIndex = 0;
listboxName.SelectedIndex = 1;
listboxName.SelectedIndex = 2;
The above code selects 2 while I need to select all three of those.
The values I need to preselect are given to me in a array like
{true,true,true,false,false}
So I tried the using IsSelected like shown below... doesn't work.
int i = 0;
foreach (ListBoxItem currentItem in listboxName.SelectedItems)
{
if (tagindexeselected[i])
{
currentItem.IsSelected = true;
}
i++;
}
What would be the proper way to select multiple items in a listbox?
Hard to say there's a single, best way - it depends on how you're populating your list box, etc. First, be sure your list box's Selection Mode is set to Multiple or Extended.
One option is to use the ListBox's SelectedItems collection:
listBox1.SelectedItems.Add(listBox1.Items[0]);
listBox1.SelectedItems.Add(listBox1.Items[1]);
listBox1.SelectedItems.Add(listBox1.Items[2]);
Note also, in your example above, you're iterating over the SelectedItems collection - not the Items collection. If nothing is selected, that's an empty collection. Also, if your list box ItemsSource is not a series of ListBox Items (you can set your itemsSource to almost any enumeration), you will get an InvalidCastException when you go to run your foreach statement.
foreach (DataRowView item in lstServer.SelectedItems)
{
string WebServerIP = item[lstServer.DisplayMember].ToString();
string WebServerUrl = item[lstServer.ValueMember].ToString();
_WebObjIgent.Url = WebServerUrl;
}
Note : lstServer is Listbox of window application . By using Displaymember and valuemember proprty you can access value and text of listbox.
I want to reach checked rows item in ASP.Net Listview control.
For example:
if 3 Lines of Listview checked, I want to reach checked items value in listview.
How can I do this?
Thanks.
You can use listview property 'CheckedItems'.
You can get list of items which are checked using listview.CheckedItems. For the value in that item, based on the subitem index you need to fetch it. For ex.
ListView.CheckedListViewItemCollection checkedItems =
ListView1.CheckedItems;
foreach ( ListViewItem item in checkedItems )
{
value = item.SubItems[1].Text;
}