Wpf Can';t鼠标在TreeView列表上滚动
我有一个listview,它的项目模板是treeview(具有固定的高度和宽度)。没有太多要发布的xaml或代码 现在,如果鼠标不在树状视图上,我可以滚动列表。Wpf Can';t鼠标在TreeView列表上滚动,wpf,xaml,treeview,Wpf,Xaml,Treeview,我有一个listview,它的项目模板是treeview(具有固定的高度和宽度)。没有太多要发布的xaml或代码 现在,如果鼠标不在树状视图上,我可以滚动列表。 即使鼠标在任何树视图上,如何滚动列表?我们可以通过一些行为来解决这个问题。简单的例子: <Window x:Class="WpfApplication6.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xml
即使鼠标在任何树视图上,如何滚动列表?我们可以通过一些行为来解决这个问题。简单的例子:
<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 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
});
}
}
以及引用中的System.Windows.Interactivity。您可以为
TreeView
设置ishitestvisible=“False”
。
简单的例子:
<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>
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
列表用户=新列表();
Add(newuser(){Id=1,Name=“johndoe”,生日=newdatetime(1971,7,23)});
Add(newuser(){Id=2,Name=“Jane Doe”,生日=new DateTime(1974,1,17)});
Add(newuser(){Id=3,Name=“sammydoe”,生日=newdatetime(1991,9,2)});
Add(newuser(){Id=4,Name=“sammydoe1”,生日=newdatetime(1991,9,2)});
Add(newuser(){Id=5,Name=“sammydoe2”,生日=newdatetime(1991,9,2)});
Add(newuser(){Id=6,Name=“sammydoe3”,生日=newdatetime(1991,9,2)});
lvDataBinding.ItemsSource=用户;
}
}
公共类用户
{
公共int Id{get;set;}
公共字符串名称{get;set;}
公共日期时间生日{get;set;}
公共字符串详细信息
{
得到
{
return String.Format(“{0}是在{1}上出生的,这是对此人的详细描述。”、this.Name、this.birth.ToLongDateString());
}
}
}
可能是-@Spawn的副本,我认为该链接将解决OPs问题,但不确定它是否是副本。新的WPF用户在遇到同样的问题时更可能会遇到这个问题,而不是知道如何搜索滚动查看器和子元素。如果你说同样的话,我会支持你的回答。如果您提供工作的XAML,则可获得额外的积分:)@NETscape我已询问“当光标位于另一个可滚动列表上时滚动wpf”以获取关于该问题的链接:)我查看了树状视图的控制模板,我认为如果我移除滚动查看器,则滚动事件将从itemsPresenter切换到listview,并按需要进行滚动。(显然树视图中的项目不会滚动,因此需要处理树视图中的项目都是可见的)。
<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());
}
}
}