C# 通过编程方式添加DataTemplate和DataGrid WPF的数据绑定问题
我正在尝试以编程方式为DataGrid创建Datatemplate。 我能做到。但我被困在一个位置。DataGrid的绑定不起作用。 我认为绑定存在一些问题。以下是我得到的输出: 图像中的第三列是空的,尽管我已经为该列设置了绑定 以下是XAML文件:C# 通过编程方式添加DataTemplate和DataGrid WPF的数据绑定问题,c#,wpf,data-binding,datagrid,C#,Wpf,Data Binding,Datagrid,我正在尝试以编程方式为DataGrid创建Datatemplate。 我能做到。但我被困在一个位置。DataGrid的绑定不起作用。 我认为绑定存在一些问题。以下是我得到的输出: 图像中的第三列是空的,尽管我已经为该列设置了绑定 以下是XAML文件: <Window x:Class="NestedListViewDemo.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation
<Window x:Class="NestedListViewDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" WindowState="Maximized">
<Grid Name="MainGrid">
<DataGrid x:Name="myDataGrid" AutoGenerateColumns="true" Loaded="myDataGrid_Loaded">
<DataGrid.Columns>
<DataGridTemplateColumn x:Name="templatecolumnId" Header="Id" />
<DataGridTemplateColumn x:Name="templatecolumnName" Header="Name" />
<DataGridTemplateColumn x:Name="templateColumnInnerTable" Header="InnerTable" />
</Datagrid.Columns>
</DataGrid>
</Grid>
</Window>
始终需要为
DataGrid
设置集合。我对你现有的代码做了一些修改,效果很好。请参阅下面的代码片段
通过添加AddressClass
的Collection
来修改您的AddressClass
。此属性需要与数据网格
绑定
public class Address
{
public string Id { get; set; }
public string Name { get; set; }
public Address() { }
}
public class AddressClass
{
public string Id { get; set; }
public string Name { get; set; }
public List<Address> Address { get; set; }
public AddressClass()
{
Address = new List<Address>();
}
public void AddItems(Address item)
{
Id = item.Id;
Name = item.Name;
Address.Add(item);
}
}
public class InnerClass
{
public string InnerData { get; set; }
}
调用方法的更改
public MainWindow()
{
InitializeComponent();
DataGrid innergrid = new DataGrid();
InnerClass ic = new InnerClass();
ic.InnerData = "InnerXYZ";
innergrid.Items.Add(ic);
Address ac = new Address() { Id = "100",Name ="Vimal" };
Address ac1 = new Address(){Id ="101", Name= "Vimal 1"};
AddressClass add = new AddressClass();
add.AddItems(ac);
add.AddItems(ac1);
myDataGrid.Items.Add(add);
myDataGrid.MinRowHeight = 100;
myDataGrid.MinColumnWidth = 250;
}
你为什么要用代码隐藏做这些事?这就是XAML的用途,我只想创建n级嵌套的ListView/DataGrid。级别在运行时决定。因此,我想在代码隐藏中做这件事。非常感谢兄弟。成功了。我很乐意订阅您的博客以获取更新。
public void myDataGrid_Loaded(object sender, EventArgs e)
{
var dataTemplate = new DataTemplate();
FrameworkElementFactory tbHolder1 = new FrameworkElementFactory(typeof(Label));
tbHolder1.SetBinding(Label.ContentProperty, new Binding("Id"));
dataTemplate.VisualTree = tbHolder1;
dataTemplate.DataType = typeof(DataGridTemplateColumn);
templatecolumnId.CellTemplate = dataTemplate;
dataTemplate = new DataTemplate();
FrameworkElementFactory tbHolder2 = new FrameworkElementFactory(typeof(TextBlock));
tbHolder2.SetBinding(TextBlock.TextProperty, new Binding("Name"));
dataTemplate.VisualTree = tbHolder2;
dataTemplate.DataType = typeof(DataGridTemplateColumn);
templatecolumnName.CellTemplate = dataTemplate;
dataTemplate = new DataTemplate();
FrameworkElementFactory tbHolder3 = new FrameworkElementFactory(typeof(DataGrid));
tbHolder3.SetBinding(DataGrid.ItemsSourceProperty, new Binding("Address"));
tbHolder3.SetValue(DataGrid.AutoGenerateColumnsProperty, true);
dataTemplate.VisualTree = tbHolder3;
dataTemplate.DataType = typeof(DataGridTemplateColumn);
templateColumnInnerTable.CellTemplate = dataTemplate;
}
public MainWindow()
{
InitializeComponent();
DataGrid innergrid = new DataGrid();
InnerClass ic = new InnerClass();
ic.InnerData = "InnerXYZ";
innergrid.Items.Add(ic);
Address ac = new Address() { Id = "100",Name ="Vimal" };
Address ac1 = new Address(){Id ="101", Name= "Vimal 1"};
AddressClass add = new AddressClass();
add.AddItems(ac);
add.AddItems(ac1);
myDataGrid.Items.Add(add);
myDataGrid.MinRowHeight = 100;
myDataGrid.MinColumnWidth = 250;
}