Reputation: 385
I am very new in WPF. I have been trying to do the following:
Following is the structure of my data:
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>
I added the all the necessary data to the tokenList and did the following:
ParentControl.ItemsSource = tokenList;
No doubt everything binded properly for the ParentControl. Now I want to fill it's child Itemcontrol with the list currentItems. Since there's going to me multiple parent control, I needed dynamic naming to the children Itemscontrol. Further I cannot access the children ItemsControl from code.
How to make that possible? I don't know if there exist child ItemsControl of ItemsControl itself.
Please suggest me any solution or alternative solution to this problem.
EDIT: I want the user to see the Token Number, time, etc with the list of items that is to be replaced by list of interactive buttons.
Update: slightly modified the XAML content. The children ItemsControl is within the DataTemplate.
Upvotes: 4
Views: 4978
Reputation: 5262
You shouldn't need naming in the itemscontrol, if you want the itemscontrol to trigger something, use buttons in the itemtemplate:
<ItemsControl ItemsSource="{Binding tokenList}">
<ItemsControl ItemsSource="{Binding currentItems}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Content="{Binding TokenNo}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ItemsControl>
You vould add whatever you need to show in the datatemplate tag. In my example, the text on the button will be the TokenNo it finds in the parent, being the Token that's shown through the tokenlist binding.
Upvotes: 6