Itemscontrol中的Itemscontrol并使用自定义数据绑定它们

时间:2012-08-14 06:38:17

标签: c# .net wpf

我是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中。

1 个答案:

答案 0 :(得分:6)

你不应该在itemscontrol中命名,如果你想让itemscontrol触发某些东西,请使用itemtemplate中的按钮:

<ItemsControl ItemsSource="{Binding tokenList}">
   <ItemsControl ItemsSource="{Binding currentItems}">
       <ItemsControl.ItemTemplate>
           <DataTemplate>
               <Button Content="{Binding TokenNo}"/>
           </DataTemplate>
       </ItemsControl.ItemTemplate>
   </ItemsControl>
</ItemsControl>

你可以在datatemplate标签中添加你需要显示的内容。在我的示例中,按钮上的文本将是它在父级中找到的TokenNo,即通过tokenlist绑定显示的Token。

相关问题