Shan
Shan

Reputation: 423

Creating Image control dynamically inside a listbox based on number of items user wants using C#

Simple question but I am totally confused. I am developing a wp7 app using C#. I want a listbox with input number of image item which source should be same i.e. the list box should contain 'n' Image control with source set to a single image where 'n' is number of Listbox Item enter by user. e.g. If the user input '10', then the listbox should have ten items. I want the listbox ItemsPanelTemplate as Wrap-panel. Can somebody suggest me how to get this?

Upvotes: 0

Views: 364

Answers (1)

nkchandra
nkchandra

Reputation: 5557

Define a ListBox in your XAML something like this

<ListBox x:Name="ListBoxImages">
    <ListBox.ItemTemplate>
        <DataTemplate>
           <StackPanel>
              <Image Source="{Binding Imagesource}" Width="300"/>
           </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

and then set its Source in the code behind like this

int noOfImages = 10; //Take the input from user
List<ImageClass> imageList = new List<ImageClass>();
for(int i=0; i<noOfImages; i++)
imageList.Add(new ImageClass() { Imagesource = "/user.jpg" });

ListBoxImages.ItemsSource = imageList; //Set the source of the listbox here

where ImageClass is,

public class ImageClass
{
    public String Imagesource { get; set; }
}

The above is a sample for your understanding. Please customize wisely to suit your needs

Upvotes: 2

Related Questions