Click here to Skip to main content
15,396,859 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have a
Data Holder class with some properties
C#
public string Name
 {
     get;
     set;
 }
 public string No
 {
     get;
     set;
 }

 public IList<Customer> CustomerData
 {
     get;
     set;
 }

 public Customer()
 {
     IList<Customer> customerList = new List<Customer>();
     this.Name = "Adam";
     this.No = "1";
     customerList.Add(this);
     CustomerData = customerList;
 }



and in my Xaml I am trying to bind it to my grid
XML
<DataGrid Name="Sampledatagrid" AutoGenerateColumns="True" ItemsSource="{Binding CustomerData}" Margin="0,0,0,106" />



I am not getting the updated CustumerData
Am i missing somthing ??
please advise
Posted
Updated 21-Jun-11 2:12am
v2

I think you need to create the List of customers as a class in it's own right like this

C#
public class Customer
    {
        public string Name
        {
            get;
            set;
        }
        public int No
        {
            get;
            set;
        }
        public Customer()
        {
        }
    }
    public class Customers : List<Customer>
    {
        public Customers()
        {
            this.Add(new Customer() { Name = "Adam", No = 1 });
        }
    }


then you can create a Static or Dynamic Resource in your Window and bind to the Resource like this

XML
<Window.Resources>
        <local:Customers x:Key="MyCustomers"/>
    </Window.Resources>



and create your DataGrid like this
XML
<DataGrid Name="Sampledatagrid" AutoGenerateColumns="True" ItemsSource="{StaticResource MyCustomers}" Margin="0,0,0,106"/>


and then you should be able to see your data straight away even in the designer.

This assumes that you have declared your namespace that the Customer class is in and you have called it local like this

XML
xmlns:local="clr-namespace:yourApplication"


Hope this helps
   
v2
Comments
arun_pk 21-Jun-11 8:40am
   
Thank you very much
my 5

it worked :)
Wayne Gaylard 21-Jun-11 8:42am
   
Pleasure!
Joezer BH 5-May-13 5:53am
   
5+
From what I can see here, you haven't assigned the Customer class to the DataContext. Without doing that, your Binding will not work.
   
Comments
arun_pk 21-Jun-11 8:23am
   
Thanks for the reply
DataContext="{Binding Customer}"
inside the DataGrid Tag ?

You mean something like this

<datagrid name="Sampledatagrid" autogeneratecolumns="True" datacontext="{Binding Customer}" itemssource="{Binding CustomerData}" margin="0,0,0,106">

I m sorry .. new to WPF
Pete O'Hanlon 21-Jun-11 9:03am
   
No - I mean doing something as simple as:
<Window.DataContext>
<local:Customer />
</Window.DataContext>
Rollie 28-Aug-19 12:53pm
   
Thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900