简体   繁体   English

从使用外部项目C#WPF的ComboBox获取价值

[英]Get Value From ComboBox that uses external items C# WPF

So I am creating a program which has several comboboxes for a user to select an option from. 因此,我正在创建一个具有多个组合框的程序,供用户从中选择一个选项。 My aim was to populate the comboboxes from an API I have created, which I have succeeded with. 我的目标是从我创建的API中填充组合框,该API已成功实现。 I have used an Enum in my API containing everything I want to appear in my ComboBox. 我在我的API中使用了一个Enum,其中包含我想出现在ComboBox中的所有内容。

Now, I'm wanting to put all my data from my comboboxes and text boxes, into an array. 现在,我想将组合框和文本框中的所有数据放入一个数组中。

I cannot seem to get the selected item of the comboboxes! 我似乎无法获得组合框的选定项! I have tried: 我努力了:

String s = comboBox1.Text;

but '.Text' is not an option for me to use and neither is '.SelectedItem' 但是“ .Text”不是我可以使用的选项,也不是“ .SelectedItem”

What should I use? 我应该使用什么? Thank you! 谢谢!

SelectedValue should work SelectedValue应该工作

<Window x:Class="WpfApplication3.MainWindow"
        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"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ComboBox Name="comboBox1" Margin="0,0,0,64">
            <ComboBox.Items>
                <sys:String>a</sys:String>
                <sys:String>b</sys:String>
                <sys:String>c</sys:String>
                <sys:String>d</sys:String>
                <sys:String>e</sys:String>
            </ComboBox.Items>
        </ComboBox>
        <TextBox Height="23" Margin="10,0,10,22" TextWrapping="Wrap" Text="{Binding SelectedValue, ElementName=comboBox1}" VerticalAlignment="Bottom"/>
    </Grid>
</Window>

Create a SelectionChanged event, then in code: 创建一个SelectionChanged事件,然后在代码中:

private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
   ComboBox comboBox = sender as ComboBox;
   string value = comboBox.SelectedItem.ToString();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM