My collection:
private ObservableCollection<Data> listData;
ListBox source:
ListBoxData.ItemsSource = listData;
Later i add elements 20 times at start of collection:
ListBoxData.Insert(0, new Data());
After adding ListBox scrolls to first element of collection. How to disable scrolling after adding?
does this one works for you? ListBox .SelectedIndex = 0;
Related
I'm trying to move the selected items from checkedlistbox to listbox.But I can't...
for (int x = 0; x<=checkedListBox1.Items.Count;x++ )
{
if (checkedListBox1.GetItemChecked(x))
{
listBox1.Items.Add(checkedListBox1.SelectedItem + "\r\n");
}
}
Please help me
Below code will add selected items to listBox1 and remove it from checkedListBox1, but SelectedItems will be one item each time because Multi-selection is not supported on CheckedListBox you can use CheckedItems instead of SelectedItems to select more than one item.
// Add Selected Items to ListBox
listBox1.Items.AddRange(checkedListBox1.SelectedItems.OfType<object>().ToArray());
// Remove from CheckedListBox
foreach (var item in checkedListBox1.SelectedItems.OfType<object>().ToList())
{
checkedListBox1.Items.Remove(item);
}
In WPF application, I have a ListBox and binded with data from Sql. and also have a stackpanel inthe listbox.
once i double click the item of the Listbox, Where i need to place/add a dynamic combobox.
I can get the selected index of a ListBox.
int seleteditem = lstbxusername.SelectedIndex;
and i created dynamic Combobox
System.Windows.Controls.ComboBox cmb = new ComboBox();
cmb.Background = Brushes.Green;
Using Stackpanel to add a combobox
newstckpnl.Children.Add(cmb);
My question is how to add a dynamic combobox in the selected index of a listbox.
Is this possible or not?Helps appreciated.
Sure you can. You just need to get the ListBoxItem out of the ListBox first, as can be seen below.
var listBox = new ListBox();
var listBoxItem = listBox.SelectedItem as ListBoxItem;
var listBoxItemMargin = listBoxItem.Margin;
Currently you're adding strings to your ListBox. Strings obviously don't have margins. In order for the code above to work, you will need to add ListBoxItems to your ListBox as below.
listBox.Items.Add(new ListBoxItem {Content = dr.GetString(1) });
i did some thing , its worked for me!!
I get the selected index of a listbox
int seleteditem = lstbxusername.SelectedIndex;
Then created combobox
System.Windows.Controls.ComboBox cmb = new ComboBox();
By using combobox object , i added it in listbox selected index
lstbxusername.Items.Insert(seleteditem, cmb);
For example if I click on the first item it will be at index 0.
If I click on item 15 then the index should be 16.
I tried
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
int index = listView1
}
But I'm not sure if this is the right event or I should use the listView1_Click event ?
And the listView1 does not have any property SelectedIndex.
And last thing is I want to get the item text according to the index of the item I clicked on.
Assuming you want the index of the currently selected item you can do it like this :
int index = ListView1.FocusedItem.Index
Use ListView.SelectedIndices property:
List<int> selectedIndices = listView1.SelectedIndices.Cast<int>().ToList();
It returns collection of selected indices (because by default you can select several items in listview if you click on items with Ctrl or Shift key pressed). Also note that when you deselect all items, this collection will be empty and things like listView1.SelectedIndices[0] will throw IndexOutOfRange exception.
But if you will set MultiSelect property to false. Then this collection will always contain zero or one item. You can use Count property of SelectedIndicesCollection to check if item was selected:
if (listView1.SelectedIndices.Count > 0)
{
int selectedIndex = listView1.SelectedIndices[0];
}
You need to use the selected indices list you can also do this be item too.
listView1.SelectedIndices[0]
First you can get listview item object like below
ListViewItem lst=(ListViewItem)listView.SelectedItems[0];
from that object(lst) you can get the text like below
string text=lst.Content.ToString();
According to MSDN, there is still SelectedIndex. In my opinion your event is wrong, but you can still see it by .SelectedIndex. As it was mentioned before.
UPDATE: according to the comment, the link is fixed for the correct case.
How can I remove items from the listbox and the List at the same time? The User selects has the listbox click the button to remove, it removes the listbox and list and then updates the listbox.
I'm not using as datasource, I added items in the listbox with foreach
mylist.ForEach(delegate(list add)
{
listBox1.Items.Add(list.ITEM + list.VALOR [...]);
}
);
Solution:
mylist.RemoveAt(listbox.SelectedIndex);
I'm trying to remove an item from a listBox after i have drag and dropped it, but when i do so i got a message saying i cant modify the listbox since its linked with a datasource.
The thing is that i want to remove the item only from the view of the listBox while keeping it in the datasource.
Here is what i have tried to do:
int indexSelec = listBox3.SelectedIndex;
listBox3.Items.Remove(listBox3.Items[indexSelec]);
Does anybody has a solution?
Thx
You should storage List Box items to List< string > then use that List to add or remove items from List Box. Here is example:
First add a new list: List<string> _items = new List<string>();
Now add items to list and display it on List Box:
_items.Add("One");
_items.Add("Two");
_items.Add("Three");
listBox1.DataSource = _items; //this will display added items to List Box
Adding items to List Box:
_items.Add("New Item " + DateTime.Now.Second);
listBox1.DataSource = null;
listBox1.DataSource = _items;
Removing items from List Box:
int selectedIndex = listBox1.SelectedIndex;
try
{
_items.RemoveAt(selectedIndex);
}
catch
{
}
listBox1.DataSource = null;
listBox1.DataSource = _items;
You cannot remove the ListBoxItem from the ListBox, because it is part of a ReadOnlyCollection.
But you can set its Visibility.
ListBoxItem c = (ListBoxItem) ListBox1.ItemContainerGenerator.ContainerFromItem(ListBox1.Items[0]);
c.Visibility = System.Windows.Visibility.Collapsed;