locked
How to add controls to Datatemplate dynamically inside flipview

    Question

  • Hello

    Is there any way to add controls dynamically inside flipview datatemplate.

    Thanks in advance.

    Tuesday, February 18, 2014 7:34 AM

Answers

  • this going be an hardone; you can do with the VisualTreeHelper to get the container in the flipview. or an loaded event

    for example:

      <FlipView>
                <FlipView.ItemTemplate>
                    <DataTemplate>
                        <Grid Loaded="Grid_Loaded" />
                    </DataTemplate>
                </FlipView.ItemTemplate>
            </FlipView>

    in the code you can do this:

    private void Grid_Loaded(object sender, RoutedEventArgs e)
            {
                Grid g = (Grid)sender;
                g.Children.Add(new TextBlock());
            }

    ofcourse you can save the Grid in a global variable so you can access it later too.

    BUT i think its nicer to do with binding. do you have something like a list that you try to present in your flipview?


    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    • Marked as answer by zee_patel Thursday, February 20, 2014 10:20 AM
    Tuesday, February 18, 2014 11:01 AM

All replies

  • this going be an hardone; you can do with the VisualTreeHelper to get the container in the flipview. or an loaded event

    for example:

      <FlipView>
                <FlipView.ItemTemplate>
                    <DataTemplate>
                        <Grid Loaded="Grid_Loaded" />
                    </DataTemplate>
                </FlipView.ItemTemplate>
            </FlipView>

    in the code you can do this:

    private void Grid_Loaded(object sender, RoutedEventArgs e)
            {
                Grid g = (Grid)sender;
                g.Children.Add(new TextBlock());
            }

    ofcourse you can save the Grid in a global variable so you can access it later too.

    BUT i think its nicer to do with binding. do you have something like a list that you try to present in your flipview?


    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    • Marked as answer by zee_patel Thursday, February 20, 2014 10:20 AM
    Tuesday, February 18, 2014 11:01 AM
  • Hi,

    Have you considered using DataTemplateSelector? It gives you flexibility to select a DataTemplate at runtime. I am not sure if you can add controls to existing DataTemplate. 

    -Sagar

    Tuesday, February 18, 2014 11:12 AM
  • Hi Dave,

    Thanks for the reply. My flipview is bind using Itemsource property.

    I tried your code , it's work fine with new instances of the UIElement, but when I am adding the following code it throws "Element is already the child of another element" exception.

     var flipViewItem = myFlipView.ItemContainerGenerator.ContainerFromIndex(myFlipView.SelectedIndex) as FlipViewItem;
                        Windows.UI.Xaml.Controls.Grid grd = FindChild<Windows.UI.Xaml.Controls.Grid>(flipViewItem, "innerGrid");
                        Windows.UI.Xaml.Controls.Grid grd1 = FindChild<Windows.UI.Xaml.Controls.Grid>(flipViewItem, "grdTextBox");
    
    grd.Children.Add(grd1);
    I need to add the same control again because do to virtualization my controls gets disappear after moving to certain items.


    • Edited by zee_patel Tuesday, February 18, 2014 1:44 PM
    Tuesday, February 18, 2014 1:43 PM
  • hi. you cant add it more then once. then you need to remove it first from its parents and then add it again.

    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    Tuesday, February 18, 2014 1:45 PM
  • I tried grd.Children.Remove(grd1); but its throwing same exception .

    I am again adding reference of grd1 which I removed from the grid . is it Valid? can u suggest some good way to do it?


    • Edited by zee_patel Tuesday, February 18, 2014 1:59 PM
    Tuesday, February 18, 2014 1:59 PM