使用以编程方式添加的DataTemplate和DataGrid WPF的数据绑定问题

时间:2013-12-19 13:29:04

标签: c# wpf data-binding datagrid

我试图以编程方式为DataGrid创建Datatemplate。 我能做到这一点。但我坚持一个位置。 DataGrid的绑定不起作用。 我认为绑定存在一些问题。这是Iam获得的输出:

Output Screenshot

虽然我已为该列设置了绑定,但图像中的第3列为空。

这是XAML文件:

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

我添加了三列,并在第三列iam尝试以编程方式添加另一个数据网格。 这是代码隐藏:

public partial class MainWindow : Window
        {

    public MainWindow()
        {
            InitializeComponent();
            DataGrid innergrid = new DataGrid();

            InnerClass ic=new InnerClass();
            ic.InnerData="InnerXYZ";
        innergrid.Items.Add(ic);

            AddressClass ac = new AddressClass();
            ac.Id = "1";
            ac.Name = "XYZ";
            ac.setDatagrid(innergrid);
            myDataGrid.Items.Add(ac);

            myDataGrid.MinRowHeight = 100;
            myDataGrid.MinColumnWidth = 250;

        }
    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.DataContextProperty, new Binding("Address"));
            tbHolder3.SetValue(DataGrid.AutoGenerateColumnsProperty, true);
            dataTemplate.VisualTree = tbHolder3;
            dataTemplate.DataType = typeof(DataGridTemplateColumn);
            templateColumnInnerTable.CellTemplate = dataTemplate1;

        }
    public class AddressClass
    {
            public string Id { get; set; }
            public string Name { get; set; }

            private DataGrid address;
            public DataGrid Address
            {
                get { return address; }
            }
            public void setDatagrid(DataGrid dtnew)
            {
                this.address = dtnew;
            }

        }
    public class InnerClass
        {
            public string InnerData{ get; set; }
        }
        }

1 个答案:

答案 0 :(得分:1)

始终需要为Collection设置DataGrid。我对您现有的代码进行了一些更改,但工作正常。请参阅以下代码段

通过添加AddressClass Collection修改了您的AddressClass。此属性需要与DataGrid绑定。

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; }
}

修改模板以绑定DataGrid。请注意,我已将Collection绑定到ItemsSource属性而不是DataContext属性

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;
    }