gpt4 book ai didi

.net - 将二维集合绑定(bind)到某个控件

转载 作者:行者123 更新时间:2023-12-03 10:24:33 25 4
gpt4 key购买 nike

我想绑定(bind)一个二维集合——复杂数据类型的集合。使控件看起来像富文本框的 (n) 个垂直列表(列)。

每个列表将具有相同数量的记录。

一种方法是将数据从 viewmodel 传递到 View ,然后以编程方式在 xaml 后面的代码中创建这些列表。但我不想那样做,有没有更简单的?

最佳答案

只需使用数据绑定(bind)和数据模板:

<ItemsControl ItemsSource="{Binding MainCollection}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<ItemsControl ItemsSource="{Binding .}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<RichTextBox Text="{Binding .}"/>
</ItemsControl.ItemTemplate>
</ItemsControl>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>

所以外 ItemsControl绑定(bind)到集合的集合并以垂直 StackPanel 呈现.对于该集合中的每个集合,都有一个内部 ItemsControl .各内 ItemsControl将其集合中的每个项目显示为 RichTextBox在水平 StackPanel .

显然,您将需要适本地修复绑定(bind)路径,并根据您的特定场景进行必要的调整。

关于.net - 将二维集合绑定(bind)到某个控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5397153/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com