[Silverlight] Checkbox List within ListBox using DataTemplate


DataTemplates in Silverlight are typically used for visual representation of your data.Data Templates are allow you to define a custom way in which the data should be display in a Control. They are particularly useful when you are binding an ItemsControl such as a ListBox to a collection. In this article, I have demonstrated how to declare DataTemplates in XAML and to read and apply them at runtime. Also one more thing over here is bind check box list in DataTemplate.

Here I Declare ListOfRecords class for Entity and bind it to Listbox Item Source.Looks like:



public class ListOfRecords : INotifyPropertyChanged
{
private bool _IsSelected = false;
public int ID { get; set; }
public string Content { get; set; }
public bool IsSelected
{
get { return _IsSelected; }
set
{
_IsSelected = value;
NotifyPropertyChanged("IsSelected");
}
}
public Brush BrushObj { get; set; }

public event PropertyChangedEventHandler PropertyChanged;

public void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}

After Creating Class My MainPage.xmal looks like





IN the XAML defines DataTemplates to display ListBoxItem for the ListBox. This DataTemplate also has a Checkbox element which is used to changed value for the Collection. The MainPage.Xaml.cs looks like:




private ObservableCollection _objList;
public ObservableCollection objList
{
get { return _objList; }
set
{
_objList = value;

}
}
public MainPage()
{
InitializeComponent();
_objList = new ObservableCollection();
ListOfRecords supNode = new ListOfRecords() { ID = 0, Content = "Select All", IsSelected = false, BrushObj = new SolidColorBrush(Colors.Blue) };
_objList.Add(supNode);
for (int i = 1; i <= 10; i++)
{
ListOfRecords l = new ListOfRecords() { ID = i, Content = "Content : " + i.ToString(), IsSelected = false, BrushObj = new SolidColorBrush(Colors.Black) };
_objList.Add(l);
}
objList = _objList;
lstTemp.ItemsSource = objList;

Grid grd = new Grid();

for (int i = 0; i < 15; i++)
{
RowDefinition rd = new RowDefinition();
rd.SetValue(NameProperty, i.ToString());
rd.Height = new GridLength(25);
grd.RowDefinitions.Add(rd);
}
scrCriteriaSummaryTemp.Content = grd;
}



Also refer this site

http://www.codeproject.com/Tips/404796/Silverlight-Checkbox-List-within-ListBox-using-Dat


Attachments

  • Checkbox List within ListBox using DataTemplate (44038-203557-Checkbox-List-within-ListBox-using-DataTemplate.zip)
  • Comments

    No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: