none
How to bind datagrid to DataContext?

    Question

  •  

    MainPage.cs has property Items (List<T>)

    public List<Item> Items;


     

    I would like to bind a datagrid to the property.

     

    On load event:

    DataContext = this;


     

    <sdk:DataGrid AutoGenerateColumns="True" ItemsSource="{Binding Items}">
    </sdk:DataGrid>
    
    


     

    I’m getting an empty datagrid.

    What is wrong with binding syntax?

    Friday, October 01, 2010 12:21 AM

Answers

  • if m nt wrong u can try the following -

    <DataGridName>.ItemsSource = Items;

    public class Customers
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
    
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
    
            // with basic integer list
            List<int> items = new List<int>();
            items.Add(1);
            items.Add(2);
            items.Add(3);
    
            dataGrid1.ItemsSource = items;
    
            // with basic string list
            ObservableCollection<string> values = new ObservableCollection<string>();
            values.Add("one");
            values.Add("two");
            values.Add("three");
    
            dataGrid1.ItemsSource = values;
    
            // with a customer list
            ObservableCollection<Customers> cust = new ObservableCollection<Customers>();
            cust.Add(new Customers() { FirstName = "abc", LastName="def" });
            cust.Add(new Customers() { FirstName = "ghi", LastName = "jkl" });
    
            dataGrid1.ItemsSource = cust;
        }
    }

    cheers..


    Saturday, October 02, 2010 4:03 AM
  • Hi

    Based on moizmh's sample, here is a modified sample which may meet your requirement 

    C#

                public class Customers 
                { 
                    public string FirstName { get; set; } 
                    public string LastName { get; set; } 
                }
                public ObservableCollection<Customers> cust
                {
                    get;
                    set;
                }
    
                public MainPage ()
                {
                    InitializeComponent();
    
                    cust = new ObservableCollection<Customers>();
                    cust.Add(new Customers()
                    {
                        FirstName = "abc",
                        LastName = "def"
                    });
                    cust.Add(new Customers()
                    {
                        FirstName = "ghi",
                        LastName = "jkl"
                    });
    
                    this.DataContext = this;
                }


     

    XAML

    <sdk:DataGrid x:Name="dataGrid1" AutoGenerateColumns="True" ItemsSource="{Binding cust}"/>

     

    Best Regards,

     

    Thursday, October 07, 2010 5:49 AM

All replies

  • You are using list<items> as an item source are you ensuring that the collections are filled at load time befor setting the data context.

    It is better to use observable collection as it will update the UI when collection gets updated instead of list.

    Friday, October 01, 2010 12:39 AM
  • Yes, I'm sure collection is created before page loaded.
    I changed List<T> to ObservableCollection the same result.

    Friday, October 01, 2010 2:28 AM
  • hi, Lucky

    like this. just binding

    <sdk:DataGrid AutoGenerateColumns="True" ItemsSource="{Binding}"> 
    </sdk:DataGrid>

    Friday, October 01, 2010 3:41 AM
  • DataContext = this;

    Should this line be?: -

    this.DataContext = Items;


     

    Friday, October 01, 2010 8:09 AM
  • Does the property raise change notification?  Are there items in the list?  Are the items added to the list before the settings of the datacontext?  Do you see any binding errors in the output page after closing the browser?

    Friday, October 01, 2010 9:02 AM
  • DataContext = this;

    Should this line be?: -

    this.DataContext = Items; 

    Hi ChrisCookDev,

    What would be the difference DataContext = this; ItemsSource {Binding Items} vs DataContext = items; ItemsSource {Binding}?

    Friday, October 01, 2010 1:11 PM
  • Does the property raise change notification?  Are there items in the list?  Are the items added to the list before the settings of the datacontext?  Do you see any binding errors in the output page after closing the browser?


    Does the property raise change notification?  No

    Are there items in the list?  Yes

    Are the items added to the list before the settings of the datacontext?  Yes

    Do you see any binding errors in the output page after closing the browser? No

    Friday, October 01, 2010 1:15 PM
  • I'm just guessing here, but I'd try adding notification onto that property.  That way, when the property changes, the UI knows and can display.

    Friday, October 01, 2010 11:01 PM
  • if m nt wrong u can try the following -

    <DataGridName>.ItemsSource = Items;

    public class Customers
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
    
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
    
            // with basic integer list
            List<int> items = new List<int>();
            items.Add(1);
            items.Add(2);
            items.Add(3);
    
            dataGrid1.ItemsSource = items;
    
            // with basic string list
            ObservableCollection<string> values = new ObservableCollection<string>();
            values.Add("one");
            values.Add("two");
            values.Add("three");
    
            dataGrid1.ItemsSource = values;
    
            // with a customer list
            ObservableCollection<Customers> cust = new ObservableCollection<Customers>();
            cust.Add(new Customers() { FirstName = "abc", LastName="def" });
            cust.Add(new Customers() { FirstName = "ghi", LastName = "jkl" });
    
            dataGrid1.ItemsSource = cust;
        }
    }

    cheers..


    Saturday, October 02, 2010 4:03 AM
  • Hi

    Based on moizmh's sample, here is a modified sample which may meet your requirement 

    C#

                public class Customers 
                { 
                    public string FirstName { get; set; } 
                    public string LastName { get; set; } 
                }
                public ObservableCollection<Customers> cust
                {
                    get;
                    set;
                }
    
                public MainPage ()
                {
                    InitializeComponent();
    
                    cust = new ObservableCollection<Customers>();
                    cust.Add(new Customers()
                    {
                        FirstName = "abc",
                        LastName = "def"
                    });
                    cust.Add(new Customers()
                    {
                        FirstName = "ghi",
                        LastName = "jkl"
                    });
    
                    this.DataContext = this;
                }


     

    XAML

    <sdk:DataGrid x:Name="dataGrid1" AutoGenerateColumns="True" ItemsSource="{Binding cust}"/>

     

    Best Regards,

     

    Thursday, October 07, 2010 5:49 AM