本文介绍了将两个可观察集合绑定到单个列表框。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有一个列表框。我必须从数据库中添加一个项目列表。然后在运行时加载窗口时,我希望看到选中的预先选择的项目。现在第一项任务正在进行中。但是为了获得选择过程,我必须将另一个可观察集合绑定到列表框的ItemsSource属性。我怎么绑它?任何人都可以帮忙??? 我在这里附上代码: Class1.cs 使用System; 使用System.Collections.Generic; 使用System.Linq; 使用System.Text; 使用System.Collections.ObjectModel; 使用System.ComponentModel; 使用System.Data; 使用System.Data.SqlClient ; 名称空间WpfApplication1 { 公共类Class1:INotifyPropertyChanged { 私人ObservableCollection< book> b1 = new ObservableCollection< book>(); private ObservableCollection< fill> f1 = new ObservableCollection< fill>(); //私有字符串y; public ObservableCollection< book> B1 { get { 返回b1; } set { b1 = value; OnPropertyChanged(B1); } } public ObservableCollection< fill> F1 { get { 返回f1; } set { f1 = value; OnPropertyChanged(F1); } } public Class1() { LoadListBox(); LoadBookList(); } private void LoadListBox() { 试试 { SqlConnection con1 = new SqlConnection(Data Source = .; Initial Catalog = test; User ID = sa; Password = nest123 @!;); con1.Open(); SqlCommand cmd1 = new SqlCommand(从booklist中选择List,con1); SqlD ataReader myReader = cmd1.ExecuteReader(); while(myReader.Read()) { string str2 = myReader [List ] .ToString(); f1.Add(new Fill(str2)); } myReader.Close(); myReader.Dispose(); } catch(例外e) { Console.WriteLine(e.Message); } } private void LoadBookList() { 尝试 { SqlConnection con1 = new SqlConnection(Data Source =。;初始目录=测试;用户ID = sa; Password = nest123 @!;); con1.Open(); SqlCommand cmd1 = new SqlCommand(从authorinfo中选择书籍,其中Id =(选择最大值(Id) )来自authorinfo),con1); SqlDataReader myReader = cmd1.ExecuteReader(); while(myReader.Read()) { string str1 = myReader [Books]。ToString(); string [] arr = str1.Split('',''); foreach(字符串在arr中) { b1.Add(新书(word)); } } myReader.Close(); myReader.Dispose(); } catch(exception ex) { Console.WriteLine(ex.Message); } } 公共事件PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(string propertyname) { if(PropertyChanged!= null) { PropertyChanged(this,new PropertyChangedEventArgs (propertyname)); } } } 公共类书籍:INotifyPropertyChanged { 私人字符串项; 公共字符串ITEM { get { 返回项目; } 设定 { item = value; OnPropertyChanged(ITEM ); } } 公共图书(字符串x) { item = x; } 公共事件PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(string propertyname ) { if(PropertyChanged!= null) { PropertyChanged(这个,新的PropertyChangedEventArgs(属性名)); } } } 公共类填写: INotifyPropertyChanged的 { 私人字符串数据; 公共字符串DATA { get { 返回数据; } set { data = value; OnPropertyChanged(DATA); } } public Fill (字符串y) { data = y; } 公共事件PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(string propertyname) { if(PropertyChanged!= null) { PropertyChanged(这个,新的PropertyChangedEventArgs(属性名)); } } } } 先谢谢...I''ve a listbox. I''ve to add a list of items to it from the database. Then at runtime when the window is loaded, I want the preveously selected item to be seen selected. Now the first task is working. But for getting the selection process I''ve to bind another observable collection to the ItemsSource property of the listbox. How can I bind it??? Can anyone help???I''m attaching the code down here: Class1.csusing System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Collections.ObjectModel;using System.ComponentModel;using System.Data;using System.Data.SqlClient;namespace WpfApplication1{ public class Class1 : INotifyPropertyChanged { private ObservableCollection<book> b1 = new ObservableCollection<book>(); private ObservableCollection<fill> f1 = new ObservableCollection<fill>(); //private string y; public ObservableCollection<book> B1 { get { return b1; } set { b1 = value; OnPropertyChanged("B1"); } } public ObservableCollection<fill> F1 { get { return f1; } set { f1 = value; OnPropertyChanged("F1"); } } public Class1() { LoadListBox(); LoadBookList(); } private void LoadListBox() { try { SqlConnection con1 = new SqlConnection("Data Source=.; Initial Catalog= test; User ID= sa; Password=nest123@!;"); con1.Open(); SqlCommand cmd1 = new SqlCommand("select List from booklist", con1); SqlDataReader myReader = cmd1.ExecuteReader(); while (myReader.Read()) { string str2 = myReader["List"].ToString(); f1.Add(new Fill(str2)); } myReader.Close(); myReader.Dispose(); } catch (Exception e) { Console.WriteLine(e.Message); } } private void LoadBookList() { try { SqlConnection con1 = new SqlConnection("Data Source=.; Initial Catalog= test; User ID= sa; Password=nest123@!;"); con1.Open(); SqlCommand cmd1 = new SqlCommand("select Books from authorinfo where Id=(select max(Id) from authorinfo)", con1); SqlDataReader myReader = cmd1.ExecuteReader(); while (myReader.Read()) { string str1 = myReader["Books"].ToString(); string[] arr = str1.Split('',''); foreach (string word in arr) { b1.Add(new Book(word)); } } myReader.Close(); myReader.Dispose(); } catch (Exception ex) { Console.WriteLine(ex.Message); } } public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(string propertyname) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyname)); } } } public class Book: INotifyPropertyChanged { private string item; public string ITEM { get { return item; } set { item = value; OnPropertyChanged("ITEM"); } } public Book(string x) { item = x; } public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(string propertyname) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyname)); } } } public class Fill : INotifyPropertyChanged { private string data; public string DATA { get { return data; } set { data = value; OnPropertyChanged("DATA"); } } public Fill(string y) { data = y; } public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(string propertyname) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyname)); } } }}Thanks in Advance...推荐答案ComboBox1.SetBinding(ItemsControl.ItemsSourceProperty, new Binding { Source = f1 }); 这篇关于将两个可观察集合绑定到单个列表框。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-20 22:26