Plexo
asked on
How to get select value from listbox / Silverligh4
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;
}
I tried listBox1.SelectedItem, listBox1.SelectedValue, but it is not the right way.
Thanks.
<ListBox Height="100" HorizontalAlignment="Left"
<ListBoxItem Content="One" />
<ListBoxItem Content="Two" />
<ListBoxItem Content="Three" IsEnabled="False" />
</ListBox>
private void listBox1_SelectionChanged(
{
var x = listBox1.SelectedItem;
var y = listBox1.SelectedValue;
}
What Error is it giving? What is displayed?
I'm not sure, but maybe this could work:
var x = DirectCast(listBox1.Select edItem, ListBoxItem).Content.ToStr ing();
var x = DirectCast(listBox1.Select
see a sample to for the WPF list box.
as far as possible everything should be done using bindings in WPF/Silverlight.
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; }
}
ASKER
Hi TonyReba
There's no mistake, I just can not get the contents.
Thanks.
There's no mistake, I just can not get the contents.
Thanks.
ASKER
Hi joriszwaenepoel
var x = DirectCast(listBox1.Select edItem, ListBoxItem).Content.ToStr ing();
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.L istBoxItem ' is a 'type' but is used like a 'variable'
var x = DirectCast(listBox1.Select
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.L
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.