checkboxes from code behind

Go To StackoverFlow.com

1

I have 3 columns which are filled using binding in xaml from the database and then I have some columns which are dynamic in the ListView. The columns which are dynamic have the headers filled from code behind such as :

viewLayout.Columns.Add(
    new GridViewColumn {       
        Header = subjectvalues_forstdatt[i],
        DisplayMemberBinding = new Binding(String.Format("[{0}]", i))    
    }
);

I want a CheckBox in this dynamic column, not the content being showed by displaymemberbinding.

2012-04-04 07:02
by rohit


1

Haven't tested it but something like this should work:

  DataTemplate template = new DataTemplate { DataType = typeof(object) };  // <-- insert your concrete objet type here

  FrameworkElementFactory stackPanelFactory = new FrameworkElementFactory(typeof(StackPanel));
  stackPanelFactory.SetValue(StackPanel.OrientationProperty, Orientation.Vertical);

  FrameworkElementFactory checkbox = new FrameworkElementFactory(typeof(CheckBox));
  checkbox.SetBinding(CheckBox.IsCheckedProperty, new Binding(String.Format("[{0}]", subjectvalues_forstdatt[i]));
  stackPanelFactory.AppendChild(checkbox);


  viewLayout.Columns.Add(new GridViewColumn
  {

    Header = subjectvalues_forstdatt[i],
    CellTemplate = template

or I'd prefer writing the DataTemplate in XAML and access it via Key in CodeBehind..

CellTemplate = TryFindResource("myDataTemplateKey") as DataTemplate
2012-04-04 07:42
by SvenG
I think the better way is to use SetResourceReference instead of TryFindResource ; - Marat Khasanov 2012-04-04 07:49
thank you very much for your help i m really grateful. The second option worked well.But the other problem is how do i add the content into the checkbox like and according to an event get the subject id per row for the no of checkboxes clicked. Thank you very very muc - rohit 2012-04-05 06:24
i gt the answer for it thank you very much. - rohit 2012-04-05 08:14
Ads