Search code examples
c#wpflistboxlistboxitem

The Listbox items initialize but once one is selected I don't know how to RESET the listbox where neither item is selected


If you run the code neither of the list box items are selected. If you select either it remains selected okay and displays either "One" or "Two" in the text box accordingly. When the ResetListBox button is clicked the selected item is deselected (?maybe) but retains a grey background (undesired). Once the item has this light grey background the onClick event no longer fires... No additional text is added to the text box. This question has been asked in various forms all over the web and none of the answers that I've tried in this simple example has worked.

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="279.186" Width="401.98">
    <Grid Margin="0,0,-6.8,-2.4">
        <ListBox x:Name="ThanklessListBox" HorizontalAlignment="Left" Height="89" Margin="24,25,0,0" VerticalAlignment="Top" Width="117">
            <ListBoxItem x:Name="ItemI" Content="ItemUno" Selected="ItemI_Selected"/>
            <ListBoxItem x:Name="Item2" Content="ItemDos" Selected="Item2_Selected"/>
        </ListBox>
        <TextBox x:Name="StuffToShow" HorizontalAlignment="Left" Height="178" Margin="198,25,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="116"/>
        <Button x:Name="ResetListBox" Content="ResetListBox" HorizontalAlignment="Left" Height="26" Margin="28,131,0,0" VerticalAlignment="Top" Width="116" Click="ResetListBox_Click"/>
        <Button x:Name="SeleectButton" Content="SelectItemDos" HorizontalAlignment="Left" Height="24" Margin="28,179,0,0" VerticalAlignment="Top" Width="116" Click="SeleectButton_Click"/>

    </Grid>
</Window>


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void ItemI_Selected(object sender, RoutedEventArgs e)
        {
            StuffToShow.Text += "\nOne";
        }

        private void Item2_Selected(object sender, RoutedEventArgs e)
        {
            StuffToShow.Text += "\nTwo";
        }

        private void SeleectButton_Click(object sender, RoutedEventArgs e)
        {
            ThanklessListBox.SelectedItem = 1; //Choose Dos
        }

        private void ResetListBox_Click(object sender, RoutedEventArgs e)
        {
            ThanklessListBox.SelectedItem = -1; //Deselect
        }
    }

Solution

  • In your code behind, you are using SelectedItem with an integer.

        private void ResetListBox_Click(object sender, RoutedEventArgs e)
        {
            ThanklessListBox.SelectedItem = -1; //Deselect
        }
    

    Try either using SelectedIndex

        private void ResetListBox_Click(object sender, RoutedEventArgs e)
        {
            ThanklessListBox.SelectedIndex = -1; //Deselect
        }
    

    Or Selected Item with null

    Edited to add: You could also do

    ThanklessListBox.SelectedItems.Clear 
    

    which is what I think the other person whose post is now deleted meant>

    In fairness, this is something you could really learn just by browsing the online documentation for ListBox on the MS site