6

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

3
  • 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. May 9, 2011 at 7:12
  • kindly see the above edited question tag
    – Ash
    May 9, 2011 at 8:52
  • +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.
    – UnclePaul
    Jul 20, 2012 at 16:05

3 Answers 3

5

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

2
  • @ Damian Mehers : Any other way to create listbox dynamically(without using XMAL) without using data template.
    – Vasu Ashok
    May 10, 2011 at 10:09
  • var listBox = new ListBox { ItemsSource = Assembly.GetCallingAssembly().GetTypes() }; LayoutRoot.Children.Add(listBox);
    – Damian
    May 10, 2011 at 10:28
5

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.

2
  • @ Dennis Delimarsky: Any other way to create listbox dynamically(without using XMAL) without using data template...
    – Vasu Ashok
    May 10, 2011 at 10:04
  • You can create the ListBox itself dynamically and add it to the visual tree. For the template, you will have to rely on XAML.
    – Den
    May 10, 2011 at 16:18
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.