简体   繁体   English

在 C# 中使用数组填充 ComboBox

[英]Populating a ComboBox with a Array in C#

My Array is a list of Pokemon Names read from a text file and then stored into an Array in the PokemonData class seen below我的数组是从文本文件中读取的 Pokemon Names 列表,然后存储到 PokemonData 类中的数组中,如下所示

        private string[] pokemonNames;
        private StreamReader readNames;

        public PokemonData()
        {
            readNames = new StreamReader(setDirectory() + @".\PokemonNames.txt");
            pokemonNames = new string[256];
            populateArray(pokemonNames, readNames);

        }

        public string[] populateArray(string[] pokemonNames, StreamReader readNames)
        {
            string pokemonName = readNames.ReadLine();
            int i = 0;
            while (pokemonName != null)
            {
                pokemonNames[i] = pokemonName.Trim();
                pokemonName = readNames.ReadLine();
                i++;
            }
            readNames.Close();
            return pokemonNames;
        }

        public string[] getPokemonNames()
        {
            return pokemonNames;
        }

What I want to do is now populate an Combobox using WPF with all the names inside the array.我现在想要做的是使用 WPF 使用数组中的所有名称填充组合框。 I have tried googling this and frequently alot of the answers have classes setup much like this:我试过用谷歌搜索这个,经常有很多答案的课程设置很像这样:

Class ExampleClass {
    Public ExampleClass() {
         string PokemonName; {get; set;}
     }
}

I believe there is an assignment going on here, but I am unsure.我相信这里有一项任务正在进行,但我不确定。 C# isn't my usual language and this is my first time creating a gui. C# 不是我常用的语言,这是我第一次创建 gui。 Could someone please guide me through so I could finish this.有人可以指导我完成这个,所以我可以完成这个。

I have tried doing a handful of things such as the code below and Databinding.我尝试过做一些事情,例如下面的代码和数据绑定。 At this point I believe I am missing something.在这一点上,我相信我错过了一些东西。

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:StarterEdit"
        xmlns:Collections="clr-namespace:System.Collections;assembly=System.Runtime.Extensions" x:Class="StarterEdit.MainWindow"
        mc:Ignorable="d"
        Title="Starter Edit" Height="420" Width="550">
    <Grid Margin="0,0,0,11" HorizontalAlignment="Center" Width="530">
        <Label Content="Squirtle" HorizontalAlignment="Left" Margin="45,50,0,0" VerticalAlignment="Top" ToolTip="Starter One"/>
        <Label Content="Bulbasaur" HorizontalAlignment="Left" Margin="245,50,0,0" VerticalAlignment="Top" ToolTip="Starter Two"/>
        <Label Content="Charmander" HorizontalAlignment="Left" Margin="445,50,0,0" VerticalAlignment="Top" ToolTip="Starter Three"/>
        <ComboBox x:Name="NameList" HorizontalAlignment="Left" Margin="10,81,0,0" VerticalAlignment="Top" Width="120" IsReadOnly="True" SelectedIndex="0" Cursor="Arrow" IsTextSearchEnabled="True" ToolTip="List of Pokemon names">

        </ComboBox>
</Window>

Here is my MainWindow class这是我的 MainWindow 类

    public partial class MainWindow : Window
    {
        Dictionary<int, string> pokemonNames = new Dictionary<int, string>();
        PokemonData pokemonData = new PokemonData();

        public MainWindow()
        {
            InitializeComponent();

            NameList.ItemsSource = pokemonData.getPokemonNames(); //method that returns string array 
            NameList.ItemsSource = pokemonNames; //this is a dictionary
        }

    }

What I'm trying to do is using WPF I want to populate my comboBox with the data from the PokemonData Class, specifically the array containing all the names.我想要做的是使用 WPF 我想用 PokemonData 类中的数据填充我的组合框,特别是包含所有名称的数组。 The problem is whenever I bind the data or set the data it never displays on the gui or in the comboBox.问题是每当我绑定数据或设置数据时,它都不会显示在 gui 或组合框中。

If shortly, the next code must work correctly, just do this initialization after loading data from the file.如果很快,下一个代码必须正常工作,只需在从文件加载数据后执行此初始化。

NameList.ItemsSource = pokemonData.getPokemonNames();

If you want a better solution, you can find it below (when Pokemons collection have changed UI would be updated automatically):如果你想要一个更好的解决方案,你可以在下面找到它(当口袋妖怪收藏改变时 UI 会自动更新):

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = new PokemonData(setDirectory() + @".\PokemonNames.txt");
    }
}

public class Pokemon
{
    public int ID { get; set; }
    public string Name { get; set; }
}

public class PokemonData
{
    public ObservableCollection<Pokemon> Pokemons { get; set; } = new ObservableCollection<Pokemon>();

    public PokemonData(string path)
    {
        LoadData(path);
    }

    private void LoadData(string path)
    {
        Pokemons.Clear();
        using (StreamReader stream = new StreamReader(path))
        {
            int i = 1;
            while (true)
            {
                string pokemonName = stream.ReadLine();

                if (pokemonName != null)
                    Pokemons.Add(new Pokemon { ID = i, Name = pokemonName.Trim() });
                else break;

                i++;
            }
        }
    }
}

And XAML code:和 XAML 代码:

<ComboBox ItemsSource="{Binding Pokemons}">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Name}"/>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

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

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