【问题标题】:Populating a ComboBox with a Array in C#在 C# 中使用数组填充组合框
【发布时间】:2020-03-31 16:56:59
【问题描述】:

我的数组是从文本文件中读取的口袋妖怪名称列表,然后存储到下面看到的 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;
        }

我现在要做的是使用 WPF 填充一个组合框,其中包含数组中的所有名称。我试过用谷歌搜索这个,经常有很多答案都有这样的类设置:

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

我相信这里正在进行一项任务,但我不确定。 C# 不是我常用的语言,这是我第一次创建 gui。有人可以指导我完成这项工作。

我尝试过做一些事情,例如下面的代码和数据绑定。在这一点上,我相信我错过了一些东西。

<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>

这是我的 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
        }

    }

我想要做的是使用 WPF 我想用 PokemonData 类中的数据填充我的组合框,特别是包含所有名称的数组。问题是每当我绑定数据或设置它从不显示在 gui 或组合框中的数据时。

【问题讨论】:

  • 这是 wpf // winforms // asp 吗?
  • 你没有说明你遇到了什么问题。您还没有显示getPokemonNames() 是什么(真的是populateArray()?),并且您的评论说pokemonNames 是字典,而您的早期代码显示它是一个数组。但是,setDirectory() + @".\PokemonNames.txt" 会有问题,因为您将 . 附加到 setDirectory() 返回的任何内容。它应该只是setDirectory() + @"\PokemonNames.txt",或者更好的是Path.Combine(setDirectory(), "PokemonNames.txt")
  • @KevinDiTraglia wpf
  • 但是有什么问题呢?名称未加载到 ComboBox?设置NameList.ItemsSource 的两条线,它们在哪里,是什么触发了它们?这个PokemonData 类在哪里使用?您可能需要显示包含ComboBoxWindow 的相关XAML 和代码隐藏。
  • @BACON 组合框不会显示我给它的数据是这里的全部问题。我已经更新了我的帖子以显示其他相关代码。

标签: c# arrays wpf combobox


【解决方案1】:

如果很快,下一个代码必须正常工作,只需在从文件加载数据后执行此初始化。

NameList.ItemsSource = pokemonData.getPokemonNames();

如果你想要一个更好的解决方案,你可以在下面找到它(当 Pokemons 集合发生变化时 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++;
            }
        }
    }
}

还有 XAML 代码:

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

【讨论】:

    猜你喜欢
    • 2011-01-25
    • 1970-01-01
    • 2010-09-20
    • 1970-01-01
    • 2017-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多