Question

Ive been researching for quite sometime and I couldnt find a tutorial explicitly guiding me how to do a simple listbox in silverlight. I just want to scroll up and down a list of data (lets say names of cities and lets say for now explicitly initialized). I tried decalring a listbox with textboxes inside of it, that doesnt seem to work since the spacing between the textboxes is huge. How would i go to solve this?

Was it helpful?

Solution

Make a listbox in the pages xaml

 <ListBox Margin="0,0,0,0" Height="550" Foreground="White" Name="lbSearchList">

                            <ListBox.ItemTemplate>
                        <DataTemplate>
                                    <Border BorderThickness="2" Margin="10,0,10,10" Width="460" BorderBrush="#FF404040">
    <TextBlock  FontSize="22" Foreground="Black" TextTrimming="WordEllipsis" Text="{Binding ClientName}"/>
                                    </Border>
                                </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>

Make a Client Class Like this

public class Client
{
public string ClientName{get;set;}
}

now in the code of the page where listbox resides

make a Global variable

List<Client> listClient=new List<Client>();

and in the page constructor

listClient.Add(new Client {ClientName="A"});
listClient.Add(new Client {ClientName="B"});
listClient.Add(new Client {ClientName="C"});
lbSearchList.ItemsSource = listClient;

I hope it works

OTHER TIPS

The most basic way to use ListBox :

<ListBox Margin="0,0,0,0" Name="lstCountries" >
    <ListBox.Items>
        <ListBoxItem Content="Brazil"/>
        <ListBoxItem Content="China"/>
        <ListBoxItem Content="India"/>
        <ListBoxItem Content="Switzerland"/>
        <ListBoxItem Content="Austria"/>
    </ListBox.Items>
</ListBox>

Adding items to ListBox from code :

lstCountries.Items.Add("Indonesia");
lstCountries.Items.Add("Thailand");
lstCountries.Items.Add("Australia");

Further technique will require defining ItemTemplate (as you can see in @Aman's answer), using DataBinding, etc. Tutorials are available on many websites/blogs online.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top