簡體   English   中英

C# WPF ComboBox ItemsSource 綁定問題

[英]C# WPF ComboBox ItemsSource Binding Issue

我在將 ComboBox ItemsSource 與列表綁定時遇到問題。 我從 csv 文件中讀取了工作場所。 它看不到工作場所列表。 請告訴我哪里出了問題。 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:

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

工作場所信息 class:

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

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

這是您優化后的代碼:

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

因此,您的代碼中需要進行的更改是:

  1. 將您的 Workplaces 集合設置為公共 ObservableCollection
  2. 添加 DataContext 綁定
  3. 讀入日期並在主 window 初始化之前創建集合(否則 UI 不會檢測到你 object 的變化,除非你實現 INotifyPropertyChanged 事件)

ps 我不知道你的 csv 文件的結構所以我制作了我的小演示(Workplaces.csv)並采用了解析器。 如果它與您的 csv 文件結構匹配,您可以保留解析器。:

title;member_of 
London;First 
Amsterdam;Second

我的熱情建議是在處理文件和處理應用程序外部的任何內容時始終使用 try-catch 塊。

最好的祝福。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM