.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> WPF >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Creating ListBox and Get the Data of Selected ListBox Item in WPF

Posted By:Syed Shakeer Hussain       Posted Date: January 14, 2011    Points: 75    Category: WPF    URL: http://www.dotnetspark.com  

This article show you how to create,bind static Data and get the ListBox selected Item.
 

Creating the ListBox in .XML

First drag and drop ListBox control from your ToolBox on a Window Form.Then add Items as follows to the ListBox 

<StackPanel Orientation="Horizontal" VerticalAlignment="Center">

<Label Content="Select Zone :" Width="80" Height="22" >Label>

<ListBox Height="60" Name="listBox1" Width="120">

<ListBoxItem Tag="1" Content="EAST">ListBoxItem>

<ListBoxItem Tag="2" Content="WEST">ListBoxItem>

<ListBoxItem Tag="3" Content="NORTH">ListBoxItem>

<ListBoxItem Tag="4" Content="SOUHT">ListBoxItem>

ListBox>

StackPanel>

Here 1)Content Attribute is used for storing displaying text in ListBox.

     2)Tag Attribute is used for storing value like a hidden field.

Below is the Image of ListBox Items

Get the Selected ListItem Text and Values of a ListBox:-

Here i am creating 2 textBoxes for showing selected Item of a ListBox in a TextBox as follows

<Label Content="Your Selected Zone:" Width="115"/>

<TextBox Name="txtZoneName" 

 Text="{Binding ElementName=listBox1,Path=SelectedItem.Content}"

 Width="80" Height="18"> TextBox>

<Label Content="Your Selected ZoneId:"/>

<TextBox Name="txtZoneId" Text="{Binding ElementName=listBox1,Path=SelectedItem.Tag}" Width="80" Height="18" >TextBox>

Displaying selectedItem Text of a listBox:-Set the SelectedItem.Content with a Path attribute in TextBox control

eg:-<TextBox Name="txtZoneName" 

Text="{Binding ElementName=listBox1,Path=SelectedItem.Content}"

Width="80" Height="18"> TextBox>

Displaying SelectedItem Values of a ListBox:-Set the SelectedItem.Tag with a Path attribute in a TextBox control as follows

<TextBox Name="txtZoneId" Text="{Binding ElementName=listBox1,Path=SelectedItem.Tag}" Width="80" Height="18" >TextBox>

Before diong this you have to set the Control Name of a ListBox by using ElementName attribute as follows in a TextBox Control.

Text="{Binding ElementName=listBox1}"

When you run the program the ListBox with SelectedItem looks as below.

 

Thanks for reading my article!

Shakeer


 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend