【问题标题】:Can't mouse scroll over list of TreeViews无法将鼠标滚动到 TreeView 列表
【发布时间】:2015-11-24 15:05:57
【问题描述】:

我有一个列表视图,其项目模板是树视图(具有固定的高度和宽度)。没有太多要发布的 xaml 或代码。

现在,如果鼠标不在 TreeView 上,我可以滚动列表。 即使鼠标悬停在任何 TreeView 上,如何滚动列表?

【问题讨论】:

  • @Spawn 我认为该链接将解决 OPs 问题,但不确定它是否会重复。与知道搜索滚动查看器和子元素相比,遇到相同问题时,新的 WPF 用户更有可能偶然发现这个问题。如果你说同样的话,我会赞成你的回答。如果您提供有效的 XAML 奖励积分 :)
  • @NETscape 我已经问过“当光标在另一个可滚动对象上时滚动 wpf”以获得该问题的链接 :)
  • 我查看了 tree view 的控制模板,我想如果我删除滚动查看器,那么滚动事件会从 itemsPresenter 冒泡到 listview 并根据需要进行滚动。 (显然树视图中的项目不会滚动,因此需要处理树视图中的项目都是可见的)。
  • 来自 spawn 和 mr.lb 的答案都有效。

标签: wpf xaml treeview


【解决方案1】:

我们可以通过一些行为来解决这个问题。简单例子:

cmets 的行为:

public sealed class ScrollParentBehavior : Behavior<UIElement>
{
    protected override void OnAttached()
    {
        base.OnAttached();
        AssociatedObject.PreviewMouseWheel += AssociatedObjectOnPreviewMouseWheel;
    }

    protected override void OnDetaching()
    {
        AssociatedObject.PreviewMouseWheel -= AssociatedObjectOnPreviewMouseWheel;
        base.OnDetaching();
    }

    private void AssociatedObjectOnPreviewMouseWheel(object sender, MouseWheelEventArgs e)
    {
        // find parent, it will be ContentPresenter
        var parent = VisualTreeHelper.GetParent(AssociatedObject) as UIElement;
        if (parent == null) return;
        // handle event

        // We can create here some logic, if we need to scroll TreeView content

        e.Handled = true;
        // raise it on parent
        parent.RaiseEvent(new MouseWheelEventArgs(e.MouseDevice, e.Timestamp, e.Delta)
        {
            RoutedEvent = UIElement.MouseWheelEvent
        });
    }
}

及用法——ItemTemplate中的ListView和TreeView。

<ListView ItemsSource="{Binding}">
    <ListView.ItemTemplate>
         <DataTemplate>
              <TreeView Width="100" Height="100">
                  <i:Interaction.Behaviors>
                      <local:ScrollParentBehavior />
                  </i:Interaction.Behaviors>
              </TreeView>
         </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

不要忘记用于 XAML 的 xmlns:

xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"

引用中的 System.Windows.Interactivity。

【讨论】:

    【解决方案2】:

    您可以将IsHitTestVisible="False" 设置为TreeView。 简单例子:

    <Window x:Class="WpfApplication6.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ListView Margin="10" x:Name="lvDataBinding" >
            <ListView.ItemTemplate>
                <DataTemplate>
                    <TreeView IsHitTestVisible="False">
                            <TreeViewItem Header="{Binding Id}" />
                        <TreeViewItem Header="{Binding Name}" />
                        <TreeViewItem Header="{Binding Details}" />
                    </TreeView>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </Grid>
    

     public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
    
            List<User> users = new List<User>();
            users.Add(new User() { Id = 1, Name = "John Doe", Birthday = new DateTime(1971, 7, 23) });
            users.Add(new User() { Id = 2, Name = "Jane Doe", Birthday = new DateTime(1974, 1, 17) });
            users.Add(new User() { Id = 3, Name = "Sammy Doe", Birthday = new DateTime(1991, 9, 2) });
            users.Add(new User() { Id = 4, Name = "Sammy Doe1", Birthday = new DateTime(1991, 9, 2) });
            users.Add(new User() { Id = 5, Name = "Sammy Doe2", Birthday = new DateTime(1991, 9, 2) });
            users.Add(new User() { Id = 6, Name = "Sammy Doe3", Birthday = new DateTime(1991, 9, 2) });
            lvDataBinding.ItemsSource = users;
        }
    }
    
    public class User
    {
        public int Id { get; set; }
    
        public string Name { get; set; }
    
        public DateTime Birthday { get; set; }
    
        public string Details
        {
            get
            {
                return String.Format("{0} was born on {1} and this is a long description of the person.", this.Name, this.Birthday.ToLongDateString());
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-25
      • 1970-01-01
      相关资源
      最近更新 更多