簡單快速破解這個(這意味着你可以在一個更好的方式與附加的行爲做到這一點,也許)
private void OnPreviewMouseWheel(object sender, MouseWheelEventArgs e)
{
var lb = sender as ListBox;
if (lb != null) {
// get or store scrollviewer
if (lb.Tag == null) {
lb.Tag = GetDescendantByType(lb, typeof(ScrollViewer)) as ScrollViewer;
}
var lbScrollViewer = lb.Tag as ScrollViewer;
if (lbScrollViewer != null) {
if (e.Delta < 0) {
lbScrollViewer.LineDown();
} else {
lbScrollViewer.LineUp();
}
e.Handled = true;
}
}
}
GetDescendantByType方法
public static Visual GetDescendantByType(Visual element, Type type)
{
if (element == null) {
return null;
}
if (element.GetType() == type) {
return element;
}
Visual foundElement = null;
if (element is FrameworkElement) {
(element as FrameworkElement).ApplyTemplate();
}
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(element); i++) {
Visual visual = VisualTreeHelper.GetChild(element, i) as Visual;
foundElement = GetDescendantByType(visual, type);
if (foundElement != null) {
break;
}
}
return foundElement;
}
使用
<ListBox PreviewMouseWheel="OnPreviewMouseWheel" />
希望可以幫助
您必須能夠禁用滾動,然後捕獲滾動事件,然後使用'JQuery'或類似的方法,然後將您的項目滾動到您指定的預定義高度。不要問我的例子,雖然:) –