How can I sort a ListBox using only XAML and no code-behind?

Use a CollectionViewSource: <CollectionViewSource x:Key=”SortedItems” Source=”{Binding CollectionOfStrings}” xmlns:scm=”clr-namespace:System.ComponentModel;assembly=Win‌​dowsBase”> <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.

How to bind Dictionary to ListBox in WinForms

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

C# : changing listbox row color?

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; } } } }

How to change ListBox selection background color?

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

Numbered listbox

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

How to disable highlighting on listbox but keep selection?

<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>