Thursday, September 16, 2010

Enable scroll in a disabled ListBox in Silverlight

Sometimes we get a requirement that asks to enable scrolling on a long ListBox control while keeping its content disabled. The easy workaround for this is to wrap the ListBox with ScrollViewer control in Silverlight.

 

If you hide the border of ListBox and set the padding of ScrollViewer to zero, then the difference as compared to normal ListBox is unnoticeable e.g. see below

imageHere the XAML

<ScrollViewer VerticalScrollBarVisibility="Auto" Height="100" Width="110" Padding="0">
            <ListBox x:Name="lstCities" ItemsSource="{Binding}" ScrollViewer.VerticalScrollBarVisibility="Hidden" BorderThickness="0" IsEnabled="False">
            <ListBox.ItemTemplate>
                <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <CheckBox></CheckBox>
                            <TextBlock Text="{Binding Name}"></TextBlock>
                        </StackPanel>                        
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
        </ScrollViewer>


Technorati Tags: ,


LiveJournal Tags: