Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.cs
using 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...
Posted
Updated 27-Dec-12 21:35pm
v2

1 solution

C#
ComboBox1.SetBinding(ItemsControl.ItemsSourceProperty,
                      new Binding { Source = f1  });
 
Share this answer
 
Comments
Juhi saf 28-Dec-12 3:36am    
But, I want to bind it in the listbox.
Oleksandr Kulchytskyi 28-Dec-12 3:40am    
So what a problem ??? Any issues with replacing instead of combobox to put listbox??

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900