如何在代码中重新创建以下XAML数据绑定?除了DataTemplate定义之外,我有大部分内容。
以下是XAML中的DataBinding的示例
<GridViewColumn Width="140" Header="Name">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=Label}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
这是我到目前为止的代码:
return new GridViewColumn()
{
Header = header,
Width = width,
DisplayMemberBinding = new System.Windows.Data.Binding(bindingProperty)
};
问题是,我是如何通过代码为DataTemplate设置CellTemplate的?
答案 0 :(得分:0)
对于任何有兴趣的人,这是解决方案:
private GridViewColumn GetGridViewColumn(string header, double width, string bindingProperty, Type type)
{
GridViewColumn column = new GridViewColumn();
column.Header = header;
FrameworkElementFactory controlFactory = new FrameworkElementFactory(type);
var itemsBinding = new System.Windows.Data.Binding(bindingProperty);
controlFactory.SetBinding(TextBox.TextProperty, itemsBinding);
DataTemplate template = new DataTemplate();
template.VisualTree = controlFactory;
column.CellTemplate = template;
return column;
}