无法将鼠标滚动到TreeView列表上

时间:2015-11-24 15:05:58

标签: wpf xaml treeview

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

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

2 个答案:

答案 0 :(得分:2)

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

评论行为:

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。

答案 1 :(得分:1)

您可以为console.log设置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>