WPF Get selected item from ListBox in VB.NET

In this article you will see how to get selected item from ListBox in WPF Application.
  • 3745

The ListBox control displays a list of items. The user can select one or multiple items depending on the selection mode. Here we will create an application in which, I have a ListBox with a list of colors, a TextBox, and a Ellipse.

When we pick a color from the ListBox, the text of TextBox and color of Ellipse changes dynamically to the color selected in the ListBox. Lets see the following XAML code snippet:


Xaml Code

<
Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        x:Class="WpfApplication1.CompileXamlWindow.CompileXamlWindow"
        Title="Compile XAML Window"
        SizeToContent="WidthAndHeight"
        ResizeMode="CanMinimize">
    <StackPanel Orientation="Vertical">
 
        <TextBlock Margin="10,10,10,10" FontWeight="Bold">
          Pick a color from below list 
        
</TextBlock>
 
        <ListBox Name="mcListBox" Height="100" Width="100"
             Margin="10,10,0,0" HorizontalAlignment="Center">
            <ListBoxItem>Orange</ListBoxItem>
            <ListBoxItem>Green</ListBoxItem>
            <ListBoxItem>Blue</ListBoxItem
            <ListBoxItem>Gray</ListBoxItem
            <ListBoxItem>LightGray</ListBoxItem>
            <ListBoxItem>Red</ListBoxItem>
        </ListBox>
        <TextBox Height="23" Name="textBox1" Width="120" Margin="10,10,0,0" HorizontalAlignment="Center" >
            <TextBox.Text>
                <Binding ElementName="mcListBox" Path="SelectedItem.Content"/>
            </TextBox.Text>
 
       </TextBox>
        <Ellipse Stroke="Black" StrokeThickness="1"
                 Height="115" Width="148" ToolTip="An author.">
                 <Ellipse.Fill>
                <Binding ElementName="mcListBox" Path="SelectedItem.Content"/>
            </Ellipse.Fill>
        </Ellipse>
    </StackPanel>
</
Window>

The output of the above code looks like:

listbox.gif

Summary


In this article, I discussed how to select item from ListBox. I hope you will enjoy this article.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.