绑定到可观察集合 silverlight 4,网格

发布于 2024-11-07 17:27:18 字数 184 浏览 0 评论 0原文

我可以将一个简单的“网格”绑定到可观察的集合吗?所以我有一个只有一列和几行的网格。这些行在运行时填充一些可以删除/添加的超链接等。我不想每次都编写函数来添加/删除它们,而更喜欢使用可观察的集合并让网格自行更新。一个简单的网格可以做到这一点吗?我尝试过,但它甚至没有编译。

谢谢

[编辑]解决了。标记的答案是正确的[/编辑]

Can I bind a simple 'grid' to an observable collection? So i have a grid with just one column and several rows. The rows get populated at runtime with some hyperlinks which can get deleted/added etc. I don't want to write functions to add/remove them from the grid everytime and would prefer to use an observable collection and let the grid update itself. Can a simple grid do that ? i tried but it didn't even compile.

Thanks

[EDIT] Solved. The marked answer is correct [/EDIT]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

楠木可依 2024-11-14 17:27:18

其他答案涵盖了各种基础,但您的具体要求由 ItemsControl 满足。您可能希望将其包含在 ScrollViewer 中,以防万一您拥有的链接多于一次可以看到的链接。您将拥有如下标记:-

<ScrollViewer>
   <ItemsControl ItemsSource="{Binding MyObsCollectionOfUrlInfo">
       <ItemsControl.ItemTemplate>
          <DataTemplate>
              <HyperLinkButton NavigateUri={Binding Uri} Content={Binding Title} />
          </DataTemplate>
       </ItemsControl.ItemTemplate>
   </ItemsControl>
</ScrollViewer>

您的可观察集合将包含类似类型的对象列表:-

 public class UrlInfo
 {
      public Uri Uri {get; set; }
      public string Title {get; set; }
 }

Other answers cover various bases but your specific requirement is filled by the ItemsControl. You would probably want to contain it in ScrollViewer just in case where you have more links than can be seen at one time. You would have markup something like this:-

<ScrollViewer>
   <ItemsControl ItemsSource="{Binding MyObsCollectionOfUrlInfo">
       <ItemsControl.ItemTemplate>
          <DataTemplate>
              <HyperLinkButton NavigateUri={Binding Uri} Content={Binding Title} />
          </DataTemplate>
       </ItemsControl.ItemTemplate>
   </ItemsControl>
</ScrollViewer>

Your observable collection would contain a list of objects of a type like:-

 public class UrlInfo
 {
      public Uri Uri {get; set; }
      public string Title {get; set; }
 }
血之狂魔 2024-11-14 17:27:18

Silverlight 不支持这一点。您有两种选择,要么使用 DataGrid,要么使用以下代码,该代码允许您在 ItemsControl 中使用 Grid,这将实现您的目标是在之后。

http://www.scottlogic.co.uk/blog/colin/2010/11/using-a-grid-as-the-panel-for-an-itemscontrol/

Silverlight does not support this. You have two options, either use a DataGrid or use the following code that allows you to use a Grid within an ItemsControl which will achieve what you are after.

http://www.scottlogic.co.uk/blog/colin/2010/11/using-a-grid-as-the-panel-for-an-itemscontrol/

缺⑴份安定 2024-11-14 17:27:18

@ColinE 是正确的,您无法绑定到 Grid,但您可以ObservableCollection 绑定到 DataGrid > 或 ItemsControl

@ColinE is correct, you can't bind to a Grid, but you can bind an ObservableCollection to a DataGrid or an ItemsControl.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文