سؤال

I created a dynamic list with a few expanderviews in them.

Here is my code:

  foreach (var TransactionType in WorkflowBase.ViewModel_WFTransaction.AvailableTransactionTypes)
        {
            ExpanderView newDoc = new ExpanderView();
            newDoc.FontSize = 32;
            newDoc.Margin = new Thickness() { Bottom = 10, Left = 13, Right = 10, Top = 15 };

            newDoc.Header = TransactionType.ProcessName;

            foreach (var role in TransactionType.ListOfRoles)
            {
                TextBlock roleItem = new TextBlock();
                roleItem.FontSize = 28;
                roleItem.Margin = new Thickness() { Bottom = 4, Left = 4, Right = 4, Top = 4 };
                roleItem.Text = role;
                roleItem.Tag = TransactionType;
                roleItem.Tap += roleItem_Tap;
                newDoc.Items.Add(roleItem);
            }

            NewTransactionsPanel.Children.Add(newDoc);

        }

As you can see inside the expanderview I've put in textblocks to show my data, now what I want to do is when I click on one of those textblocks, I want to get the index of that item in list and also the index of the expanderview in the list. I have no idea how to go about doing this.

Here is a picture of the list after loading it to give a better idea. Expanderviews

For example, if I click on 'line manager' under the 'po Mobile' heading, I now want the index of the line manager textblock in that list of 2 items, and I want the index of the po mobile in the bigger list.

This is all dynamic data, so the amount of items in list will change.

SOLVED : From the advice Kookiz gave me this is what i did, 1. i added all my data i needed in the textblock tag -see codesnippit below, 2.then i added it to my navigation ur, 3.and just queried it out on my other page load

1.

 roleItem.Tag = 
                    TransactionType.ListOfRoles.IndexOf(role) + "," 
                    + WorkflowBase.ViewModel_WFTransaction.AvailableTransactionTypes.IndexOf(TransactionType) 
                    + "," + TransactionType.FormName 
                    + "," + TransactionType.ProcessID
                    + "," + TransactionType.ActivityID
                    + "," + TransactionType.FormID;

2.

 this.NavigationService.Navigate(new Uri("/UI/Documents/frmGenericNewTransaction.xaml?Formname="
                                        + FormName.ToString() +
                                        "&Role=" + roleindex +
                                        "&Processindex="+ processindex +
                                        "&Processid=" + ProcID +
                                        "&Activityid=" + ActID +
                                        "&Formid=" + FormID +
                                        "&Roleid=" + RoleName, UriKind.Relative));  

3.

NavigationContext.QueryString.TryGetValue("Activityid", out ActivityID);
NavigationContext.QueryString.TryGetValue("Roleid", out RoleName);
NavigationContext.QueryString.TryGetValue("Formid", out FormID);
هل كانت مفيدة؟

المحلول

The obvious way would be to browse the child controls of the expanders until you find your textblock.

A more elegant way would be to store the index in the Tag property of the textblock. You're already using it to store your TransactionType, so I guess you know how it works. Just use a more complex type to store the index along with your transaction type. But then, your index could become outdated if you add new elements before existing ones, so it depends on how you're going to update your items.

Last but not least, instead of manually adding each item to your expanderview, you could set an ItemTemplate and assign your list to the ItemsSource property of the expanderview. Then, in the Tap event, you could retrieve your whole role object from the datacontext of the textblock. From there, it's up to you to architecture your code to retrieve the information you want. Basically, instead of trying to retrieve the information from the UI, make your UI match your object structure, and work your way from there.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top