Solved

How to get select value from listbox / Silverligh4

Posted on 2010-09-14
7
488 Views
Last Modified: 2013-11-12
How  can I get the selected value from listbox.
I tried listBox1.SelectedItem, listBox1.SelectedValue, but it is not the right way.
Thanks.


 <ListBox Height="100" HorizontalAlignment="Left" Margin="21,266,0,0"      Name="listBox1" VerticalAlignment="Top" Width="556">
            <ListBoxItem Content="One" />
            <ListBoxItem Content="Two" />
            <ListBoxItem Content="Three" IsEnabled="False" />
        </ListBox>

 private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var x = listBox1.SelectedItem;
            var y = listBox1.SelectedValue;
        }

0
Comment
Question by:Plexo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 9

Expert Comment

by:TonyReba
ID: 33678764
What Error is it giving? What is displayed?
0
 
LVL 10

Expert Comment

by:joriszwaenepoel
ID: 33679364
I'm not sure, but maybe this could work:

var x = DirectCast(listBox1.SelectedItem, ListBoxItem).Content.ToString();
0
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 33679994
see a sample to for the WPF list box.
as far as possible everything should be done using bindings in WPF/Silverlight.
<ListBox ItemsSource="{Binding ListBoxItems}" SelectedItem="{Binding SelectedItem}" SelectionChanged="ListBox_SelectionChanged">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Content}"/>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

// code behind

public partial class Window2 : Window
    {
        DataContext context;
        public Window2()
        {
            InitializeComponent();
            context = new DataContext();
            this.DataContext = context;
        }

        private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            MessageBox.Show("Selected item is : " + context.SelectedItem.Content);
        }
    }

    public class DataContext
    {
        public ObservableCollection<Item> ListBoxItems { get; set; }
        public Item SelectedItem { get; set; }
        public DataContext()
        {
            ListBoxItems = new ObservableCollection<Item>();
            ListBoxItems.Add(new Item()
            {
                Content = "Test 1"
            });

            ListBoxItems.Add(new Item()
            {
                Content = "Test 2"
            });

            ListBoxItems.Add(new Item()
            {
                Content = "Test 3"
            });

            ListBoxItems.Add(new Item()
            {
                Content = "Test 4"
            });
        }
    }

    public class Item
    {
        public string Content { get; set; }
    }

Open in new window

0
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 

Author Comment

by:Plexo
ID: 33683929
Hi TonyReba
There's no mistake, I just can not get the contents.
Thanks.
0
 

Author Comment

by:Plexo
ID: 33683988
Hi joriszwaenepoel

var x = DirectCast(listBox1.SelectedItem, ListBoxItem).Content.ToString();
I used your code and I had the following error:

Error      1      The name 'DirectCast' does not exist in the current context
Error      2      'System.Windows.Controls.ListBoxItem' is a 'type' but is used like a 'variable'
0
 
LVL 9

Expert Comment

by:TonyReba
ID: 33684466
I'm not too good on c# but try, .        

Dim x as listboxitem = ctype (listbox1.selectedvalue, listboxitem).tostring()

 Then use x as your variable.

Note Google telerik code converter if u need convert to c #  this is vb                          
0
 
LVL 29

Accepted Solution

by:
Gautham Janardhan earned 500 total points
ID: 33685100
in C# that would be

((ListBoxItem)listBox1.SelectedItem).Content.Tostring()
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question