Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF: Disable ListBox, but enable scrolling

Been banging my head against this all morning.

Basically, I have a listbox, and I want to keep people from changing the selection during a long running process, but allow them to still scroll.

Solution:

All the answers were good, I went with swallowing mouse events since that was the most straight forward. I wired PreviewMouseDown and PreviewMouseUp to a single event, which checked my backgroundWorker.IsBusy, and if it was set the IsHandled property on the event args to true.

like image 398
Matt Briggs Avatar asked Feb 09 '09 17:02

Matt Briggs


4 Answers

If you look in to the control template of the ListBox, there is a ScrollBar and ItemsPresenter inside. So Make the ItemsPresenter Disabled and you will get this easily. Use the bellow Style on the ListBox and you are good to go.

    <Style x:Key="disabledListBoxWithScroll" TargetType="{x:Type ListBox}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ListBox}">
                    <Border x:Name="Bd" SnapsToDevicePixels="true" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="1">
                        <ScrollViewer Padding="{TemplateBinding Padding}" Focusable="false">
                            <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" IsEnabled="False" IsHitTestVisible="True"/>
                        </ScrollViewer>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
                        </Trigger>
                        <Trigger Property="IsGrouping" Value="true">
                            <Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

On the ListBox use the Style

<ListBox    Style="{DynamicResource disabledListBoxWithScroll}" ..... />
like image 70
Jobi Joy Avatar answered Nov 08 '22 00:11

Jobi Joy


I found that putting a disabled ListBox in a ScrollViewer with auto scrolling enabled gives the desired effect.

like image 20
Konrad Avatar answered Nov 08 '22 00:11

Konrad


The trick is to not really disable. Disabling will lock out all messages from the scroll box.

During the long operation, gray out the text in the list box using its .ForeColor property and swallow all mouse clicks. This will simulate disabling the control and allow scrolling unimpeded.

like image 1
Yes - that Jake. Avatar answered Nov 07 '22 23:11

Yes - that Jake.


While it's for Silverlight, maybe this blog post would help you get going in the right direction? Silverlight No Selection ListBox and ViewBox

like image 1
Andy Avatar answered Nov 07 '22 22:11

Andy