WPF从树视图中删除的ScrollViewer [英] WPF Remove ScrollViewer from TreeView
问题描述
我想知道,如果它可以很容易地关掉TreeView的ScrollViewer中。
I was wondering if it is possible to turn off the TreeView's ScrollViewer easily.
我有一个格一个用户控件。一项所述的细胞具有的StackPanel内几的TreeView。控制的高度的纸型自动取决于的TreeView的高度,所以也没有必要为一个滚动条。
I have a UserControl with a Grid. One of the Cells has a few TreeViews inside a Stackpanel. The height of the Control sizes automatically depending on the height of the TreeViews, so there is no need for a scrollbar.
现在的问题是:我有一大堆的这些符合自己的ScrollViewer列表框,但是当我使用鼠标滚轮,滚动停止时,你是在一个TreeView。
The problem is: I have a bunch of these in a ListBox with its own ScrollViewer, but when i am using the MouseWheel, scrolling stops when you are over a TreeView.
这是因为TreeView控件都有自己的ScrollViewer用于窃取鼠标滚轮。我知道这可能是可以通过修改控制模板,但我希望有一个更简单的方法。
This is because the TreeView has its own ScrollViewer which steals the MouseWheel. I know this is probably possible by overriding the control template, but I hope there is an easier way.
推荐答案
您可以使用技术说明如下: http://serialseb.blogspot.com/2007/ 09 / WPF的提示-6-防止-的ScrollViewer-from.html ,以防止由ScrollViewer中正在处理的鼠标滚轮事件。添加 PreviewMouseWheel =HandlePreviewMouseWheel
您TreeView和定义HandlePreviewMouseWheel为:
You can use the technique described here: http://serialseb.blogspot.com/2007/09/wpf-tips-6-preventing-scrollviewer-from.html to prevent the mouse wheel events from being handled by the ScrollViewer. Add PreviewMouseWheel="HandlePreviewMouseWheel"
to your TreeView and define HandlePreviewMouseWheel as:
private void HandlePreviewMouseWheel(object sender, MouseWheelEventArgs e)
{
if (!e.Handled)
{
e.Handled = true;
var eventArg = new MouseWheelEventArgs(
e.MouseDevice, e.Timestamp, e.Delta);
eventArg.RoutedEvent = UIElement.MouseWheelEvent;
eventArg.Source = sender;
var parent = ((Control)sender).Parent as UIElement;
parent.RaiseEvent(eventArg);
}
}
更改控制模板不包括的ScrollViewer ISN ŧ难,不过,由于TreeView控件的默认模板非常简单,大部分的复杂性正在处理的ScrollViewer。试着这样做:
Changing the control template to not include a ScrollViewer isn't that hard, though, since the default template for TreeView is pretty simple, and most of the complexity is handling the ScrollViewer. Try doing this:
<TreeView.Template>
<ControlTemplate TargetType="TreeView">
<Border BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
SnapsToDevicePixels="true">
<ItemsPresenter/>
</Border>
</ControlTemplate>
</TreeView.Template>
这篇关于WPF从树视图中删除的ScrollViewer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!