如何防止我的WPF TreeView聚焦所选的TreeViewItem

时间:2012-01-04 09:36:42

标签: c# treeview scroll behavior treeviewitem

我正在使用TreeView,我有一些孩子。

问题在于,当我选择宽度高于TreeView的子项时,滚动自动移动以显示所有选定项目,我想禁用此行为!

我已经尝试过处理RequestBringIntoView事件,但它似乎没有用!

感谢您的帮助!

这是XAML:

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="350">
    <Grid>
        <TreeView RequestBringIntoView="TreeView_RequestBringIntoView">
            <TreeViewItem Header="Monster huge tree view item Header OMG WTF IS THAT BBQ"/>
            <TreeViewItem Header="TreeView Elem 1">
                <TreeViewItem Header="TreeView Elem 1"/>
                <TreeViewItem Header="TreeView Elem 1"/>
                <TreeViewItem Header="TreeView Elem 1"/>
                <TreeViewItem Header="TreeView Elem 1">
                    <TreeViewItem Header="TreeView Elem 1"/>
                    <TreeViewItem Header="TreeView Elem 1"/>
                    <TreeViewItem Header="TreeView Elem 1"/>
                    <TreeViewItem Header="TreeView Elem 1"/>
                    <TreeViewItem Header="TreeView Elem 1"/>
                    <TreeViewItem Header="TreeView Elem 1">
                        <TreeViewItem Header="TreeView Elem 1"/>
                        <TreeViewItem Header="TreeView Elem 1"/>
                        <TreeViewItem Header="TreeView Elem 1"/>
                        <TreeViewItem Header="TreeView Elem 1">
                            <TreeViewItem Header="TreeView Elem 1"/>
                            <TreeViewItem Header="TreeView Elem 1"/>
                            <TreeViewItem Header="TreeView Elem 1"/>
                            <TreeViewItem Header="TreeView Elem 1">
                                <TreeViewItem Header="TreeView Elem 1"/>
                                <TreeViewItem Header="TreeView Elem 1"/>
                                <TreeViewItem Header="TreeView Elem 1"/>
                                <TreeViewItem Header="TreeView Elem 1">
                                    <TreeViewItem Header="TreeView Elem 1"/>
                                    <TreeViewItem Header="TreeView Elem 1"/>
                                    <TreeViewItem Header="Monster huge tree view item Header OMG WTF IS THAT BBQ">
                                        <TreeViewItem Header="TreeView Elem 1"/>
                                        <TreeViewItem Header="TreeView Elem 1">
                                            <TreeViewItem Header="Monster huge tree view item Header OMG WTF IS THAT BBQ"/>
                                        </TreeViewItem>
                                    </TreeViewItem>
                                </TreeViewItem>
                            </TreeViewItem>
                        </TreeViewItem>
                    </TreeViewItem>
                </TreeViewItem>
            </TreeViewItem>
            <TreeViewItem Header="TreeView Elem 1"/>
            <TreeViewItem Header="TreeView Elem 1"/>
            <TreeViewItem Header="TreeView Elem 1"/>
            <TreeViewItem Header="TreeView Elem 1"/>
            <TreeViewItem Header="TreeView Elem 1"/>
            <TreeViewItem Header="TreeView Elem 1"/>
        </TreeView>
    </Grid>
</Window>

代码behing CS:

using System.Windows;

namespace WpfApplication2
{
  /// <summary>
  /// Interaction logic for MainWindow.xaml
  /// </summary>
  public partial class MainWindow : Window
  {
    public MainWindow()
    {
      InitializeComponent();
    }

    private void TreeView_RequestBringIntoView(object sender, RequestBringIntoViewEventArgs e)
    {
      e.Handled = true;
    }
  }
}

1 个答案:

答案 0 :(得分:4)

您需要处理RequestBringIntoView而非TreeViewItem的{​​{1}}事件。

您需要使用事件设置器为数据绑定项执行此操作,如下所示。

TreeView

或类似下面的xaml定义示例

<TreeView>
    <TreeView.ItemContainerStyle>
        <Style TargetType="TreeViewItem">
            <EventSetter Event="RequestBringIntoView" Handler="Null_Handler"/>
        </Style>
    </TreeView.ItemContainerStyle>
</TreeView>

<TreeView> <TreeViewItem Header="Monster huge tree view item Header OMG WTF IS THAT BBQ" RequestBringIntoView="Null_Handler"/> <TreeViewItem Header="TreeView Elem 1" RequestBringIntoView="Null_Handler"> </TreeView> 的定义如下

Null_Handler