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

I find that there are some element templates, data templates, and a binding in the .xaml file for the list. Is there any way to create it in code?

Is there a way to create data templates programmatically?

this is a XAML CODE, BUT I need code using C #, but not in XAML, because I work in creating a dynamic list with the addition of 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> 

Please give a decision

Thanks Ashok

+6
source share
3 answers

You cannot create templates from code - this can only be done from XAML.

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

I suspect you will find that you can open a whole can of worms if you go this route. Alternatively, you can predefine a set of templates and choose dynamically dynamically at runtime, as described here

+5
source

You can use XamlReader.Load to dynamically load XAML into code and put it in a DataTemplate, and then assign it an ItemTemplate. Here is an example.

+5
source

Is a DataTemplate that you want to use the same for all lists, or is it also dynamically generated? If everything is the same for them, you can save it as a Style in your resources , and then simply create a Listbox object dynamically and apply the style.

+1
source

Source: https://habr.com/ru/post/887596/


All Articles