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

WPF ListBox with a ListBox – UI Virtualization and Scrolling

It is possible to achieve smooth scrolling VirtualizingStackPanels in WPF 4.0 without sacrificing virtualization if you’re prepared to use reflection to access private functionality of the VirtualizingStackPanel. All you have to do is set the private IsPixelBased property of the VirtualizingStackPanel to true. Note that in .Net 4.5 there’s no need for this hack as … Read more

Binding Listbox to List in WinForms

You’re looking for the DataSource property: List<SomeType> someList = …; myListBox.DataSource = someList; You should also set the DisplayMember property to the name of a property in the object that you want the listbox to display. If you don’t, it will call ToString().

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

How to capture a mouse click on an Item in a ListBox in WPF?

I believe that your MouseLeftButtonDown handler is not called because the ListBox uses this event internally to fire its SelectionChanged event (with the thought being that in the vast majority of cases, SelectionChanged is all you need). That said, you have a couple of options. First, you could subscribe to the PreviewLeftButtonDown event instead. Most … Read more

Select ListBoxItem if TextBox in ItemTemplate gets focus

You can trigger on the property IsKeyboardFocusWithin in the ItemContainerStyle and set IsSelected to true. <ListBox.ItemContainerStyle> <Style TargetType=”{x:Type ListBoxItem}”> <Style.Triggers> <DataTrigger Binding=”{Binding IsKeyboardFocusWithin, RelativeSource={RelativeSource Self}}” Value=”True”> <DataTrigger.EnterActions> <BeginStoryboard> <Storyboard> <BooleanAnimationUsingKeyFrames Storyboard.TargetProperty=”(ListBoxItem.IsSelected)”> <DiscreteBooleanKeyFrame KeyTime=”0″ Value=”True”/> </BooleanAnimationUsingKeyFrames> </Storyboard> </BeginStoryboard> </DataTrigger.EnterActions> </DataTrigger> </Style.Triggers> </Style> </ListBox.ItemContainerStyle> You could also use a Setter instead of a single frame animation but … Read more