Thursday, January 3, 2013

Change ScrollViewer Style of ListBox in Silverlight

To change Silverlight ListBox ScrollViewer default style, you have to change its ControlTemplate.

Please follow these steps to change Default style of ListBox Scrollviewer.

1.  Let's say your ScrollViewer Style : MyScrollViewerStyle

2.  Change ControlTemplate, like this :

    <ListBox.Template>
 <ControlTemplate TargetType="ItemsControl">
      <ScrollViewer Style="{StaticResource MyScrollViewerStyle}">
  <ItemsPresenter />
      </ScrollViewer>
 </ControlTemplate>
     </ListBox.Template>
   
here in ControlTemplate add ScrollViewer and set your style, and don't forget to add <ItemPresenter/> within ScrollViewer.
because without ItemPresenter your DataItems is not visible in ListBox.

3. Full Source.

 <ListBox ItemsSource="{Binding EmployeeCollection}" 
                 ScrollViewer.VerticalScrollBarVisibility="Auto">
 <ListBox.ItemContainerStyle>
  <Style TargetType="ListBoxItem">
   <Setter Property="HorizontalContentAlignment" 
                                Value="Stretch" />
  </Style>
 </ListBox.ItemContainerStyle>
 <ListBox.Template>
  <ControlTemplate TargetType="ItemsControl">
       <ScrollViewer Style="{StaticResource MyScrollViewerStyle}">
      <ItemsPresenter />
        </ScrollViewer>
  </ControlTemplate>
 </ListBox.Template>
 <ListBox.ItemTemplate>
  <DataTemplate> 
                    .......
                     Your Data to Format
                    .......        
                </DataTemplate>
        </ListBox.ItemTemplate> 
 </ListBox>   
 


No comments:

Post a Comment