简体   繁体   English

C# WPF ComboBox ItemsSource 绑定问题

[英]C# WPF ComboBox ItemsSource Binding Issue

I have an issue with binding ComboBox ItemsSource with a list.我在将 ComboBox ItemsSource 与列表绑定时遇到问题。 I read workplaces from csv file.我从 csv 文件中读取了工作场所。 It can't see the Workplaces list.它看不到工作场所列表。 Please, tell me what is wrong.请告诉我哪里出了问题。 xaml: xaml:

    <ComboBox Grid.Column="1" Grid.Row="9" Height="25" Margin="0,18,0,0" ItemsSource="{Binding Workplaces}">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding title}" />
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>

xaml.cs: xaml.cs:

public partial class MainWindow : Window
{
   private BindableCollection<WorkplaceInfo> Workplaces { get; set; }

    public MainWindow()
    {
        InitializeComponent();
        Workplaces = new BindableCollection<WorkplaceInfo>(GetWorkplaces());
    }

    private List<WorkplaceInfo> GetWorkplaces()
    {
        List<WorkplaceInfo> workplaces = new List<WorkplaceInfo>();

        using (var streamReader = new StreamReader("Workplaces.csv"))
        {
            using (var csvReader = new CsvReader(streamReader, CultureInfo.InvariantCulture))
            {
                //csvReader.Context.RegisterClassMap<WorkplaceInfoClassMap>();
                var workplaceInfoList = csvReader.GetRecords<dynamic>().ToList();
                foreach (var wi in workplaceInfoList)
                {
                    workplaces.Add(new WorkplaceInfo(wi.title, wi.member_of.Split(";")));  
                }
            }
        }
        return workplaces;
    }
}

WorkplaceInfo class:工作场所信息 class:

class WorkplaceInfo
{
    public String title { get; }
    public String[] memberOfList { get; }

    public WorkplaceInfo(string title, string[] memberOfList)
    {
        this.title = title;
        this.memberOfList = memberOfList;
    } 
}

Here is your code optimized to work:这是您优化后的代码:

   public ObservableCollection<WorkplaceInfo> Workplaces { get; set; }

    public MainWindow()
    {
        this.DataContext = this;
        Workplaces = new ObservableCollection<WorkplaceInfo>(GetWorkplaces());
        InitializeComponent();


    }

    private List<WorkplaceInfo> GetWorkplaces()
    {
        List<WorkplaceInfo> workplaces = new List<WorkplaceInfo>();

        try
        {
            using (var streamReader = new StreamReader("Workplaces.csv"))
            {
                using (var csvReader = new CsvReader(streamReader, CultureInfo.CurrentCulture))
                {
                    //csvReader.Context.RegisterClassMap<WorkplaceInfoClassMap>();
                    var workplaceInfoList = csvReader.GetRecords<dynamic>().ToList();
                    foreach (var wi in workplaceInfoList)
                    {
                        var titl = wi.title;

                        workplaces.Add(new WorkplaceInfo(wi.title, new List<string>() { wi.member_of }.ToArray()));
                    }
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
        return workplaces;
    }

So the changes needed in your code are:因此,您的代码中需要进行的更改是:

  1. Set your Workplaces collection to public ObservableCollection将您的 Workplaces 集合设置为公共 ObservableCollection
  2. Add DataContext binding添加 DataContext 绑定
  3. Read in the Date and create the collection before the main window is initialized (other way round the UI will not detect the change in you object unless you implement INotifyPropertyChanged event)读入日期并在主 window 初始化之前创建集合(否则 UI 不会检测到你 object 的变化,除非你实现 INotifyPropertyChanged 事件)

ps I don't know the structure of your csv file so I made my small demo like (Workplaces.csv) and adopted the parser. ps 我不知道你的 csv 文件的结构所以我制作了我的小演示(Workplaces.csv)并采用了解析器。 You can keep your parser if it matches your csv file structrue.:如果它与您的 csv 文件结构匹配,您可以保留解析器。:

title;member_of 
London;First 
Amsterdam;Second

And my warm recommendation is to use try-catch block always when handling files and when working with anything what is external to your application.我的热情建议是在处理文件和处理应用程序外部的任何内容时始终使用 try-catch 块。

Best regards.最好的祝福。

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

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