C# 如何将combo.SelectedItem作为字符串发送到函数 >在C++ CLI中有以下函数: void receiveData(String^ data) { }
我从C#调用函数如下:C# 如何将combo.SelectedItem作为字符串发送到函数 >在C++ CLI中有以下函数: void receiveData(String^ data) { },c#,wpf,c++-cli,C#,Wpf,C++ Cli,我从C#调用函数如下: if (e.OriginalSource is ComboBox) { ComboBox combo = e.OriginalSource as ComboBox; if (combo.SelectedItem != null) { receiveData(combo.SelectedItem as string); } } 但在函数内部,字符串参数始终未定义。 将SelectedItem作为字符串传递给my函数的正确方法是什么 谢谢。Sel
if (e.OriginalSource is ComboBox)
{
ComboBox combo = e.OriginalSource as ComboBox;
if (combo.SelectedItem != null)
{
receiveData(combo.SelectedItem as string);
}
}
但在函数内部,字符串参数始终未定义。将SelectedItem作为字符串传递给my函数的正确方法是什么
谢谢。
SelectedItem
返回所选对象。如果需要文本,请使用选择editem.ToString()
。但请注意为空的选定项,这可能更好:
string selectedText = combo.SelectedItem == null ? string.Empty : combo.SelectedItem.ToString()
如果不将字符串添加到项目列表中,则在变量上使用
as
运算符并尝试将其转换为非类型时,将获得null
。组合框的SelectedItem是否为字符串?这取决于具体情况。如果组合框的TextBox是可编辑的,SelectedText将返回TextBox中的选定字符。它不可编辑,但我没有此属性:SelectedText