c#
ListBox filled with binding doesn't select item on click
I'm trying to use a ListBox to choose an entry and then display a picture belonging to this selected entry. But just at the beginning I got my first problem: filling the ListBox with binding is working, but if I click on one line in my running program, it doesn't select the line. I can just see the highlighted hover effect, but not select a line. Any ideas what my mistake could be? This is my XAML: <ListBox x:Name="entrySelection" ItemsSource="{Binding Path=entryItems}" HorizontalAlignment="Left" Height="335" Margin="428,349,0,0" VerticalAlignment="Top" Width="540" FontSize="24"/> And in MainWindow.xaml.cs I'm filling the ListBox with entries: private void fillEntrySelectionListBox() { //Fill listBox with entries for active user DataContext = this; entryItems = new ObservableCollection<ComboBoxItem>(); foreach (HistoryEntry h in activeUser.History) { var cbItem = new ComboBoxItem(); cbItem.Content = h.toString(); entryItems.Add(cbItem); } this.entrySelection.ItemsSource = entryItems; labelEntrySelection.Text = "Einträge für: " + activeUser.Id; //show image matching the selected entry if (activeUser.History != null) { int index = entrySelection.SelectedIndex; if (index != -1 && index < activeUser.History.Count) { this.entryImage.Source = activeUser.History[index].Image; } } } So I can see my ListBox correctly filled, but not select anything - so I can't go on with loading the picture matching the selected entry. I'm still quite new to programming, so any help would be great :) EDIT: If someone takes a look at this thread later: here's the - quite obvious -solution XAML now looks like this <ListBox x:Name="entrySelection" ItemsSource="{Binding Path=entryItems}" HorizontalAlignment="Left" Height="335" Margin="428,349,0,0" VerticalAlignment="Top" Width="540" FontFamily="Siemens sans" FontSize="24"> <ListBox.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding Text}"/> </DataTemplate> </ListBox.ItemTemplate> </ListBox> Code behind to fill it: //Fill listbox with entries for selected user DataContext = this; entryItems = new ObservableCollection<DataItem>(); foreach (HistoryEntry h in selectedUser.History) { var lbItem = new DataItem(h.toString()); entryItems.Add(lbItem); } this.entrySelection.ItemsSource = entryItems; labelEntrySelection.Text = "Einträge für: " + selectedUser.Id; And new Class DataItem: class DataItem { private String text; public DataItem(String s) { text = s; } public String Text { get { return text; } } }
You are filling it with ComboBoxItem, which is not relevant to the ListBox, and also wrong by definition. You need to have the ObservableCollection filled with data items. Meaning, make a class that contains the data you want to store, and the ListBox will generate a ListBoxItem automatically per data item. http://www.wpf-tutorial.com/list-controls/listbox-control/
Related Links
How do I hide a directory even if the show hidden files is set?
Mouse Move and click use Rows and Column
Execute multiple SQL select queries
navigation issue : PopToRootAsync is not supported globally on iOS, please use a NavigationPage
How to handle id element when mapping mongoDB document to C# class
MVVM - How to use a given REST-API dll
Select unique IDs from two DataTables with three queries and merge
Get All Files Inside Directories with WinForms C# [duplicate]
When I upload image then it sent me error from views I don't know how to solve it?
gRPC - request response performance
Stored procedure not found
ContinuousRecognitionSession.StartAsync() return “Access is denied”
Creating Multiple Attachments from byte[] to MemoryStream in C#
how to write an EF query to join three tables and order by a field?
print in one column with a Propertyblock
visual studio C# combo box event