【问题标题】:Itemscontrol within Itemscontrol and binding them with custom dataItemscontrol 中的 Itemscontrol 并将它们与自定义数据绑定
【发布时间】: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 做点什么吗?
  • 如果它可以在没有名字的情况下工作,那么对我来说就没有必要了。

标签: c# .net wpf


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 2011-03-17
    • 2016-05-11
    • 1970-01-01
    • 1970-01-01
    • 2018-09-30
    • 2011-02-16
    • 2014-03-08
    • 1970-01-01
    相关资源
    最近更新 更多