#982 – Executing Code When Selected Items in a ListBox Change

You can handle the SelectionChanged event for a ListBox to react to a change in the currently selected items.

Within the event handler, you can inspect the following properties of the SelectionChangedEventArgs parameter:

  • AddedItems – Collection of items just selected, type matches type of object in collection that ListBox is bound to
  • AddedInfos – Can get index and ListBoxItem for added items
  • RemovedItems – Collection of items just unselected
  • RemovedInfos – Get index and ListBoxItem for removed items
        <ListBox Name="lbActors" Margin="15" Width="200" Height="190"
                 ItemsSource="{Binding ActorList}"
                 SelectionMode="Extended"
                 SelectionChanged="lbActors_SelectionChanged"/>
        <TextBlock Name="tbInfo"/>

 

        private void lbActors_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            StringBuilder sbInfo = new StringBuilder();

            sbInfo.AppendLine("Added:");
            foreach (object o in e.AddedItems)
            {
                Actor a = o as Actor;
                sbInfo.AppendLine(a.FullName);
            }

            sbInfo.AppendLine("Removed:");
            foreach (object o in e.RemovedItems)
            {
                Actor a = o as Actor;
                sbInfo.AppendLine(a.FullName);
            }

            tbInfo.Text = sbInfo.ToString();
        }

982-001
982-002
982-003

#936 – TextBox Properties that Reflect Currently Selected Text

The following properties of a TextBox are updated whenever the user changes the currently selected text in a TextBox.

  • SelectedText – the currently selected text (string)
  • SelectionStart – 0-based index into the Text property for the start of the selection  (int)
  • SelectionLength – number of characters selected (int)

The SelectionChanged event will fire whenever the currently selected text changes.

Below, we wire up the SelectionChanged event, capture the values of these properties, and then bind to those values.

        <TextBox Margin="5" Height="100"
            Text="{Binding SomeText}"
            TextWrapping="Wrap"
            VerticalScrollBarVisibility="Auto"
            SelectionChanged="TextBox_SelectionChanged"/>
        <Label Content="{Binding SelectedText}"/>
        <StackPanel Orientation="Horizontal">
            <Label Content="{Binding SelectionStart}"/>
            <Label Content="{Binding SelectionLength}"/>
        </StackPanel>
        private void RaisePropertyChanged(string propName)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propName));
        }

        public string SelectedText { get; set; }
        public int SelectionStart { get; set; }
        public int SelectionLength { get; set; }

        private void TextBox_SelectionChanged(object sender, RoutedEventArgs e)
        {
            SelectedText = ((TextBox)sender).SelectedText;
            SelectionStart = ((TextBox)sender).SelectionStart;
            SelectionLength = ((TextBox)sender).SelectionLength;

            RaisePropertyChanged("SelectedText");
            RaisePropertyChanged("SelectionStart");
            RaisePropertyChanged("SelectionLength");
        }

936-001