C# 需要将我的枚举插入C中的组合框中#

C# 需要将我的枚举插入C中的组合框中#,c#,enums,C#,Enums,我需要帮助将枚举中的信息放入组合框 以下是我的枚举的代码: namespace Arsalan_Salam_991571527_A2 { public enum CarType { Odyssey, Rouge, Sienna, Accord } } 我发现了一些本应可以工作的代码,并尝试在代码中实现,以使enum中的信息如下所示: private void AddingEnumIntoCombo

我需要帮助将枚举中的信息放入组合框

以下是我的枚举的代码:

namespace Arsalan_Salam_991571527_A2
{
    public enum CarType
    {
        Odyssey,
        Rouge,
        Sienna,
        Accord
     }
 }
我发现了一些本应可以工作的代码,并尝试在代码中实现,以使enum中的信息如下所示:

   private void AddingEnumIntoComboBox(Car c)
    {
        foreach (var item in Enum.GetValues(typeof(CarType)))
        {
            carTypeInput.Items.Add(item);
        }
    }
但由于某些原因,该程序工作正常,但此代码没有将我的枚举信息显示到名为carTypeInput的组合框中。这是大学作业

以下是我用来创建UI界面的xaml:

<Page
x:Class="Arsalan_Salam_991571527_A2.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Arsalan_Salam_991571527_A2"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid RenderTransformOrigin="0.497,0.522">
    <TextBlock HorizontalAlignment="Left" Margin="572,10,0,0" Text="DriveWell Inc." TextAlignment="Center" FontSize="50" VerticalAlignment="Top" Width="374" Height="72"/>
    <TextBlock HorizontalAlignment="Left" Margin="87,88,0,0" Text="Vin Number" TextAlignment="Center" FontSize="30" VerticalAlignment="Top" Width="192" Height="67" RenderTransformOrigin="0.457,-0.751"/>
    <TextBlock HorizontalAlignment="Left" Margin="67,185,0,0" Text="Car Make" TextAlignment="Center" FontSize="30" VerticalAlignment="Top" Width="194" Height="63"/>
    <TextBlock HorizontalAlignment="Left" Margin="87,282,0,0" Text="Car Type" TextAlignment="Center" FontSize="30" VerticalAlignment="Top" Width="183" Height="61"/>
    <TextBlock HorizontalAlignment="Left" Text="Purchase Price"  TextAlignment="Center" FontSize="30" VerticalAlignment="Top" Margin="87,380,0,0" Width="226" Height="61" RenderTransformOrigin="3.948,-0.233"/>
    <TextBlock HorizontalAlignment="Left" Margin="87,487,0,0" Text="Model Year" TextAlignment="Center" FontSize="30" VerticalAlignment="Top" Height="65" Width="190" RenderTransformOrigin="3.283,-2.555"/>
    <TextBlock HorizontalAlignment="Left" Margin="87,584,0,0" Text="Mileage (Km)" TextAlignment="Center" FontSize="30" VerticalAlignment="Top" Height="43" Width="192"/>
    <Button x:Name="addingCar" Click="addingCar_Click" Content="Add Car" FontSize="30" Margin="43,639,0,0" VerticalAlignment="Top" Height="56" Width="156"/>
    <Button x:Name="clearing" Click="clearing_Click" Content="Clear" FontSize="30" Margin="224,639,0,0" VerticalAlignment="Top" Height="56" Width="134"/>
    <Button x:Name="updatingCar" Click="updatingCar_Click" Content="Update" FontSize="30" Margin="379,639,0,0" VerticalAlignment="Top" Height="56" Width="130"/>
    <ComboBox x:Name="carTypeInput" Margin="348,282,0,0" Width="191" Height="57"/>
    <ComboBox x:Name="modelYearInput" Margin="348,483,0,0" Width="191" Height="52"/>
    <TextBox x:Name="vinNumberInput" HorizontalAlignment="Left" Margin="348,88,0,0" Text="" FontSize="25"  VerticalAlignment="Top" Height="40" Width="191" RenderTransformOrigin="0.476,-1.383"/>
    <TextBox x:Name="carMakeInput" HorizontalAlignment="Left" Margin="348,185,0,0" Text="" FontSize="25"  VerticalAlignment="Top" Height="40" Width="191"/>
    <TextBox x:Name="purchasePriceInput" HorizontalAlignment="Left" Margin="348,380,0,0" Text="" FontSize="25"  VerticalAlignment="Top" Height="52" Width="191"/>
    <TextBox x:Name="mileageInput" HorizontalAlignment="Left" Margin="348,584,0,0" Text="" FontSize="15"  VerticalAlignment="Top" Height="32" Width="191"/>
    <Image x:Name="carImageOutput" HorizontalAlignment="Left" Height="429" Margin="1013,106,0,0" VerticalAlignment="Top" Width="226"/>
    <TextBlock x:Name="errorMessageOutput" HorizontalAlignment="Left" Margin="572,624,0,0" Text="" FontSize="35" VerticalAlignment="Top" Width="641" Height="62"/>
    <ListView x:Name="lstCarDetailOutput" Margin="572,88,315,120"></ListView>

</Grid>
</Page>

jdweng在他们的评论中指出了这一点,但我将对此进行详细阐述并给出答案:

问题是
Enum.GetValues
返回枚举的值,这是一种整数类型(目前C#Enum或多或少是一个围绕一组常量的花哨包装器)。默认情况下,这是一个
int
(更多)。这意味着您对
Enum.GetValues(typeof(CarType))
的调用正在返回
int[]
。现在有多种方法可以获取枚举值的名称,我将演示两种方法

  • 将整数转换回枚举值并调用
    ToString
  • foreach(Enum.GetValues中的变量项(typeof(CarType))
    {
    //这可以写成“carTypeInput.Items.Add(((CarType)item.ToString());”
    var carType=(carType)项;
    carTypeInput.Items.Add(carType.ToString());
    }
    
  • 使用
    Enum.GetName
    避免必须获取
    CarType的实例
  • foreach(Enum.GetValue中的变量项(typeof(CarType))
    {
    //这可以写成“carTypeInput.Items.Add(Enum.GetName(typeof(CarType),item));
    var carTypeName=Enum.GetName(typeof(CarType),item);
    carTypeInput.Items.Add(carTypeName);
    }
    
    为什么不使用Enum.GetNames

    private void Form1_Load(object sender, EventArgs e)
    {
        comboBox1.Items.AddRange(Enum.GetNames(typeof(CarType)));
    }
    
    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string carTypeName = comboBox1.SelectedItem.ToString();
        
        if(carTypeName == CarType.Accord.ToString())
        {
        ...
        }
    }
    

    是否为
    组合框设置了
    ItemsSource
    ?如果设置了,则应将其删除,因为这与手动添加项冲突。是否要枚举的名称或值(为整数)在combobox中?我需要combobox中枚举的名称。您是否在代码中的任何位置调用方法
    AddingEnumIntoComboBox
    ?不,我只是使用此方法将枚举中的名称添加到名为CartypeInput的combobox中。它仍然没有在combobox中显示枚举值。我仍然看到combobox空白。什么GUI框架k您是在使用?WPF或WinForms还是其他什么?我正在使用UWP空白应用程序(通用Windows)来创建此应用程序,因此您可以使用XAML来创建UI。您可以编辑原始问题以包含组合框的定义吗?当然可以,请想一想,给我一秒钟时间添加我用于创建UI界面的所有XAML代码