C# 滚轮滚动事件覆盖,向上/向下增加一个元素 我在Windows演示文稿基金会中做了一个项目。 我有一个列表框,其中每个元素的高度都相同

C# 滚轮滚动事件覆盖,向上/向下增加一个元素 我在Windows演示文稿基金会中做了一个项目。 我有一个列表框,其中每个元素的高度都相同,c#,.net,wpf,visual-studio-2010,C#,.net,Wpf,Visual Studio 2010,我想要实现的是: 当我鼠标滚轮向上或向下滚动时,我希望列表框始终按一个元素递增/递减视图 我现在拥有的: 当I鼠标滚轮向上或向下滚动时,它总是增加/减少几个(取决于屏幕高度)元素 有什么简单的解决办法吗 谢谢你的快速破解(这意味着你可以用一种更好的方式来做这件事,也许还需要一个附加的行为) PreviewMouseWheel上的私有void(对象发送者,MouseWheelEventArgs e) { var lb=发送方作为列表框; 如果(lb!=null){ //获取或存储scrol

我想要实现的是:

  • 当我
    鼠标滚轮向上或向下滚动时,我希望
    列表框
    始终按一个元素递增/递减视图
我现在拥有的:

  • 当I
    鼠标滚轮向上或向下滚动时,它总是增加/减少几个(取决于屏幕高度)元素
有什么简单的解决办法吗

谢谢你的快速破解(这意味着你可以用一种更好的方式来做这件事,也许还需要一个附加的行为)

PreviewMouseWheel上的私有void(对象发送者,MouseWheelEventArgs e) { var lb=发送方作为列表框; 如果(lb!=null){ //获取或存储scrollviewer 如果(lb.Tag==null){ lb.Tag=getDegenantByType(lb,typeof(ScrollViewer))作为ScrollViewer; } var lbScrollViewer=lb.标记为ScrollViewer; if(lbScrollViewer!=null){ 如果(e.Delta<0){ lbScrollViewer.LineDown(); }否则{ lbScrollViewer.LineUp(); } e、 已处理=正确; } } }
GetDegenantByType方法

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;
}
public静态可视getDegenantByType(可视元素,类型)
{
if(元素==null){
返回null;
}
if(element.GetType()==type){
返回元素;
}
Visual foundElement=null;
if(元素为FrameworkElement){
(元素作为FrameworkElement);
}
for(int i=0;i
用法



希望这对您有所帮助

您必须能够禁用scroll并捕获scroll事件,然后使用
JQuery
或类似功能,将项目滚动到您指定的预定义高度。不过不要问我例子:)谢谢!我就是这么做的!除了我使用了
ItemsControl
而不是
ListBox
包装在带有附加行为的
ScrollView
中。谢谢
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" />