Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在C中获取在at列表框中选择的文本值#_C#_Listbox - Fatal编程技术网

C# 在C中获取在at列表框中选择的文本值#

C# 在C中获取在at列表框中选择的文本值#,c#,listbox,C#,Listbox,我有一个列表框 <Label Content="Report" HorizontalAlignment="Left" Height="47" Margin="36,75,0,0" VerticalAlignment="Top" Width="63"/> <ListBox x:Name="ListBox1" HorizontalA

我有一个列表框

  <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>
我也试过SelectedItem

string text = (string)ListBox1.SelectedItem;
但消息框始终为空。
这应该很简单,但我已经为此工作了几个小时,并尝试了stackoverflow上的每个建议或答案。大多数建议甚至都不会编译。例如:

  string selected = listBox1.GetItemText(listBox1.SelectedValue);
不会编译。找不到GetItemText。我正在使用VisualStudio17。“'ListBox不包含'GetItemText'的定义…”

有什么想法吗?请告知。谢谢

谢谢你的评论,查尔斯。我做到了。 玩得更远,现在我明白了


在标记中,
SelectedIndex
被设置为-1,这意味着没有选择。在这种情况下,
SelectedValue
SelectedItem
都返回null。您可以通过将
SelectedIndex
设置为介于0和3之间的值,或者通过准备代码来处理
SelectedValue
SelectedItem
中的空值来解决此问题,例如

string text = (ListBox1.SelectedItem as ListBoxItem)?.Content?.ToString();

这不会引发错误,因此用户可以在以后选择项目。通过选择,文本应按预期显示。

在标记中,
SelectedIndex
设置为-1,表示没有选择。在这种情况下,
SelectedValue
SelectedItem
都返回null。您可以通过将
SelectedIndex
设置为介于0和3之间的值,或者通过准备代码来处理
SelectedValue
SelectedItem
中的空值来解决此问题,例如

string text = (ListBox1.SelectedItem as ListBoxItem)?.Content?.ToString();

这不会引发错误,因此用户可以在以后选择项目。通过选择,文本应按预期显示。

如Charles May所示,您的XAML显示您的列表框位于另一个列表框中,这就是您出现错误的原因

被称为“ListBox\u SelectionChanged\u 1”的事件绑定到ListBox1内的ListBox对象,该对象未命名

我相信,您正在寻找的行为将如下所示:

XAML:


或者至少与此解决方案相近。

正如Charles May所指出的,您的XAML显示您的列表框位于另一个列表框中,这就是为什么会出现错误

被称为“ListBox\u SelectionChanged\u 1”的事件绑定到ListBox1内的ListBox对象,该对象未命名

我相信,您正在寻找的行为将如下所示:

XAML:



或者至少与此解决方案相近。

您的问题更多的是您的项目不包含在Listbox1中,而是包含在Listbox1中的第二个列表框中。删除原始示例中的第3行和第8行,看看这是否能让您更进一步。
string text=ListBox1.SelectedItem.ToString()或更好的
字符串文本=(ListBox1.Selecteditem作为ListBoxItem)。Content.ToString()
为您提供所选项目的内容。Charles May-您的评论与SharpNip的答案完全相同。我相信他的回答。你应该回答而不是评论!但我会投票支持你的评论!没问题,只是看到了这个问题,并认为一条评论就足以让您继续前进。您的问题更多的是您的项目不包含在Listbox1中,而是包含在Listbox1中的第二个listbox中。删除原始示例中的第3行和第8行,看看这是否能让您更进一步。
string text=ListBox1.SelectedItem.ToString()或更好的
字符串文本=(ListBox1.Selecteditem作为ListBoxItem)。Content.ToString()
为您提供所选项目的内容。Charles May-您的评论与SharpNip的答案完全相同。我相信他的回答。你应该回答而不是评论!但我会投票支持你的评论!没问题,只是看到了这个问题,并认为一条注释就足以让您继续前进。隐藏的代码结果是:System.InvalidCastException:“无法将类型为”System.Windows.Controls.ListBoxItem“的对象强制转换为类型为”System.String”。“对不起”,关于这一点,我正确编辑了响应。瞬间的大脑失误,万岁!它起作用了。字符串文本=((发件人作为列表框)?.SelectedItem作为列表框项)?.Content.ToString();但我的评论是想知道C#Microsoft的人在想什么?我的意思是,选择一个列表框不是很容易吗?StackOverflow主管是否允许在注释中添加意见注释?隐藏的代码结果是:System.InvalidCastException:“无法将类型为”System.Windows.Controls.ListBoxItem“的对象强制转换为类型为”System.String”。“抱歉”,关于这一点,我正确编辑了响应。瞬间的大脑失误,万岁!它起作用了。字符串文本=((发件人作为列表框)?.SelectedItem作为列表框项)?.Content.ToString();但我的评论是想知道C#Microsoft的人在想什么?我的意思是,选择一个列表框不是很容易吗?StackOverflow主管是否允许在注释中添加意见注释?我在标记中将SelectedIndex设置为0。首先,当我点击其中一个选择时,我会得到错误,因此当SelectionChanged被触发时,selectionIndex永远不应该是-1。另外,我还是将SelectIndex设置为0,但仍然不起作用。而且,你的建议也不适用。字符串文本=(ListBox1.SelectedItem作为ListBoxItem)?.text;“文本”加下划线。ListBoxItem不包含文本定义…我在标记中将SelectedIndex设置为0。首先,当我点击其中一个选择时,我会得到错误,因此当SelectionChanged被触发时,selectionIndex永远不应该是-1。另外,我还是将SelectIndex设置为0,但仍然不起作用。而且,你的建议也不适用。字符串文本=(ListBox1.SelectedItem作为ListBoxItem)?.text;“文本”加下划线。ListBoxItem不包含文本的定义。。。。
string text = (ListBox1.SelectedItem as ListBoxItem)?.Content?.ToString();
<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);
}