Monday, December 3, 2012

Silverlight Listbox scrolling only works if i set the height

Silverlight Listbox scrolling only works if i set the height

i have a listbox and i am trying to make the scrolling work without having to set the height, is this possible? Thanks. Below is the code. The scrolling does not work.

 <ListBox Name="EmployeeListBox" Background="Transparent"                                SelectionMode="Single"                              ItemsSource="{Binding Employees, Mode=TwoWay}"  >         <ListBox.ItemTemplate>             <DataTemplate >                 <StackPanel>                     <Grid>                         <Grid.ColumnDefinitions>                             <ColumnDefinition Width="120" />                             <ColumnDefinition Width="140" />                         </Grid.ColumnDefinitions>                         <Grid.RowDefinitions>                             <RowDefinition Height="Auto"/>                             <RowDefinition Height="Auto"/>                         </Grid.RowDefinitions>                         <TextBlock  Grid.Column="0" Grid.Row="0" FontWeight="Bold" Text="Name:" />                         <TextBlock x:Name="TextBlock1"  Grid.Column="1" Grid.Row="0"                                          Text="{Binding Name}" />                     </Grid>                 </StackPanel>             </DataTemplate>         </ListBox.ItemTemplate>     </ListBox> 

and viewmodel:

 public class EmployeeDataContext {     public List<Employee> Employees { get; set; }     public EmployeeDataContext()     {         GetEmployeeList();     }     private void GetEmployeeList()     {         Employees = new List<Employee>();         for (int i = 0; i < 100; ++i)         {             Employees.Add(new Employee() { Name = "Gema Arterton" });         }     } } public class Employee {     public string Name { get; set; } } 

Answers & Comments...

Answer: 1

(sorry for my bad english)

It depends too on what control is the listbox inside. If your listbox is inside a Grid, it should take all the space of that grid - if it is inside a stackpanel it will use the least amount os space possible so you will have to use fixed height/width. Try this:

<UserControl...>     <Grid>       <ListBox HorizontalAlignment="Stretch" VerticalAlignment="Stretch">                </ListBox>     </Grid> </UserControl/> 

The listbox should use all screen size and show vertical scrollbars if there is enough items.

by : Leohttp://stackoverflow.com/users/170492

Answer: 2

You can set the height in scrollviewer

  <ScrollViewer VerticalScrollBarVisibility="Visible" Height="480">    <Grid Margin="0,0,0,50">      </Grid>  </ScrollViewer> 
by : charulatha krishnamoorthyhttp://stackoverflow.com/users/1711732




No comments:

Post a Comment

Send us your comment related to the topic mentioned on the blog