繁体   English   中英

需要将我的枚举插入 C# 中的 combobox

[英]Need to get my enum inserted into a combobox in C#

我需要帮助将我的枚举中的信息放入 combobox。

这是我的枚举的代码:

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 的 combobox 中。 这是为了大学作业。

这是我用来创建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# 枚举或多或少是一堆常数的精美包装)。 默认情况下,这是一个int (更多here )。 这意味着您对Enum.GetValues(typeof(CarType))的调用正在返回int[] 现在有多种方法可以获取枚举值的名称,我将演示两种。

  1. 将 int 转换回枚举值并调用ToString
foreach (var item in Enum.GetValues(typeof(CarType))
{
    // This can be written as 'carTypeInput.Items.Add(((CarType) item).ToString());'
    var carType = (CarType) item;
    carTypeInput.Items.Add(carType.ToString());
}
  1. 使用Enum.GetName避免必须获取CarType的实例
foreach (var item in Enum.GetValue(typeof(CarType))
{
    // This can be written as '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())
    {
    ...
    }
}

暂无
暂无

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

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