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>

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

Selecting a Textbox Item in a Listbox does not change the selected item of the listbox

We use the following style to set a PreviewGotKeyboardFocus which handles all events of TextBox control and ComboBoxes and such: <ListView.ItemContainerStyle> <Style TargetType=”ListViewItem”> <EventSetter Event=”PreviewGotKeyboardFocus” Handler=”SelectCurrentItem”/> </Style> </ListView.ItemContainerStyle> And then we select the row in code behind: protected void SelectCurrentItem(object sender, KeyboardFocusChangedEventArgs e) { ListViewItem item = (ListViewItem) sender; item.IsSelected = true; }

ItemContainerGenerator.ContainerFromItem() returns null?

I found something that worked better for my case in this StackOverflow question: Get row in datagrid By putting in UpdateLayout and a ScrollIntoView calls before calling ContainerFromItem or ContainerFromIndex, you cause that part of the DataGrid to be realized which makes it possible for it return a value for ContainerFromItem/ContainerFromIndex: dataGrid.UpdateLayout(); dataGrid.ScrollIntoView(dataGrid.Items[index]); var row … Read more

There is no ListBox.SelectionMode=”None”, is there another way to disable selection in a listbox?

Approach 1 – ItemsControl Unless you need other aspects of the ListBox, you could use ItemsControl instead. It places items in the ItemsPanel and doesn’t have the concept of selection. <ItemsControl ItemsSource=”{Binding MyItems}” /> By default, ItemsControl doesn’t support virtualization of its child elements. If you have a lot of items, virtualization can reduce memory … Read more

ListBox items return string, when DataTemplate is Button

You need FrameworkTemplate.FindName Method (String, FrameworkElement) for this purpose: private childItem FindVisualChild<childItem>(DependencyObject obj) where childItem : DependencyObject { for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++) { DependencyObject child = VisualTreeHelper.GetChild(obj, i); if (child != null && child is childItem) return (childItem)child; else { childItem childOfChild = FindVisualChild<childItem>(child); if (childOfChild != null) return childOfChild; } … Read more

Background color of a ListBox item (Windows Forms)

Thanks for the answer by Grad van Horck. It guided me in the correct direction. To support text (not just background color), here is my fully working code: //global brushes with ordinary/selected colors private SolidBrush reportsForegroundBrushSelected = new SolidBrush(Color.White); private SolidBrush reportsForegroundBrush = new SolidBrush(Color.Black); private SolidBrush reportsBackgroundBrushSelected = new SolidBrush(Color.FromKnownColor(KnownColor.Highlight)); private SolidBrush reportsBackgroundBrush1 = … Read more

WPF Binding a ListBox to an enum, displaying the Description Attribute

Yes, it is possible. This will do it. Say we have the enum public enum MyEnum { [Description(“MyEnum1 Description”)] MyEnum1, [Description(“MyEnum2 Description”)] MyEnum2, [Description(“MyEnum3 Description”)] MyEnum3 } Then we can use the ObjectDataProvider as xmlns:MyEnumerations=”clr-namespace:MyEnumerations” <ObjectDataProvider MethodName=”GetValues” ObjectType=”{x:Type sys:Enum}” x:Key=”MyEnumValues”> <ObjectDataProvider.MethodParameters> <x:Type TypeName=”MyEnumerations:MyEnum” /> </ObjectDataProvider.MethodParameters> </ObjectDataProvider> And for the ListBox we set the ItemsSource to … Read more

disable mouse wheel on itemscontrol in wpf

The answer you have referenced is exactly what is causing your problem, the ListBox (which is composed of among other things a ScrollViewer) inside your ScrollViewer catches the MouseWheel event and handles it, preventing it from bubbling and thus the ScrollViewer has no idea the event ever occurred. Use the following extremely simple ControlTemplate for … Read more