Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I want to add buttons to each item of a listbox "Tripslist" dynamically
For this i had created a 2 stack panels in a listbox data template.
1st stackpanel "DataRecords" used for displaying data records those are taken from a webservice.
Inside DataRecords stackpanel i had added another stackpanel "stkbuttons" .
The xaml tags for this is as shown below:
<StackPanel>
        <ListBox x:Name="TripList" Height="465" HorizontalAlignment="Left" VerticalAlignment="Top" Width="456" removed="White" Foreground="Blue">
           <ListBox.ItemTemplate>
             <DataTemplate>
                <Border BorderBrush="Black" BorderThickness="0,1,0,1">
                    <StackPanel x:Name="DataRecords" Orientation="Vertical" Width="456">
                This is for showing data records...
                  </StackPanel>
                  <StackPanel x:Name="stkbuttons"  Orientation="Vertical">                                                                                    This is to add buttons...
                                        </StackPanel>
</StackPanel>
                              </Border>
                                
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>
                </StackPanel>


To this stkbuttons stackpanel i am trying to add buttons dynamically based on criteria as shown below.
private void AddButtons()
       {
           for(int i=0; i<TripList.Items.Count;i++)
           {
               TripsList listitem =(TripsList) TripList.Items[i];
               if (string.Compare(listitem.TripAccept, "Accepted") == 0)
               {
                   Button btnClick = new Button();
                   btnClick.Content = "accept trip";
                   btnClick.Background = new SolidColorBrush(Colors.Green);this.TripList.ItemContainerGenerator.ContainerFromIndex(2) as ListBoxItem;
                   StackPanel mySpDataRecords = SearchVisualTree(TripList, "DataRecords");
                   if (mySpDataRecords != null)
                   {
                       StackPanel mySpButtons = SearchVisualTree(mySpDataRecords, "stkbuttons");
                   }
               }
           }
       }


Definition for SearchVisualTree() is as shown below:
private StackPanel SearchVisualTree(DependencyObject targetElement, string controlName)
       {
           var count = VisualTreeHelper.GetChildrenCount(targetElement);
           if (count == 0)
               return null;

           for (int i = 0; i < count; i++)
           {
               var child = VisualTreeHelper.GetChild(targetElement, i);
               if (child is StackPanel)
               {
                   StackPanel targetItem = (StackPanel)child;

                   if (targetItem.Name == controlName)
                   {
                       return targetItem;
                   }
               }
               else
               {
                   SearchVisualTree(child, controlName);
               }
           }
           return null;
       }


While excuting the below statement
StackPanel mySpDataRecords = SearchVisualTree(TripList, "DataRecords");

From below statement in SearchVisualtree()
var child = VisualTreeHelper.GetChild(targetElement, i);

Then i am getting children count as 1 and child as
{System.Windows.Controls.ScrollViewer}


So due to this the it comes from if condition as
if (child is StackPanel)


and from the for loop in SearchVisualTree() method.

Please let me know the solution for this.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900