WPF: Disable ListBox, but enable scrolling

前端 未结 12 2264
佛祖请我去吃肉
佛祖请我去吃肉 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 06:02

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

    The 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.

提交回复
热议问题