簡體   English   中英

Itemscontrol中的Itemscontrol並將其與自定義數據綁定

[英]Itemscontrol within Itemscontrol and binding them with custom data

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

請向我建議解決此問題的任何解決方案或替代解決方案。

編輯:我希望用戶看到令牌號,時間等與要由交互式按鈕的列表替換的項目的列表。

更新:略微修改了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>

您應該添加任何需要顯示在datatemplate標記中的內容。 在我的示例中,按鈕上的文本將是它在父級中找到的TokenNo,即通過tokenlist綁定顯示的Token。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM