尚未对其进行测试,但这样的事情应该可以工作:
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
或者我更喜欢DataTemplate
用 XAML 编写并通过 CodeBehind 中的 Key 访问它。
CellTemplate = TryFindResource("myDataTemplateKey") as DataTemplate