我是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>();
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
我将所有必要的数据添加到tokenList并执行以下操作:
ParentControl.ItemsSource = tokenList;
Run Code Online (Sandbox Code Playgroud)
毫无疑问,为ParentControl正确绑定了所有内容.现在我想用列表currentItems填充它的子Itemcontrol.由于我有多个父控件,我需要动态命名到子项Itemscontrol.此外,我无法从代码访问子ItemsControl.
如何做到这一点?我不知道ItemsControl本身是否存在子ItemsControl.
请建议我解决此问题的任何解决方案或替代解决方案.
编辑:我希望用户看到令牌号,时间等,以及要由交互式按钮列表替换的项目列表.
更新:略微修改了XAML内容.子项ItemsControl在DataTemplate中.
你不应该在itemscontrol中命名,如果你想让itemscontrol触发某些东西,请使用itemtemplate中的按钮:
<ItemsControl ItemsSource="{Binding tokenList}">
<ItemsControl ItemsSource="{Binding currentItems}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Content="{Binding TokenNo}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)
你可以添加你需要在datatemplate标签中显示的任何内容.在我的示例中,按钮上的文本将是它在父节点中找到的TokenNo,即通过tokenlist绑定显示的Token.