WPF:禁用ListBox,但启用滚动

Mat*_*ggs 8 wpf listbox scrollbar

整个上午都在撞我的头.

基本上,我有一个列表框,我想让人们不要在长时间运行的过程中更改选择,但允许他们仍然滚动.

解:

所有答案都很好,我选择了吞咽老鼠事件,因为那是最直接的.我将PreviewMouseDown和PreviewMouseUp连接到单个事件,该事件检查了我的backgroundWorker.IsBusy,如果将事件args的IsHandled属性设置为true.

Job*_*Joy 8

如果你查看ListBox的控件模板,里面有一个ScrollBar和ItemsPresenter.因此,禁用ItemsPresenter,您将轻松获得此功能.使用ListBox上的波纹管样式,你很高兴.

    <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>
Run Code Online (Sandbox Code Playgroud)

在ListBox上使用Style

<ListBox    Style="{DynamicResource disabledListBoxWithScroll}" ..... />
Run Code Online (Sandbox Code Playgroud)


Yes*_*ke. 1

诀窍是不要真正禁用。禁用将锁定滚动框中的所有消息。

在长时间操作期间,使用其 .ForeColor 属性使列表框中的文本变灰,并吞掉所有鼠标单击。这将模拟禁用控件并允许无障碍滚动。

  • 这种方法的问题在于键盘仍然可以用来进行选择。 (4认同)