【发布时间】:2012-08-14 06:38:16
【问题描述】:
我是 WPF 的新手。我一直在尝试执行以下操作:
以下是我的数据结构:
public class TokenItems {public string items {get;set;} public int quantity {get;set}}
public class Token { public int tokenNo {get;set;} public string name {get;set;} public List<TokenItems> currentItems {get;set;}}
public List<Token> tokenList = new List<Token>();
XAML:
<ItemsControl Name="ParentControl">
....
<DataTemplate>
<TextBlock Content="{Binding tokenNo}"/>
<Itemscontrol Name="{Binding tokenNo}"> ?? I tried and want dynamic naming or any other solution for this {Binding name} just won't work i know.??
...
<DataTemplate>
<Button>
<Grid>
...
<TextBlock Content="{Binding items}/>
<TextBlock Content="{Binding quantity}/>
</DataTemplate>
</Itemscontrol>
....
</DataTemplate>
</Itemscontrol>
我将所有必要的数据添加到 tokenList 并执行了以下操作:
ParentControl.ItemsSource = tokenList;
毫无疑问,所有内容都为 ParentControl 正确绑定。现在我想用列表 currentItems 填充它的子 Itemcontrol。由于我需要多个父控件,因此我需要对子 Itemscontrol 进行动态命名。此外,我无法从代码中访问子 ItemsControl。
如何做到这一点?我不知道是否存在 ItemsControl 本身的子 ItemsControl。
请向我建议任何解决方案或替代解决方案。
编辑:我希望用户查看令牌编号、时间等以及要替换为交互式按钮列表的项目列表。
更新:稍微修改了 XAML 内容。子 ItemsControl 位于 DataTemplate 中。
【问题讨论】:
-
您能解释一下为什么需要将项目控件命名为“tokenNo”。您可以通过另一种方式访问该项目控件。另外,为什么要在 itemscontrol 中使用 itemscontrol。你能解释一下你想实现/向用户展示什么吗?
-
所以,如果我理解的话,您有一个要在第一个项目控件中显示的标记列表。那么第二个itemscontrol是显示tokenitems的列表?
-
Multiple tokenNo 将有多个项目分配给它们。多个 tokenNow 将有自己的 Sub ItemsControl,它必须具有唯一的名称,由 tokenNo 确定。
-
为什么要命名?你想让 itemscontrol 做点什么吗?
-
如果它可以在没有名字的情况下工作,那么对我来说就没有必要了。