locked
Clone ListView and add to exsiting Listview in a Windows 8.1 app

    Question

  • I am trying to copy a ListView and add to an existing ListView (creating a sub-list).  here is my XAML for the ListView:

    <ListView x:Name="ListOfTasks" IsItemClickEnabled="False" HorizontalContentAlignment="Stretch" HorizontalAlignment="Stretch" Grid.Column="1" Margin="0,0,0,60" CanReorderItems="True" AllowDrop="True" CanDragItems="True" ItemContainerStyle="{StaticResource ListViewItemStyle2}"> <ListView.ItemTemplate> <DataTemplate> <Grid> <Border Background="{ThemeResource ListViewItemPlaceholderBackgroundThemeBrush}"> </Border> <TextBlock HorizontalAlignment="Stretch" Margin="5,15,0,15" Text="{Binding Section}" Foreground="{ThemeResource ListViewItemOverlayForegroundThemeBrush}" Style="{StaticResource TitleTextBlockStyle}" TextWrapping="WrapWholeWords" FontSize="18" /> <BitmapIcon x:Name="ShowTasks" Tag="{Binding}" Height="25" Width="25" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,10,10,0" UriSource="Assets/CollapseArrow.png" Tapped="ShowTasks_Click" DataContextChanged="ShowTasks_DataContextChanged"/> <ListView Margin="20,50,0,0" ItemsSource="{Binding Tasks}" Visibility="{Binding showTasks}" HorizontalAlignment="Stretch" CanReorderItems="True" AllowDrop="True" CanDragItems="True" ItemContainerStyle="{StaticResource ListViewItemStyle2}"> <ListView.ItemTemplate> <DataTemplate> <Grid> <Border Background="{ThemeResource ListViewItemPlaceholderBackgroundThemeBrush}"> </Border> <TextBlock VerticalAlignment="Center" HorizontalAlignment="Stretch" Margin="5,15,0,15" Text="{Binding Task}" Foreground="{ThemeResource ListViewItemOverlayForegroundThemeBrush}" Style="{StaticResource TitleTextBlockStyle}" TextWrapping="WrapWholeWords" FontSize="18" /> <TextBox TextAlignment="Center" MaxHeight="22" VerticalAlignment="Center" Margin="0,0,140,0" HorizontalAlignment="Right" x:Name="TaskPercent" FontSize="18" Text="{Binding Percent}" LostFocus="TaskPercent_LostFocus"/> <TextBlock VerticalAlignment="Center" Margin="0,0,142,0" HorizontalAlignment="Right" Text="%" FontSize="18" Foreground="Black" /> <Image VerticalAlignment="Center" Height="25" Width="25" Source="{Binding ImagePath}" HorizontalAlignment="Right" Margin="0,0,26,0" Visibility="{Binding Check}"/> <Button x:Name="details" Tag="{Binding Task}" Content="Details" Click="details_Click" HorizontalAlignment="Right" Margin="0,0,60,0" FontFamily="Global User Interface"/> </Grid> </DataTemplate> </ListView.ItemTemplate> </ListView> <ListView Margin="20,100,0,0" ItemsSource="{Binding subSections}" Visibility="{Binding showSubSections}" HorizontalAlignment="Stretch" CanReorderItems="True" AllowDrop="True" CanDragItems="True" ItemContainerStyle="{StaticResource ListViewItemStyle2}"> <ListView.ItemTemplate> <DataTemplate> <Grid> <Border Background="{ThemeResource ListViewItemPlaceholderBackgroundThemeBrush}"> </Border> <TextBlock HorizontalAlignment="Stretch" Margin="5,15,0,15" Text="{Binding Section}" Foreground="{ThemeResource ListViewItemOverlayForegroundThemeBrush}" Style="{StaticResource TitleTextBlockStyle}" TextWrapping="WrapWholeWords" FontSize="18" /> <BitmapIcon x:Name="ShowSubTasks" Tag="{Binding}" Height="25" Width="25" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,10,10,0" UriSource="Assets/CollapseArrow.png" Tapped="ShowSubTasks_Tapped" DataContextChanged="ShowTasks_DataContextChanged"/>

    // This is where the new/copied ListView will be added. </Grid> </DataTemplate> </ListView.ItemTemplate> </ListView> </Grid> </DataTemplate> </ListView.ItemTemplate> </ListView>


    Thursday, February 19, 2015 7:14 PM

Answers

  • I figured it out, here is how I did it.

    private void ShowSubTasks_Tapped(object sender, TappedRoutedEventArgs e)
            {
                var Section = (SectionType)((Windows.UI.Xaml.FrameworkElement)(sender)).Tag;
                Grid parent = new Grid();
                parent = (Grid)((Windows.UI.Xaml.FrameworkElement)(sender)).Parent;
                ListView temp = new ListView();
                temp.IsItemClickEnabled = false;
                temp.HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Stretch;
                temp.HorizontalContentAlignment = Windows.UI.Xaml.HorizontalAlignment.Stretch;
                temp.Margin = new Thickness(20, 50, 0, 0);
                temp.CanReorderItems = true;
                temp.AllowDrop = true;
                temp.CanDragItems = true;
                temp.ItemContainerStyle = ListOfTasks.ItemContainerStyle;
                temp.ItemTemplate = ListOfTasks.ItemTemplate;
                temp.Template = ListOfTasks.Template;
                temp.Name = "SubSection" + Section.SectionKey;
                temp.ItemsSource = Section.subSections;
                parent.Children.Add(temp);
            }



    • Marked as answer by DivCode Friday, February 20, 2015 4:17 PM
    • Edited by DivCode Friday, February 20, 2015 4:18 PM
    Friday, February 20, 2015 4:17 PM

All replies

  • Hi DivCode,

    So ... what is your question?

    --James


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, February 20, 2015 2:19 AM
    Moderator
  • I have the above ListView and when the user clicks on the icon named "ShowSubTasks" I would like to add another ListView as a sub-list using C#.

    here is the method I have so far:

     private void ShowSubTasks_Tapped(object sender, TappedRoutedEventArgs e)
            {
                var Section = (SectionType)((Windows.UI.Xaml.FrameworkElement)(sender)).Tag;
                Grid parent = new Grid();
                parent = (Grid)((Windows.UI.Xaml.FrameworkElement)(sender)).Parent;
                ListView temp = new ListView();
                temp = ListOfTasks;//ListView That I would like to clone/copy
                temp.Name = "SubSection" + Section.SectionKey;
                temp.ItemsSource = Section.subSections;
                parent.Children.Add(temp);
            }

    With this code I get an error that says the "WinRT information: Element is already the child of another element." I believe this is because I am just assigning an existing ListView to temp.

    So my question is how do I clone my existing ListView?

    Friday, February 20, 2015 3:35 PM
  • I figured it out, here is how I did it.

    private void ShowSubTasks_Tapped(object sender, TappedRoutedEventArgs e)
            {
                var Section = (SectionType)((Windows.UI.Xaml.FrameworkElement)(sender)).Tag;
                Grid parent = new Grid();
                parent = (Grid)((Windows.UI.Xaml.FrameworkElement)(sender)).Parent;
                ListView temp = new ListView();
                temp.IsItemClickEnabled = false;
                temp.HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Stretch;
                temp.HorizontalContentAlignment = Windows.UI.Xaml.HorizontalAlignment.Stretch;
                temp.Margin = new Thickness(20, 50, 0, 0);
                temp.CanReorderItems = true;
                temp.AllowDrop = true;
                temp.CanDragItems = true;
                temp.ItemContainerStyle = ListOfTasks.ItemContainerStyle;
                temp.ItemTemplate = ListOfTasks.ItemTemplate;
                temp.Template = ListOfTasks.Template;
                temp.Name = "SubSection" + Section.SectionKey;
                temp.ItemsSource = Section.subSections;
                parent.Children.Add(temp);
            }



    • Marked as answer by DivCode Friday, February 20, 2015 4:17 PM
    • Edited by DivCode Friday, February 20, 2015 4:18 PM
    Friday, February 20, 2015 4:17 PM