【发布时间】: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类在哪里使用?您可能需要显示包含ComboBox的Window的相关XAML 和代码隐藏。 -
@BACON 组合框不会显示我给它的数据是这里的全部问题。我已经更新了我的帖子以显示其他相关代码。