简体   繁体   English

如何在 Silverlight 中绑定数据网格?

[英]How do I bind a datagrid in Silverlight?

I am new to Silverlight.我是 Silverlight 的新手。

How do I bind a datagrid in a Silverlight project?如何在 Silverlight 项目中绑定数据网格?

The app has one mainpage.xaml user control.该应用程序有一个主页。xaml 用户控制。 The datagrid is located in the mainpage.xaml user control.数据网格位于主页中。xaml 用户控件。 The list of objects is retrieved in the mainpage.在主页中检索对象列表。

How should I go about databinding a list of objects to the datagrid?我应该如何 go 关于将对象列表数据绑定到数据网格?

Is there any other way to bind this?有没有其他方法可以绑定这个?

There are three alternatives:有三种选择:

  1. Databinding using XAML syntax使用 XAML 语法进行数据绑定
  2. Databind in code behind后台代码中的数据绑定
  3. Setting the ItemsSource property of the DataGrid instance in code behind在后面的代码中设置 DataGrid 实例的 ItemsSource 属性

For alternative 1 you can get an idea reading the following post:对于备选方案 1,您可以阅读以下帖子获得一个想法:

http://odetocode.com/code/740.aspx http://odetocode.com/code/740.aspx

For alternative 2 take a look on the following link:对于替代 2,请查看以下链接:

http://blogs.msdn.com/b/scmorris/archive/2008/04/14/defining-silverlight-datagrid-columns-at-runtime.aspx 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.备选方案 3 是一种让事情顺利进行的快速方法。 The following link contains a very simple for this one:以下链接包含一个非常简单的链接:

https://docs.google.com/Doc?docid=0AQNzLAfQzOoaZGZrc25tY3BfMWhzMno3c2c4&hl=en 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.许多人不认为替代 2 和 3 是最佳实践,并建议您 go 看看 MVVM (ModelView-ViewModel) 方法。

You could create a dependency property for you Custom control that binds to the itemsource of the Datagrid.您可以为绑定到 Datagrid 的 itemsource 的自定义控件创建依赖属性。

Here are a few links that could help you out.以下是一些可以帮助您的链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM