我正在使用TreeView控件,当单击其中一个时,它会自动滚动到左对齐TreeViewItem.我已经去看了我的Styles和ControlTemplates,但我还没有找到任何东西.是否有一个默认的ControlTemplate导致这个?我想禁用它.
这些项目会滚动,因为ScrollViewer会对它们调用BringIntoView().因此,避免滚动的一种方法是禁止处理RequestBringIntoView事件.您可以通过继承TreeView并实例化此控件来快速尝试:
public class NoScrollTreeView : TreeView { public class NoScrollTreeViewItem : TreeViewItem { public NoScrollTreeViewItem() : base() { this.RequestBringIntoView += delegate (object sender, RequestBringIntoViewEventArgs e) { e.Handled = true; }; } protected override DependencyObject GetContainerForItemOverride() { return new NoScrollTreeViewItem(); } } protected override DependencyObject GetContainerForItemOverride() { return new NoScrollTreeViewItem(); } }
在花了几个小时来解决这个问题后,我找到了一个适合我的解决方案.
brians解决方案是防止TreeViewItem上的RequestBringIntoView事件冒泡的第一步.不幸的是,如果以编程方式更改所选项目,这也会阻止显示树视图
yourtreeview.SelectedItem = yourtreeviewitem
所以,对我来说,解决方案是修改treeview的controltemplate,如下所示:
"autoscrollpreventer"是:
using System; using System.Windows; using System.Windows.Controls; namespace LiveContext.Designer.GUI.Components { public class AutoScrollPreventer : StackPanel { public AutoScrollPreventer() { this.RequestBringIntoView += delegate(object sender, RequestBringIntoViewEventArgs e) { // stop this event from bubbling so that a scrollviewer doesn't try to BringIntoView.. e.Handled = true; }; } }
}
希望能帮助到你..