locked
Universal App: Using ExpandoObject and Grid View

    Question

  • I am developing an app where the number of columns in a grid view can change. Therefore I planned to use an ExpandoObject in order to generate properties at runtime.  The problem comes when trying to bind with a grid view.  The grid view seems to be unable to retrieve the dynamic properties and just outputs the toString method of the Expando Object.

    Anyone knows how to go about it?  As I have explained, the properties will be dynamic, therefore I cannot set up the grid view to bind to certain properties beforehand.

    Thanks for any help offered.

    Tuesday, September 16, 2014 8:39 PM

Answers

  • You need to provide an ItemTemplate to describe how the items should be rendered: what controls to use, and which properties to bind them to.

    Typically you would set this in Xaml, but you can build the DataTemplate in code and set it from code behind to match your dynamic properties.

    The easiest way to do this is to build a string for your template based on the custom properties and use a XamlReader to parse it:

                string xamlTemplate = "<DataTemplate xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"><StackPanel><TextBlock Text=\"{Binding Path=Owner}\"/></StackPanel></DataTemplate >";
                var template = Windows.UI.Xaml.Markup.XamlReader.Load(xamlTemplate);
                grvCircus.ItemTemplate = (DataTemplate)template;
                grvCircus.ItemsSource = testList;
    

    --Rob

    Monday, September 22, 2014 6:05 PM
    Owner

All replies

  • What is the ExpandoObject you are using? How are you binding it to the GridView? What does your data template look like?

    Can you share a minimal repro project on your OneDrive?

    --Rob

    Tuesday, September 16, 2014 11:56 PM
    Owner
  • The ExpandoObject is a new object in .Net 4 (see the following links: http://msdn.microsoft.com/en-us/library/system.dynamic.expandoobject(v=vs.110).aspx and http://msdn.microsoft.com/en-us/magazine/ff796227.aspx) which allows you to create a dynamic object and specify members which are not yet defined during runtime.

    here is a an example of what I am doing. I have 2 circuses who have different animals and I want create a grid showing the type of animals and the number of animals that each have.  Each circus may have different kinds of animals

    dynamic circus1 = new ExpandoObject();
    circus1.Owner = "Test 1";
    circus1.Tigers = 5;
    circus1.Lions = 7;

    dynamic circus2 = new ExpandoObject();
    circus2.Owner = "Test 2";

    circus2.Seals = 5;

    circus2.Tigers = 5;
    circus2.Lions = 7;

    List<dynamic> testList = new List<dynamic>();
    testList.Add(circus1);
    testList.Add(circus2);


    grdCircus.ItemsSource = testList;

    The xaml is pretty simple:

    <Page x:Name="MainPage"
        x:Class="Circus.MainInterface"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:Circus"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d">
    <Grid x:Name="grdMain" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <GridView HorizontalAlignment="Left" VerticalAlignment="Top" Name="grdCircus"/>
    </Grid>
    </Page>

    Wednesday, September 17, 2014 4:25 AM
  • If you need help with this can you please share a minimal repro project on your OneDrive?
    Thursday, September 18, 2014 11:32 PM
    Owner
  • Below please find a sample project.

    http://1drv.ms/1B0JzDj

    Regards

    Monday, September 22, 2014 4:51 AM
  • You need to provide an ItemTemplate to describe how the items should be rendered: what controls to use, and which properties to bind them to.

    Typically you would set this in Xaml, but you can build the DataTemplate in code and set it from code behind to match your dynamic properties.

    The easiest way to do this is to build a string for your template based on the custom properties and use a XamlReader to parse it:

                string xamlTemplate = "<DataTemplate xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"><StackPanel><TextBlock Text=\"{Binding Path=Owner}\"/></StackPanel></DataTemplate >";
                var template = Windows.UI.Xaml.Markup.XamlReader.Load(xamlTemplate);
                grvCircus.ItemTemplate = (DataTemplate)template;
                grvCircus.ItemsSource = testList;
    

    --Rob

    Monday, September 22, 2014 6:05 PM
    Owner
  • your solution worked.  Its a bit cumbersome but it gets the job done :)

    Thursday, October 9, 2014 12:01 PM