您可以使用附加屬性來觀察相關的ScrollBar事件。請注意,在以下示例中,我使用了兩個屬性:EnableUserScrollingObserver
以便使事件偵聽器和IsUserScrolling
能夠報告當前的滾動狀態。
public static class Attached
{
public static bool GetEnableUserScrollingObserver(DependencyObject obj)
{
return (bool)obj.GetValue(EnableUserScrollingObserverProperty);
}
public static void SetEnableUserScrollingObserver(DependencyObject obj, bool value)
{
obj.SetValue(EnableUserScrollingObserverProperty, value);
}
public static readonly DependencyProperty EnableUserScrollingObserverProperty =
DependencyProperty.RegisterAttached("EnableUserScrollingObserver", typeof(bool), typeof(Attached),
new FrameworkPropertyMetadata(false, new PropertyChangedCallback(EnableUserScrollingObserverChanged)));
private static void EnableUserScrollingObserverChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var s = d as ScrollBar;
if (s != null)
{
s.Scroll -= Scrollbar_Scroll;
if ((bool)e.NewValue)
{
s.Scroll += Scrollbar_Scroll;
}
}
else
{
// Using this on anything other than a ScrollBar sucks
throw new InvalidOperationException("EnableUserScrollingObserver is designed for ScrollBar elements!");
}
}
static void Scrollbar_Scroll(object sender, ScrollEventArgs e)
{
var s = sender as ScrollBar;
switch (e.ScrollEventType)
{
case ScrollEventType.EndScroll:
SetIsUserScrolling(s, false);
break;
/* All the things handled by default
case ScrollEventType.First:
break;
case ScrollEventType.LargeDecrement:
break;
case ScrollEventType.LargeIncrement:
break;
case ScrollEventType.Last:
break;
case ScrollEventType.SmallDecrement:
break;
case ScrollEventType.SmallIncrement:
break;
case ScrollEventType.ThumbPosition:
break;
case ScrollEventType.ThumbTrack:
break;
*/
default:
SetIsUserScrolling(s, true);
break;
}
}
public static bool GetIsUserScrolling(DependencyObject obj)
{
return (bool)obj.GetValue(IsUserScrollingProperty);
}
public static void SetIsUserScrolling(DependencyObject obj, bool value)
{
obj.SetValue(IsUserScrollingProperty, value);
}
public static readonly DependencyProperty IsUserScrollingProperty =
DependencyProperty.RegisterAttached("IsUserScrolling", typeof(bool), typeof(Attached),
new FrameworkPropertyMetadata(false));
}
然後滾動信息IsScrolling
財產
<Style TargetType="ScrollBar">
<Setter Property="local:Attached.EnableUserScrollingObserver" Value="True"/>
<Setter Property="local:Attached.IsUserScrolling" Value="{Binding IsScrolling,Mode=OneWayToSource}"/>
</Style>
最後,連接到您的視圖模型在MainWindow_CollectionChanged
方法,添加一個檢查爲IsScrolling
財產。
如*永久禁用*或只是當鼠標按下? – grek40
在滾動條上按下鼠標時禁用它,然後在釋放時恢復到啓用狀態。 – Cosmin