我尝试适应的行为列表框为我需要和我遇到了几个问题

1)如何通过程序设置的滚动位置列表框
列表框并不提供取,其内ScrollViewer所以你不能滚动到任何你想要的。

2)如何准确地设置的垂直滚动(i。电子如何有一个顺利滚动)?
默认情况下,有没有办法的滚动名单的其他通过滚动的一个完整的元素在一段时间(列表框会始终确保第一个要素是所示完全)

这种行为是确定的,在大多数情况下,但不排雷:我想要一个平稳的移动...),

有一个解决这个问题与WPF,但不与Silverlight(见问题 "是它可能实施的光滑-滚-in-a-wpf列表视图" ).

3)如何捕捉MouseDown和MouseUp活动:
如果你的亚类列表框,你可能可以赶上MouseUp和鼠标移事件。然而MouseUp事件从未发射了(我怀疑它是被吃掉的子元件列表框)

有帮助吗?

解决方案

我已经找到答案,所以我将回答我自己。


1)如何使的列表框滚动顺利:
这个问题没有发生在SilverLight2,它只会发生与SilverLight3,其中VirtualizedStackPanel进行了介绍。
该VirtualizedStackPanel能够更快在这种情况下的巨大的列表(作为唯一的可见要素被画)

有一个解决方法(要注意,它不应该被用于巨大的清单):你重新定义列表框的ItemPanelTemplate,所以,它利用在这种情况下:

<navigation:Page.Resources>
    <ItemsPanelTemplate x:Key="ItemsPanelTemplate">
        <StackPanel/>
    </ItemsPanelTemplate>
</navigation:Page.Resources>

<StackPanel Orientation="Vertical"  x:Name="LayoutRoot">                       
        <ListBox x:Name="list" ItemsPanel="{StaticResource ItemsPanelTemplate}">
        </ListBox>
</StackPanel>

2)如何通过程序改变滚动位置
看该亚类的列表框如下:它提供了一个访问内部ScrollViewer的列表框


3)如何捕捉MouseDown/移动/事件的列表框:

创建一个亚类列表框,如下所示。3方法:

 internal void MyOnMouseLeftButtonDown(MouseButtonEventArgs e)  
 protected override void OnMouseMove(MouseEventArgs e)  
 protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)  

将所谓你可以做任何你想要与他们。有一个微妙的把戏这是OnMouseLeftButtonDown方法的列表框永远不会是称为:你需要实现的一个子类ListBoxItem,你可以处理这个事件。

using System;
using System.Collections.Generic;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace MyControls
{
  //In order for this class to be usable as a control, you need to create a folder
  //named "generic" in your project, and a "generic.xaml" file in this folder
  //(this is where you can edit the default look of your controls)
  //
  /*
   * Typical content of an "empty" generic.xaml file : 
    <ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:VideoControls">
    </ResourceDictionary>
   */
  public class MyListBox : ListBox
  {
    public MyListBox()
    {
        DefaultStyleKey = typeof(ListBox);
    }

    public override void OnApplyTemplate()
    {
      base.OnApplyTemplate();
    }

    #region ScrollViewer / unlocking access related code
    private ScrollViewer _scrollHost;
    public ScrollViewer ScrollViewer
    {
      get 
      {
        if (_scrollHost == null)
          _scrollHost = FindVisualChildOfType<ScrollViewer>(this);
        return _scrollHost; 
      }
    }

    public static childItemType FindVisualChildOfType<childItemType>(DependencyObject obj)
      where childItemType : DependencyObject
    {
      // Search immediate children first (breadth-first)
      for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
      {
        DependencyObject child = VisualTreeHelper.GetChild(obj, i);

        if (child != null && child is childItemType)
          return (childItemType)child;

        else
        {
          childItemType childOfChild = FindVisualChildOfType<childItemType>(child);

          if (childOfChild != null)
            return childOfChild;
        }
      }

      return null;
    }
    #endregion

    //Modify MyListBox so that it uses MyListBoxItem instead of ListBoxItem
    protected override DependencyObject GetContainerForItemOverride()
    {
      MyListBoxItem item = new MyListBoxItem(this);
      if (base.ItemContainerStyle != null)
      {
        item.Style = base.ItemContainerStyle;
      }

      return item;
    }

    //OnMouseLeftButtonUp is never reached, since it is eaten by the Items in the list...
    /*
    protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
    {
      base.OnMouseLeftButtonDown(e);
      e.Handled = false;
    }
    */

    internal void MyOnMouseLeftButtonDown(MouseButtonEventArgs e)
    {
    }

    protected override void OnMouseMove(MouseEventArgs e)
    {
      base.OnMouseMove(e);
    }

    protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
    {
      base.OnMouseLeftButtonUp(e);
    }


  }






  public class MyListBoxItem : ListBoxItem
  {
    MyListBox _customListBoxContainer;

    public MyListBoxItem()
    { }

    public MyListBoxItem(MyListBox customListBox)
    {
      this._customListBoxContainer = customListBox;
    }

    protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
    {
      base.OnMouseLeftButtonDown(e);

      if (this._customListBoxContainer != null)
      {
        this._customListBoxContainer.MyOnMouseLeftButtonDown(e);
      }

    }
  }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top