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.
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