How do I bind a datagrid in Silverlight?
I am new开发者_StackOverflow to Silverlight.
How do I bind a datagrid in a Silverlight project?
The app has one mainpage.xaml user control. The datagrid is located in the mainpage.xaml user control. The list of objects is retrieved in the mainpage.
How should I go about databinding a list of objects to the datagrid?
Is there any other way to bind this?
There are three alternatives:
- Databinding using XAML syntax
- Databind in code behind
- Setting the ItemsSource property of the DataGrid instance in code behind
For alternative 1 you can get an idea reading the following post:
http://odetocode.com/code/740.aspxFor alternative 2 take a look on the following link:
http://blogs.msdn.com/b/scmorris/archive/2008/04/14/defining-silverlight-datagrid-columns-at-runtime.aspxAlternative 3 is a quick way to get things working. The following link contains a very simple for this one:
https://docs.google.com/Doc?docid=0AQNzLAfQzOoaZGZrc25tY3BfMWhzMno3c2c4&hl=enMany don't consider alternative 2 and 3 to be a best practice and would suggest you to go take a look on the MVVM (ModelView-ViewModel) approach.
You could create a dependency property for you Custom control that binds to the itemsource of the Datagrid.
Here are a few links that could help you out.
- http://varazdinac.posterous.com/silverlight-usercontrol-with-datagrid-and-dep
- http://weblogs.asp.net/dwahlin/archive/2011/01/09/demystifying-silverlight-dependency-properties.aspx
精彩评论