none
Add Dynamic Controls

    Question

  • Is it Possible to add controls dynamically according to a datasource using DataTemplate from XAML? The type and the number of the controls will vary according to the Data. Any Idea?
    Monday, October 29, 2007 4:28 AM

Answers

  •    This is definitely possible with the power of WPF's DataTemplate. Here is a XAMLPad ready sample:

     

    Code Block

    <Page

            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

            xmlns:sys="clr-namespace:System;assembly=mscorlib">

              <Page.Resources>

                       <x:Array Type="{x:Type sys:Object}" x:Key="data">

                                 <sys:String>String</sys:String>

                                 <sys:Boolean>True</sys:Boolean>

                                 <sys:Boolean>False</sys:Boolean>

                       </x:Array>

                       <DataTemplate DataType="{x:Type sys:Boolean}">

                                 <CheckBox Content="CheckBox" IsChecked="{Binding Mode=OneWay}"/>

                       </DataTemplate>

                       <DataTemplate DataType="{x:Type sys:String}">

                                 <TextBox Text="{Binding Mode=OneWay}"/>

                       </DataTemplate>

              </Page.Resources>

              <StackPanel>

                       <ItemsControl ItemsSource="{StaticResource data}"/>

              </StackPanel>

    </Page>

     

         ItemsControl's ItemContainerGenerator will choose the right data template based on the Type of the data object you want to display.

     

       Hope this helps

    Monday, November 05, 2007 9:05 AM
  • You can use the ItemTemplateSelector property of an ItemsSource and put your logic in there for choosing the DataTemplate you want to use. For an example see http://www.beacosta.com/blog/?p=19 that shows (among other things) how to use the ItemTemplateSelector

    I hope this helps,
    Tasos
    Monday, November 26, 2007 11:14 PM

All replies

  • Can you please explain your scenario a little more?  What does the data look like and how do you want it to be represented?  In other words, what controls do you want to be added and how do those controls relate to the data?

     

    Monday, October 29, 2007 4:57 AM
  •     As Dr. WPF has pointed out, you'd better elaborate here a bit, in particular, what type of application are you building? and what type of data visualization scenario do you want to enable?

     

        Thanks

     

    Thursday, November 01, 2007 4:12 AM
  • I want to create a form from which I will get user input; but the controls in the form are not static. The number and type of control is completely dynamic. Its determined by value that I bind. For example, If a field type is bool I should add a checkbox, if a string type a textbox should be added.

    Is it Possible with templates? If possible how to read values from the controls inside a template?

     

    Thanks,

    Rajesh

    Friday, November 02, 2007 1:00 PM
  •    This is definitely possible with the power of WPF's DataTemplate. Here is a XAMLPad ready sample:

     

    Code Block

    <Page

            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

            xmlns:sys="clr-namespace:System;assembly=mscorlib">

              <Page.Resources>

                       <x:Array Type="{x:Type sys:Object}" x:Key="data">

                                 <sys:String>String</sys:String>

                                 <sys:Boolean>True</sys:Boolean>

                                 <sys:Boolean>False</sys:Boolean>

                       </x:Array>

                       <DataTemplate DataType="{x:Type sys:Boolean}">

                                 <CheckBox Content="CheckBox" IsChecked="{Binding Mode=OneWay}"/>

                       </DataTemplate>

                       <DataTemplate DataType="{x:Type sys:String}">

                                 <TextBox Text="{Binding Mode=OneWay}"/>

                       </DataTemplate>

              </Page.Resources>

              <StackPanel>

                       <ItemsControl ItemsSource="{StaticResource data}"/>

              </StackPanel>

    </Page>

     

         ItemsControl's ItemContainerGenerator will choose the right data template based on the Type of the data object you want to display.

     

       Hope this helps

    Monday, November 05, 2007 9:05 AM
  • Thanks a Lot. It works...
    Monday, November 05, 2007 11:23 AM
  • Is there any way to create controls based on my own custom data type because I am having Strings and Boolean fields on my screen for some other purpose.

    Thanks,
    Kishore
    Tuesday, November 13, 2007 5:13 AM
  •    You can put those DataTemplates into the Resources of the control where you want to dynamically create content, For instance:

    Code Block

    <Page

      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="
    http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:sys="clr-namespace:System;assembly=mscorlib">
     <StackPanel>
      <ContentControl Name="DataTemplateApplyHere">
       <ContentControl.Resources>
        <DataTemplate DataType="{x:Type sys:DateTime}">
         <TextBlock Text="{Binding}" Foreground="Red" FontSize="40" FontWeight="Bold"/>
        </DataTemplate>
       </ContentControl.Resources>
       <x:Static Member="sys:DateTime.Now"/>
      </ContentControl>
      <ContentControl Name="DataTemplateNotApplyHere">
       <x:Static Member="sys:DateTime.Now"/>
      </ContentControl>
     </StackPanel>
    </Page>

     

     

       Hope this helps
    Tuesday, November 13, 2007 8:50 AM
  • OK, so how do I dynamically change the DataTemplate based on a value in the object

     

    eg: I have a Patient Object that can be "Male" or "Female" patient.gender = "M" or "F" I have 2 DataTemplates "MaleTemplate", "FemaleTemplate"

     

    How do I get the ListBox that is displaying them to show the appropriate DataTemplate.

     

    Thanks, really appreciate the help

    Monday, November 26, 2007 11:33 AM
  • You can use the ItemTemplateSelector property of an ItemsSource and put your logic in there for choosing the DataTemplate you want to use. For an example see http://www.beacosta.com/blog/?p=19 that shows (among other things) how to use the ItemTemplateSelector

    I hope this helps,
    Tasos
    Monday, November 26, 2007 11:14 PM
  • Tuesday, November 27, 2007 8:17 PM
  • HI Marco Zhou, 

    I Heart Fully , Thank you for your nice Material………..

    -------------------------------------------------------------------------------------- 

    Greetings, 

     Ayyappan,

    Software Engineer ,

    Chennai,

    Tamil Nadu,

    INDIA.

    http://www.banyansoft.blogspot.com/

     

    Wednesday, February 16, 2011 7:09 AM