Get Name of Selected ListBoxItem on Button Click
I'm new to WPF/C#, and I'm trying to create a simple sql query application to get used to it. I have a listbox and a corresponding button in my XAML:
<ListBox Name="dbTables" Grid.Column="1" Grid.Row="2">
<ListBoxItem>Log</ListBoxItem>
<ListBoxItem>DownloadRequest</ListBoxItem>
<ListBoxItem>EmailRequest</ListBoxItem>
</ListBox>
<!-- View 开发者_C百科report button -->
<Button x:Name="myButton" Grid.Column="1" Grid.Row="3" Margin="0,10,0,0" Width="125" Height="25" HorizontalAlignment="Right" Click="Button_Click">View</Button>
and the corresponding C# function:
private void Button_Click(object sender, RoutedEventArgs e)
{
String curItem = dbTables.SelectedValue.ToString();
Console.WriteLine("CurItem = " + curItem);
Results resultsPage = new Results(curItem);
this.NavigationService.Navigate(resultsPage);
}
However, when it outputs the CurItem it has this value:
CurItem = System.Windows.Controls.ListBoxItem: Log
Which then throws an exception when I try to run a SQL Query. I'm trying to get it to just be
CurItem = Log
I've tried several different ways but I can't seem to just get the name of the selected value without the object definition attached.
SelectedItem
returns the currently selected item in the list box. Since you're populating your list box with ListBoxItem
s, that's what it will return. (Note, by the way, that your list box automatically generates ListBoxItem
containers for its items - if you look in the visual tree, you'll find that this ListBox
contains ListBoxItem
s, each of which contains a ListBoxItem
. SelectedItem
contains the content of the generated ListBoxItem
, which is to say the ListBoxItem
you're creating in markup.)
SelectedValue
returns the value of the property of SelectedItem
that is specified by ListBox.SelectedValuePath
. If no SelectedValuePath
is given, it returns SelectedItem
, so if you don't know about SelectedValuePath
, it seems like the two are the same thing. But if you populate your list with, say, Person
objects, and set SelectedValuePath
to "Name"
, the SelectedValue
will contain the selected person's name, not a reference to the Person
object.
So in your example, you can make SelectedValue
return the string by setting SelectedValuePath
to "Content"
, which is the property of the ListBoxItem
that contains the strings you're using.
You can do it another way by not explicitly creating ListBoxItem
s and just populating the ListBox
with strings. You have to declare a namespace referencing mscorlib
to do this, so that you can represent string objects in XAML, but once you do, the result's simple:
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib">
<DockPanel>
<ListBox DockPanel.Dock="Top" Margin="10" x:Name="test" SelectedValuePath="Length">
<sys:String>Log</sys:String>
<sys:String>DownloadRequest</sys:String>
<sys:String>EmailRequest</sys:String>
</ListBox>
<TextBlock DockPanel.Dock="Top" Margin="10" Text="{Binding ElementName=test, Path=SelectedItem}"/>
<TextBlock DockPanel.Dock="Top" Margin="10" Text="{Binding ElementName=test, Path=SelectedValue}"/>
</DockPanel>
</Page>
The Selected Value is a ListBoxItem, so you can cast the value to ListBoxItem, and then use the Content property:
ListBoxItem selItem = (ListBoxItem)dbTables.SelectedValue;
Console.WriteLine(selItem.Content);
String curItem = (dbTables.SelectedValue as ListBoxItem).Content.ToString();
精彩评论