Click here to Skip to main content
15,917,610 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I tried Binding in XAML, while binding i encountered this issue

<pre lang="c#">

public class StudentsCollectionViewModal
    {
        public List<Author> studentCollection 

       = new List<Author>();

        public StudentsCollectionViewModal()
        {
            studentCollection = LoadCollectionData();
        }
        private List<Author> LoadCollectionData()
        {
            List<Author> authors = new List<Author>();
            authors.Add(new Author()
            {
                ID = 101,
                Name = "Mahesh Chand",
                BookTitle = "Graphics Programming with GDI+",
                DOB = new DateTime(1975, 2, 23),
                IsMVP = false
            });
            authors.Add(new Author()
            {
                ID = 201,
                Name = "Mike Gold",
                BookTitle = "Programming C#",
                DOB = new DateTime(1982, 4, 12),
                IsMVP = true
            });
            authors.Add(new Author()
            {
                ID = 244,
                Name = "Mathew Cochran",
                BookTitle = "LINQ in Vista",
                DOB = new DateTime(1985, 9, 11),
                IsMVP = true
            });
            return authors;
        }
        public class Author
        {
            public int ID { get; set; }
            public string Name { get; set; }
            public DateTime DOB { get; set; }
            public string BookTitle { get; set; }
            public bool IsMVP { get; set; }
        }
    }
}


here is XAML code where i tried binding
<UserControl.DataContext>
        <viewModal:StudentsCollectionViewModal></viewModal:StudentsCollectionViewModal>
    </UserControl.DataContext>
    <Grid Background="SkyBlue">
        <DataGrid Margin="10" x:Name="datagrid" ItemsSource="{Binding studentCollection}" RowBackground="LightYellow"
 AlternatingRowBackground="LightSkyBlue" AlternationCount="2" AllowDrop="True" >
        </DataGrid>       
    </Grid>


as per my expectation binding didn't work , but when i changed "studentCollection" field to properties
public List<Author> studentCollection 

       = new List<Author>();


to
private List<Author> _studentCollection;

       public List<Author> studentCollection
       {
           get { return _studentCollection; }
           set { _studentCollection = value; }
       }

it worked.

whole modified class
C#
public class StudentsCollectionViewModal
    {
        // public List<Author> studentCollection = new List<Author>();

        private List<Author> _studentCollection;

        public List<Author> studentCollection
        {
            get { return _studentCollection; }
            set { _studentCollection = value; }
        }

        public StudentsCollectionViewModal()
        {
            studentCollection = LoadCollectionData();
        }
        private List<Author> LoadCollectionData()
        {
            List<Author> authors = new List<Author>();
            authors.Add(new Author()
            {
                ID = 101,
                Name = "Mahesh Chand",
                BookTitle = "Graphics Programming with GDI+",
                DOB = new DateTime(1975, 2, 23),
                IsMVP = false
            });
            authors.Add(new Author()
            {
                ID = 201,
                Name = "Mike Gold",
                BookTitle = "Programming C#",
                DOB = new DateTime(1982, 4, 12),
                IsMVP = true
            });
            authors.Add(new Author()
            {
                ID = 244,
                Name = "Mathew Cochran",
                BookTitle = "LINQ in Vista",
                DOB = new DateTime(1985, 9, 11),
                IsMVP = true
            });
            return authors;
        }
        public class Author
        {
            public int ID { get; set; }
            public string Name { get; set; }
            public DateTime DOB { get; set; }
            public string BookTitle { get; set; }
            public bool IsMVP { get; set; }
        }
    }

So i have this query is binding in XAML works only on properties not on fields.

What I have tried:

I tried google but couldn't find any dent so tried over my XAML application to understand.
Posted
Updated 12-May-17 1:54am
Comments
[no name] 12-May-17 6:58am    
"I tried google but couldn't find any", then you need to improve your google skills. It's well documented that binding works on properties and not fields.

1 solution

No, binding does not support fields:
You can bind to public properties, sub-properties, as well as indexers, of any common language runtime (CLR) object.
 
Share this answer
 

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