How to create ListBox.Itemtemplate,datatemplate programatically in Windows Phone 7

12,165

Solution 1

You could use XamlReader.Load to dynamically load XAML in code-behind and cast it to a DataTemplate, later assigning it to the ItemTemplate. Here is an example.

Solution 2

You can't build templates from code - this can only be done from XAML.

If you dynamically generated the template XAML in your code, you could load it as described here.

I suspect you'll find yourself opening a whole can of worms if you go down this route. As an alternative, you could predefine a set of templates, and choose the correct one dynamically at runtime, as described here

Solution 3

Is the DataTemplate that you want to use the same for all listboxes or is that also dynamically generated? If it is the same for all of them then you could save it as a Style in your Resources and then just create the Listbox object dynamically and apply the style.

Share:
12,165
Ash
Author by

Ash

Updated on August 16, 2022

Comments

  • Ash
    Ash over 1 year

    I find that there is some item templates, data templates and binding in the .xaml file for listbox. Is there any way to create it in code behind?

    Is there any way to create data templates programataically?

    this is the XAML CODE,BUT I need in code behind using c# not in XAML,because am working in dynamic list box creation with adding itemtemplatem,datatemplate

    <ListBox Height="520" HorizontalAlignment="Left" Margin="0,6,0,0" Name="lstimge" VerticalAlignment="Top" Width="450" >
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <Image  Source="{Binding Image}" Width="150" Stretch="Uniform" HorizontalAlignment="Center" />
                                <TextBlock Text="{Binding FileName}" TextWrapping="Wrap" />
                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
    

    Kindly give the solution

    Thanks, Ashok

    • Veeru
      Veeru almost 13 years
      Perhaps you could elaborate why you wish to do this? In general, the concept of MVVM is to be able to define your view without any code.
    • Ash
      Ash almost 13 years
      kindly see the above edited question tag
    • UnclePaul
      UnclePaul almost 12 years
      +1 for this very good question. I absolutely cannot fathom why MS has this obvious distaste for well written, fast, strongly typed code with very good IDE support and instead forces us to use a certain design pattern, play in a locked down sandbox, obey their style guides and now also harasses us with these bloated and unflexible XML files.
  • Asta ni enohpi
    Asta ni enohpi almost 13 years
    @ Dennis Delimarsky: Any other way to create listbox dynamically(without using XMAL) without using data template...
  • Asta ni enohpi
    Asta ni enohpi almost 13 years
    @ Damian Mehers : Any other way to create listbox dynamically(without using XMAL) without using data template.
  • Damian
    Damian almost 13 years
    var listBox = new ListBox { ItemsSource = Assembly.GetCallingAssembly().GetTypes() }; LayoutRoot.Children.Add(listBox);
  • 0x2D
    0x2D almost 13 years
    You can create the ListBox itself dynamically and add it to the visual tree. For the template, you will have to rely on XAML.