Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# WPF
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 b1 = new ObservableCollection();

private ObservableCollection f1 = new ObservableCollection();
 
//private string y;
public ObservableCollection B1
{
get
{
return b1;
}
set
{
b1 = value;
OnPropertyChanged("B1");
}
}
public ObservableCollection 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 27-Dec-12 20:43pm
Edited 27-Dec-12 22:35pm
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

ComboBox1.SetBinding(ItemsControl.ItemsSourceProperty,
                      new Binding { Source = f1  });
  Permalink  
Comments
Juhi saf at 28-Dec-12 3:36am
   
But, I want to bind it in the listbox.
Oleksandr Kulchytskyi at 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)

  Print Answers RSS
0 OriginalGriff 240
1 Kamal Rocks 184
2 PIEBALDconsult 150
3 BillWoodruff 148
4 Jochen Arndt 135
0 OriginalGriff 5,695
1 DamithSL 4,506
2 Maciej Los 4,007
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,180


Advertise | Privacy | Mobile
Web04 | 2.8.141216.1 | Last Updated 28 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100