Click here to Skip to main content
Click here to Skip to main content

Scrollable Disabled ListBox in WPF

, 22 Feb 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
Many of you developing applications in WPF might have had the need to keep a ListBox disabled yet scroll it so that you can see the items in it. Say, in case you want to show the details to the user who can just view the details presented and not edit it, and some of the details might happen to...
Many of you developing applications in WPF might have had the need to keep a ListBox disabled yet scroll it so that you can see the items in it. Say, in case you want to show the details to the user who can just view the details presented and not edit it, and some of the details might happen to be in a ListBox.
 
As for the code, it is very simple. All I have done is created a custom control derived from ListBox, and added a Dependency Property, IsEnabledWithScroll. This property is directly bound with the IsEnabled property of the ItemsPresenter of the ListBox. By doing this, the scroll viewer is enabled even when the items are disabled.
 
I have changed the control template to reflect this, as shown below:
 

<Style TargetType="{x:Type local:ScrollableDisabledListBox}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Typelocal:ScrollableDisabledListBox}">
         <Border >
            <ScrollViewer IsEnabled="True"  >
                 <ItemsPresenter IsEnabled="{Binding Path= 
                  IsEnabledWithScroll, 
          RelativeSource={RelativeSource TemplatedParent}}" 
                          SnapsToDevicePixels="{TemplateBinding 
                             UIElement.SnapsToDevicePixels}"/>
                    </ScrollViewer>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
 
You can always go ahead modifying the template, but remember to bind the ItemsPresenter IsEnabled property to the IsEnabledWithScroll property.
 
With this control in place, you should not be using the IsEnabled property of the ListBox. Go ahead and use the IsEnabledWithScroll property to enable/disable your control. Of course, you have the scrollable option enabled.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Rahul P Nath
Software Developer
India India
I am a programmer.
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141223.1 | Last Updated 22 Feb 2010
Article Copyright 2010 by Rahul P Nath
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid