WPF: Disable ListBox, but enable scrolling

前端 未结 12 2069
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-05 05:33

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

相关标签:
12条回答
  • 2021-01-05 05:58

    I used this solution, it's really easy and works perfectly:

    For every SurfaceListBoxItem item you put in the Listbox, do this:

    item.IsHitTestVisible = false;
    
    0 讨论(0)
  • 2021-01-05 06:02

    A complete answer using http://www.codeproject.com/Tips/60619/Scrollable-Disabled-ListBox-in-WPF

    The Style:

    <Style TargetType="{x:Type local:CustomListBox}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:CustomListBox}">
                    <Border SnapsToDevicePixels="true" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="1">
                        <ScrollViewer IsEnabled="True">
                            <ItemsPresenter IsEnabled="{Binding Path=IsEnabledWithScroll,  RelativeSource={RelativeSource TemplatedParent}}"  SnapsToDevicePixels="{TemplateBinding  UIElement.SnapsToDevicePixels}"/>
                        </ScrollViewer>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    

    The class

    public class CustomListBox : ListBox
    {
        public bool IsEnabledWithScroll
        {
            get { return (bool)GetValue(IsEnabledWithScrollProperty); }
            set { SetValue(IsEnabledWithScrollProperty, value); }
        }
    
        public static readonly DependencyProperty IsEnabledWithScrollProperty =
            DependencyProperty.Register("IsEnabledWithScroll", typeof(bool), typeof(CustomListBox), new UIPropertyMetadata(true));
    }
    

    Then instead of setted IsEnabled on the ListBox, use IsEnabledWithScroll instead. Scrolling will work if the listbox is enabled or disabled.

    0 讨论(0)
  • 2021-01-05 06:06

    sorry it has been almost two years, but I think this solution that uses DataTrigger is even simpler. How to disable a databound ListBox item based on a property value?

    0 讨论(0)
  • 2021-01-05 06:10

    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}" ..... />
    
    0 讨论(0)
  • 2021-01-05 06:12

    I found a very simple and straight forward solution working for me, I hope it would do for you as well

    <ListBox.ItemContainerStyle>
    <Style TargetType="{x:Type ListBoxItem}">
         <Setter Property="Focusable" Value="False"/>
     </Style>
    

    0 讨论(0)
  • 2021-01-05 06:13

    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.

    0 讨论(0)
提交回复
热议问题