编程语言
首页 > 编程语言> > c# – 如何防止我的WPF TreeView聚焦所选的TreeViewItem

c# – 如何防止我的WPF TreeView聚焦所选的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>

代码惊吓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;
    }
  }
}

解决方法:

您需要在TreeViewItem上处理RequestBringIntoView事件而不是TreeView.

您需要使用事件设置器为下面的数据绑定项执行此操作.

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

或者像下面的xaml定义的例子

<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的定义如下

private void Null_Handler(object sender, RoutedEventArgs e)
{
    e.Handled = true;
}

标签:c,scroll,treeview,behavior,treeviewitem
来源: https://codeday.me/bug/20190626/1292658.html