[ACCEPTED]-How to disable ScrollViewer in ListBox?-scrollviewer
You can remove the ScrollViewer
from a ListBox
by changing 13 its control template to something much simpler:
<ListBox>
<ListBox.Template>
<ControlTemplate>
<ItemsPresenter />
</ControlTemplate>
</ListBox.Template>
...
</ListBox>
However, I 12 question the value of nesting ListBoxes. Remember 11 that each ListBox
is a Selector and has a concept 10 of which item is "selected". Does it really 9 make sense to have a selected item inside 8 a selected item, inside a selected item?
I 7 would suggest changing the "inner" ListBoxes
to simple 6 ItemsControls
so that the nested lists can't have selected 5 items. That would make for a much simpler 4 user experience. You may still need to retemplate 3 the inner ItemsControls
in the same way to remove the 2 scrollbars, but at least the user won't 1 get confused about which item is "selected".
You can disable stealing scroll events by 4 catching scroll event in XAML:
<ListBox PreviewMouseWheel="ScrollViewer_PreviewMouseWheel">
and re-publishing 3 it in Code behind:
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);
}
}
The solution is exactly 2 for ListBox, it helped me with ListView.
I 1 found this solution here:
I like to create a behavior for this type 2 of thing.
xmlns:bhv="http://schemas.microsoft.com/xaml/behaviors"
<ListView ItemsSource="{Binding Items}">
<bhv:Interaction.Behaviors>
<bhvs:NoScrollingBehavior/>
</bhv:Interaction.Behaviors>
</ListView>
The 1 behavior itself.
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);
}
}
Sorry for waking up such a old post. Actually, you 2 can disable the ScrollViewer by using ScrollViewer's 1 attached property.
<ListBox ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ScrollViewer.VerticalScrollBarVisibility="Disabled" ...
</ListBox>
Here is a variant with DependencyProperty, if 2 you don't like behaviors
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);
}
}
This is how it is 1 used
<Listbox b:IgnoreScrollingBehavior.IgnoreScrolling="True".../>
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.