surajitk
surajitk

Reputation: 147

Databinding issues with programmatically added DataTemplate and DataGrid WPF

Iam trying to create a Datatemplate for DataGrid programmatically. Iam able to do so. But iam stuck at a position. The binding of DataGrid is not working. I think there is some issues with binding. Here is the output Iam getting :

Output Screenshot

3rd column in the image is empty although I have set the binding for that column.

Here is the XAML file :

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

I have Added Three columns and in 3rd column iam trying to add another datagrid programmatically. Here is the Code-behind:

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

Upvotes: 0

Views: 2494

Answers (1)

Vimal CK
Vimal CK

Reputation: 3563

Always needs to set a Collection for the DataGrid. I had made some changes to your existing code which works fine. Please see the below snippet

Modified your AddressClass by adding a Collection of AddressClass. This property needs to bind with the 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; }
}

Modified your template to bind the DataGrid. Please notice that I have binded the Collection to ItemsSource property instead of DataContext property

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

Changes in Calling method

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

Upvotes: 1

Related Questions