Getting value of selected item in list box as string
If you want to retrieve the display text of the item, use the GetItemText method: string text = listBox1.GetItemText(listBox1.SelectedItem);
If you want to retrieve the display text of the item, use the GetItemText method: string text = listBox1.GetItemText(listBox1.SelectedItem);
Use a CollectionViewSource: <CollectionViewSource x:Key=”SortedItems” Source=”{Binding CollectionOfStrings}” xmlns:scm=”clr-namespace:System.ComponentModel;assembly=WindowsBase”> <CollectionViewSource.SortDescriptions> <scm:SortDescription PropertyName=”SomePropertyOnYourItems”/> </CollectionViewSource.SortDescriptions> </CollectionViewSource> <ListBox ItemsSource=”{Binding Source={StaticResource SortedItems}}”/> You might want to wrap your strings in a custom VM class so you can more easily apply sorting behavior.
I also had to set: HorizontalContentAlignment=”Stretch” on the containing ListBox.
var choices = new Dictionary<string, string>(); choices[“A”] = “Arthur”; choices[“F”] = “Ford”; choices[“T”] = “Trillian”; choices[“Z”] = “Zaphod”; listBox1.DataSource = new BindingSource(choices, null); listBox1.DisplayMember = “Value”; listBox1.ValueMember = “Key”; (Shamelessly lifted from my own blog: Bind a ComboBox to a generic Dictionary.) This means you can use SelectedValue to get hold of the corresponding dictionary … Read more
You need to set FocusVisualStyle of each ListBoxItem to null. Steps are bellow 1) Create ItemContainerStyle for the ListBox <Style x:Key=”ListBoxItemStyle1″ TargetType=”{x:Type ListBoxItem}”> <Setter Property=”FocusVisualStyle” Value=”{x:Null}”/> …. 2) Set that style to Listbox <ListBox ItemContainerStyle=”{DynamicResource ListBoxItemStyle1}”
I find solution that instead of using ListBox I used ListView.It allows to change list items BackColor. private void listView1_Refresh() { for (int i = 0; i < listView1.Items.Count; i++) { listView1.Items[i].BackColor = Color.Red; for (int j = 0; j < existingStudents.Count; j++) { if (listView1.Items[i].ToString().Contains(existingStudents[j])) { listView1.Items[i].BackColor = Color.Green; } } } }
You can extend the behavior of the ListBox by using attached properties. In your case I would define an attached property called ScrollOnNewItem that when set to true hooks into the INotifyCollectionChanged events of the list box items source and upon detecting a new item, scrolls the list box to it. Example: class ListBoxBehavior { … Read more
You must override the Drawitem event and set the DrawMode property to DrawMode.OwnerDrawFixed check this sample private void listBox1_DrawItem(object sender, DrawItemEventArgs e) { if (e.Index<0) return; //if the item state is selected them change the back color if ((e.State & DrawItemState.Selected) == DrawItemState.Selected) e = new DrawItemEventArgs(e.Graphics, e.Font, e.Bounds, e.Index, e.State ^ DrawItemState.Selected, e.ForeColor, Color.Yellow);//Choose … Read more
Finally! If found a way much more elegant and probably with better performance either. (see also Accessing an ItemsControl item as it is added) We “misuse” the property ItemsControl.AlternateIndex for this. Originally it is intended to handle every other row within a ListBox differently. (see http://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.alternationcount.aspx) 1. Set AlternatingCount to the amount of items contained … Read more
<ListBox.ItemContainerStyle> <Style TargetType=”ListBoxItem”> <Setter Property=”IsSelected” Value=”{Binding Content.IsSelected, Mode=TwoWay, RelativeSource={RelativeSource Self}}”/> <Setter Property=”Template”> <Setter.Value> <ControlTemplate TargetType=”ListBoxItem”> <ContentPresenter/> </ControlTemplate> </Setter.Value> </Setter> </Style> </ListBox.ItemContainerStyle>