在 C# 中获取在 ListBox 中选择的文本值
Getting the text value selected in at ListBox in C#
我有一个列表框
<Label Content="Report" HorizontalAlignment="Left" Height="47" Margin="36,75,0,0" VerticalAlignment="Top" Width="63"/>
<ListBox x:Name="ListBox1" HorizontalAlignment="Left" Height="121" Margin="84,75,0,0" VerticalAlignment="Top" Width="102" SelectionChanged="ListBox_SelectionChanged" SelectedIndex="-1">
<ListBox Height="100" Width="100" SelectionChanged="ListBox_SelectionChanged_1">
<ListBoxItem x:Name="ListBoxFAT" Content="FAT"/>
<ListBoxItem x:Name="ListBoxNUMI" Content="NUMI"/>
<ListBoxItem x:Name="ListBoxSSID" Content="SSID"/>
<ListBoxItem x:Name="ListBoxFact" Content="FACT"/>
</ListBox>
</ListBox>
这是通过从工具栏中拖动列表框图标创建的。我添加了项目及其价值。
现在我试图只获取所选项目的文本值。
private void ListBox_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
{
string text = (string)ListBox1.SelectedValue;
MessageBox.Show(text);
我也试过SelectedItem
string text = (string)ListBox1.SelectedItem;
但是消息框总是空白
这应该很简单,但我已经为此工作了几个小时,并尝试了 Whosebug 上的每一个建议或答案。大多数建议甚至不编译。例如:
string selected = listBox1.GetItemText(listBox1.SelectedValue);
不会编译。未找到 GetItemText。我正在使用 Visual Studio 17。“'ListBox 不包含 'GetItemText' 的定义...”
有什么想法吗?请指教。谢谢。
感谢您的评论,查尔斯。我做到了。
继续玩,现在我得到
System.InvalidCastException: 'Unable to cast object of type 'System.Windows.Controls.ListBoxItem' to type 'System.String'.'
string text = (string)ListBox1.SelectedItem;
在标记中,SelectedIndex
设置为 -1,这意味着没有 selection。在这种情况下,SelectedValue
和 SelectedItem
都 return 为空。您可以通过将 SelectedIndex
设置为 0 到 3 之间的值或准备代码来处理 SelectedValue
和 SelectedItem
中的空值来解决此问题,例如
string text = (ListBox1.SelectedItem as ListBoxItem)?.Content?.ToString();
这不会引发错误,因此用户之后可以 select 一个项目。使用 selection,文本应按预期显示。
正如 Charles May 所指出的,您的 XAML 表明您的 ListBox 在另一个 ListBox 中,这就是您收到错误的原因..
正在调用的事件"ListBox_SelectionChanged_1"绑定到ListBox1 中的未命名的ListBox 对象。
我相信您正在寻找的行为会像这样得到解决:
XAML:
<Label Content="Report" HorizontalAlignment="Left" Height="47" Margin="36,75,0,0" VerticalAlignment="Top" Width="63"/>
<ListBox x:Name="ListBox1" HorizontalAlignment="Left" Height="121" Margin="84,75,0,0" VerticalAlignment="Top" Width="102" SelectionChanged="ListBox_SelectionChanged" SelectedIndex="-1">
<ListBoxItem x:Name="ListBoxFAT" Content="FAT"/>
<ListBoxItem x:Name="ListBoxNUMI" Content="NUMI"/>
<ListBoxItem x:Name="ListBoxSSID" Content="SSID"/>
<ListBoxItem x:Name="ListBoxFact" Content="FACT"/>
</ListBox>
隐藏代码:
private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
string text = ((sender as ListBox)?.SelectedItem as ListBoxItem)?.Content.ToString();
MessageBox.Show(text);
}
或者至少接近这个解决方案。
我有一个列表框
<Label Content="Report" HorizontalAlignment="Left" Height="47" Margin="36,75,0,0" VerticalAlignment="Top" Width="63"/>
<ListBox x:Name="ListBox1" HorizontalAlignment="Left" Height="121" Margin="84,75,0,0" VerticalAlignment="Top" Width="102" SelectionChanged="ListBox_SelectionChanged" SelectedIndex="-1">
<ListBox Height="100" Width="100" SelectionChanged="ListBox_SelectionChanged_1">
<ListBoxItem x:Name="ListBoxFAT" Content="FAT"/>
<ListBoxItem x:Name="ListBoxNUMI" Content="NUMI"/>
<ListBoxItem x:Name="ListBoxSSID" Content="SSID"/>
<ListBoxItem x:Name="ListBoxFact" Content="FACT"/>
</ListBox>
</ListBox>
这是通过从工具栏中拖动列表框图标创建的。我添加了项目及其价值。
现在我试图只获取所选项目的文本值。
private void ListBox_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
{
string text = (string)ListBox1.SelectedValue;
MessageBox.Show(text);
我也试过SelectedItem
string text = (string)ListBox1.SelectedItem;
但是消息框总是空白
这应该很简单,但我已经为此工作了几个小时,并尝试了 Whosebug 上的每一个建议或答案。大多数建议甚至不编译。例如:
string selected = listBox1.GetItemText(listBox1.SelectedValue);
不会编译。未找到 GetItemText。我正在使用 Visual Studio 17。“'ListBox 不包含 'GetItemText' 的定义...”
有什么想法吗?请指教。谢谢。
感谢您的评论,查尔斯。我做到了。 继续玩,现在我得到
System.InvalidCastException: 'Unable to cast object of type 'System.Windows.Controls.ListBoxItem' to type 'System.String'.' string text = (string)ListBox1.SelectedItem;
在标记中,SelectedIndex
设置为 -1,这意味着没有 selection。在这种情况下,SelectedValue
和 SelectedItem
都 return 为空。您可以通过将 SelectedIndex
设置为 0 到 3 之间的值或准备代码来处理 SelectedValue
和 SelectedItem
中的空值来解决此问题,例如
string text = (ListBox1.SelectedItem as ListBoxItem)?.Content?.ToString();
这不会引发错误,因此用户之后可以 select 一个项目。使用 selection,文本应按预期显示。
正如 Charles May 所指出的,您的 XAML 表明您的 ListBox 在另一个 ListBox 中,这就是您收到错误的原因..
正在调用的事件"ListBox_SelectionChanged_1"绑定到ListBox1 中的未命名的ListBox 对象。
我相信您正在寻找的行为会像这样得到解决:
XAML:
<Label Content="Report" HorizontalAlignment="Left" Height="47" Margin="36,75,0,0" VerticalAlignment="Top" Width="63"/>
<ListBox x:Name="ListBox1" HorizontalAlignment="Left" Height="121" Margin="84,75,0,0" VerticalAlignment="Top" Width="102" SelectionChanged="ListBox_SelectionChanged" SelectedIndex="-1">
<ListBoxItem x:Name="ListBoxFAT" Content="FAT"/>
<ListBoxItem x:Name="ListBoxNUMI" Content="NUMI"/>
<ListBoxItem x:Name="ListBoxSSID" Content="SSID"/>
<ListBoxItem x:Name="ListBoxFact" Content="FACT"/>
</ListBox>
隐藏代码:
private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
string text = ((sender as ListBox)?.SelectedItem as ListBoxItem)?.Content.ToString();
MessageBox.Show(text);
}
或者至少接近这个解决方案。