代码之家  ›  专栏  ›  技术社区  ›  levanovd

如何在列表框中禁用ScrollViewer?

  •  9
  • levanovd  · 技术社区  · 16 年前

    我有一个列表框。它有内部的ScrollViewer,所以我可以用鼠标滚轮滚动列表框内容。在我设置包含另一个ListBox的项模板(事实上,我有4个嵌套的ListBox=)之前,它可以正常工作。问题是内部ListBox的ScrollViewer窃取了旋转事件。有什么简单的方法可以防止这种行为吗?


    我有一个带有ItemContainerStyle的列表框,如下所示:

    <Style x:Key="ListBoxItemStyle" TargetType="{x:Type ListBoxItem}">
        <Setter Property="BorderBrush" Value="Black"/>
         ... 
    </Style>
    <ListBox ItemContainerStyle="{StaticResource ListBoxItemStyle}" />
    

    如何在这样的资源中为ItemContainer的项边框设置样式?据我所知,ContentPresenter是ItemsControl的项容器。但它没有边框,所以我不能设计它。

    4 回复  |  直到 16 年前
        1
  •  59
  •   Hakan Fıstık MuriloKunze    9 年前

    您可以删除 ScrollViewer 从 ListBox

    <ListBox>
        <ListBox.Template>
            <ControlTemplate>
                <ItemsPresenter />
            </ControlTemplate>
        </ListBox.Template>
        ...
    </ListBox>
    

    然而,我质疑嵌套列表框的价值。记住每个 是一个选择器,具有“选定”项目的概念。把一个选中的项目放在选中的项目里面,放在选中的项目里面,真的有意义吗?

    我建议改变“内在的” ListBoxes ItemsControls 这样嵌套列表就不能有选定的项。这将带来更简单的用户体验。您可能仍然需要重新放置内部模板 项目控件 以同样的方式删除滚动条,但至少用户不会对“选中”的项目感到困惑。

        2
  •  11
  •   tequilacat    8 年前

    您可以通过在XAML中捕获滚动事件来禁用窃取滚动事件:

    <ListBox PreviewMouseWheel="ScrollViewer_PreviewMouseWheel">
    

    private void ScrollViewer_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
        {
            if (sender is ListBox && !e.Handled)
            {
                e.Handled = true;
                var eventArg = new MouseWheelEventArgs(e.MouseDevice, e.Timestamp, e.Delta);
                eventArg.RoutedEvent = UIElement.MouseWheelEvent;
                eventArg.Source = sender;
                var parent = ((Control)sender).Parent as UIElement;
                parent.RaiseEvent(eventArg);
            }
        }
    

    解决方案正是针对ListBox的,它帮助我使用ListView。

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/3a3bb6b0-e088-494d-8ef2-60814415fd89/swallowing-mouse-scroll?forum=wpf

        3
  •  2
  •   Jai    10 年前

    很抱歉,我醒了这么久。实际上,您可以使用ScrollViewer的attached属性禁用ScrollViewer。

    <ListBox ScrollViewer.HorizontalScrollBarVisibility="Disabled"
             ScrollViewer.VerticalScrollBarVisibility="Disabled" ...
    </ListBox>
    
        4
  •  2
  •   Blackey    5 年前

    我喜欢为这类事情创造一种行为。

    xmlns:bhv=”http://schemas.microsoft.com/xaml/behaviors"

    <ListView ItemsSource="{Binding Items}">
        <bhv:Interaction.Behaviors>
            <bhvs:NoScrollingBehavior/>
        </bhv:Interaction.Behaviors>
    </ListView>
    

    行为本身。

    public class NoScrollingBehavior : Behavior<UIElement>
    {
        public NoScrollingBehavior()
        { }
    
        protected override void OnAttached()
        {
            base.OnAttached();
            AssociatedObject.PreviewMouseWheel += PreviewMouseWheel;
        }
    
        protected override void OnDetaching()
        {
            AssociatedObject.PreviewMouseWheel -= PreviewMouseWheel;
            base.OnDetaching();
        }
    
        private void PreviewMouseWheel(object sender, MouseWheelEventArgs e)
        {
            e.Handled = true;
            var eventArg = new MouseWheelEventArgs(e.MouseDevice, e.Timestamp, e.Delta);
            eventArg.RoutedEvent = UIElement.MouseWheelEvent;
            eventArg.Source = sender;
            var parent = ((Control)sender).Parent as UIElement;
            parent.RaiseEvent(eventArg);
        }
    }
    
        5
  •  0
  •   elena.kim Andre Goulbourne    5 年前

    如果您不喜欢行为,这里是dependencProperty的一个变体

    public class IgnoreScrollingBehavior
    {
        public static readonly DependencyProperty IgnoreScrollingProperty =
            DependencyProperty.RegisterAttached("IgnoreScrolling", typeof(bool),
                typeof(IgnoreScrollingBehavior), new UIPropertyMetadata(false, OnIgnoreScrollingChanged));
    
        public static bool GetIgnoreScrolling(UIElement uIElement)
        {
            return (bool)uIElement.GetValue(IgnoreScrollingProperty);
        }
    
        public static void SetIgnoreScrolling(UIElement uIElement, bool value)
        {
            uIElement.SetValue(IgnoreScrollingProperty, value);
        }
    
        private static void OnIgnoreScrollingChanged(DependencyObject depOpj, DependencyPropertyChangedEventArgs e)
        {
            if (depOpj is not UIElement item)
            {
                return;
            }
    
            if (e.NewValue is bool boolean)
            {
                if (boolean)
                {
                    item.PreviewMouseWheel += OnPreviewMouseWheel;
                }
                else
                {
                    item.PreviewMouseWheel -= OnPreviewMouseWheel;
                }
            }
        }
    
        private static void OnPreviewMouseWheel(object sender, MouseWheelEventArgs e)
        {
            e.Handled = true;
            MouseWheelEventArgs eventArg = new(e.MouseDevice, e.Timestamp, e.Delta)
            {
                RoutedEvent = UIElement.MouseWheelEvent,
                Source = sender
            };
            UIElement parent = ((Control)sender).Parent as UIElement;
            parent.RaiseEvent(eventArg);
        }
    }
    

    这就是它的用法

    <Listbox b:IgnoreScrollingBehavior.IgnoreScrolling="True".../>
    
        6
  •  -1
  •   Ricardo Oliveira    8 年前

    你可以用这个!没有车轮被盗。

    <ListBox ScrollViewer.HorizontalScrollBarVisibility="Disabled"
         ScrollViewer.VerticalScrollBarVisibility="Disabled" ...
    </ListBox>