开发者

How do I bind a datagrid in Silverlight?

开发者 https://www.devze.com 2023-03-21 17:00 出处:网络
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.

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:

  1. Databinding using XAML syntax
  2. Databind in code behind
  3. 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.aspx

For 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.aspx

Alternative 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=en

Many 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
0

精彩评论

暂无评论...
验证码 换一张
取 消