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
public class StudentsCollectionViewModal
{
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.