WPF:通过 Tab 键浏览 Treeview 中用户控件中的文本框

WPF: Tab through textboxes in user controls in a Treeview

我正在尝试在我的应用程序中实现 Tab 功能,但我似乎没有任何尝试,所以我想我只是不明白它的用途。

我有一个用户控件(我们称它为 MyControl),其中包含一个文本框(以及其他内容)。 MyControl 与树视图一起使用。对于用户来说它看起来像这样(+ 号显示整个用户控件,以及子 MyControl 如果有的话,红色箭头显示预期的 Tab 行为):

在 MyControl 中,我已将除 TextBox 之外的所有控件的 IsTabStop 设置为 false,我几乎已将我的整个应用程序设置为 IsTabStop = false。我已经尝试在 MyControl 和 TreeView 以及包含 TreeView 的页面和 window 上将 KeyboardNavigation.TabNavigation 设置为“循环”和“继续”。我尝试了不同的组合(在 treeview 上循环,在 mycontrol 上继续,反之亦然,等等)。

我还尝试将 TabIndex 设置为与创建 MyControl 对象的顺序相同。 (因此,第一个获得 1,第二个获得 2,依此类推)。

但是我尝试过的任何东西(在所有不同的组合中)都没有让选项卡进入我希望它进入的文本框。所以我的问题不仅是顺序错误,它只是选择了“treeview”,在它周围制作了一个虚线方块。如果我更改一些设置,焦点就会乱七八糟地转到随机元素,但永远不会转到文本框

所以,我做错了什么? Tab 应该如何实现?

不知道要显示什么代码,但是: 我的控件:

<UserControl x:Class="SinusManager.View.ParamNodeV"
         //blaablaa
         x:Name="pn"
         KeyboardNavigation.TabNavigation="Continue"
         IsTabStop="True">
         //blaablaa
         <StackPanel Background="{StaticResource Milky} KeyboardNavigation.TabNavigation="Continue">
   <Grid>
   //blaablaa
        <TextBox x:Name="ValueBox" VerticalContentAlignment="Center" PreviewTextInput="NumberValidationTextBox" FontFamily="{StaticResource MonoFont}" Text="{Binding Path=ValueText, ElementName=pn, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" Margin="130,0,0,0" VerticalAlignment="Top" Height="26" Width="92"
                  IsTabStop="True" KeyboardNavigation.TabNavigation="Continue" IsReadOnly="{Binding Path=ReadOnly, ElementName=pn}">
    //blaablaa

TreeView 用户控件

<UserControl x:Class="SinusManager.View.ParamFrameV"
  //blaablaa
  x:Name="pf"
  KeyboardNavigation.TabNavigation="Cycle">
  <Grid>
    <ScrollViewer VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Disabled" Margin="0,0,0,0">
        <i:Interaction.Behaviors>
            <v:BubbleScrollEvent/>
        </i:Interaction.Behaviors>
          //blaablaa
         <TreeView Margin="0,16,0,0" KeyboardNavigation.TabNavigation="Cycle" ItemsSource="{Binding Nodes}" Background="{StaticResource Milky}" BorderThickness="0" ItemContainerStyle="{StaticResource TreeViewItemExpandedStyle}" HorizontalAlignment="Left" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Disabled">
                <TreeView.Resources>
                    <HierarchicalDataTemplate DataType="{x:Type vm:ParamTextNodeVM}" ItemsSource="{Binding Path=Nodes}">
                        <v:ParamNodeV Node="{Binding Path=NodeM}" Status="{Binding Status, Mode=TwoWay}" IsTabStop="True" NameText="{Binding Path=Name}" UnitText="{Binding Path=Unit}" ValueText="{Binding Path=Value, Mode=TwoWay}" RangeText="{Binding Path=Range}" ValueTypeText="{Binding Path=ValueType}" DescriptionText="{Binding Path=Description}" ReadOnly="{Binding Path=ReadOnly}" GetParameter="{Binding GetParameterCmd}" SendParameter="{Binding SendParameterCmd}"/>
                    </HierarchicalDataTemplate>
                //blaablaa

上面的代码没有我尝试过的 TabOrder 绑定,我删除了它,因为它不起作用。但基本上我有一个整数,它为每个创建并添加到 TreeView 的新 MyControl 计数,并使用该整数在控件上设置 TabIndex。不,使用或不使用 TabOrder 的行为有所不同(是的,绑定有效)。

编辑: 仍然没有回答,我想我会在这里转储所有代码,也许有人会发现一些东西,我真的很想解决这个问题。

主窗口:

<Window x:Class="SinusManager.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:SinusManager"
    xmlns:v="clr-namespace:SinusManager.View"
    xmlns:vm="clr-namespace:SinusManager.ViewModel"
    mc:Ignorable="d"
    Loaded="Window_Loaded"
    Closing="Window_Closing"
    ContentRendered="Window_Shown"
    Title="NFO Sinus Manager" Height="450" Width="800" Background="{StaticResource Milky}"
    Style="{StaticResource WinAll}" ForceCursor="True" MinHeight="375" MinWidth="500"
    KeyboardNavigation.TabNavigation="Cycle">
<Window.DataContext>
    <vm:MainViewModel/>
</Window.DataContext>
<Grid>
    <DockPanel>
        <Frame Source="View/Pages/StartPage.xaml" x:Name="MainFrame" NavigationUIVisibility="Hidden"/>
    </DockPanel>
</Grid>

主页:

<Page x:Class="SinusManager.View.MainPage"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
  xmlns:local="clr-namespace:SinusManager"
  xmlns:v="clr-namespace:SinusManager.View"
  xmlns:vm="clr-namespace:SinusManager.ViewModel"
  mc:Ignorable="d" 
  d:DesignHeight="450" d:DesignWidth="800"
  Title="MainPage"
  KeepAlive="True"
  KeyboardNavigation.TabNavigation="Cycle">

<Grid Background="{StaticResource White}" KeyboardNavigation.TabNavigation="Cycle">

    <v:IconButton x:Name="BackHomeBTN" Image="{StaticResource BackHomeBTN}" Margin="0" VerticalAlignment="Top" HorizontalAlignment="Left" IconHeight="24" IconWidth="36" ColorPalette="{StaticResource WhiteSolid}" ColorPaletteFore="{StaticResource BlackToBluePalette}" Click="BackHomeBTN_Click" ToolTip="{StaticResource ToolTipHome}" Focusable="False" IsTabStop="False">
    </v:IconButton>

    <Label Margin="55,10,0,0" Content="{Binding InverterInfoPanel.Name}" FontFamily="{StaticResource MainFont}" FontSize="16" FontWeight="Bold"/>

    <Grid Margin="0,0,0,0" KeyboardNavigation.TabNavigation="Cycle">

        <!-- TOOL BAR-->
        <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" VerticalAlignment="Top" Height="50" Background="{StaticResource White}" Focusable="False">
            <v:IconButton Command="{Binding InverterInfoPanel.OpenLogCmd}" Image="{StaticResource LogBTN}" Content="{StaticResource TxtLog}" IconMargin="0,3,0,0" FontSize="11" FontFamily="{StaticResource MainFont}" ColorPalette="{StaticResource WhitePalette}" ToolTip="{StaticResource ToolTipLog}" Visibility="{Binding ComPort.IsSimulated, Converter={StaticResource InverseBooleanToVisibility}}" Focusable="False" IsTabStop="False"/>
            <v:IconButton Image="{StaticResource MonitorBTN}" Width="50" Content="{StaticResource TxtMonitor}" IconMargin="0,3,0,0" FontSize="11" FontFamily="{StaticResource MainFont}" ColorPalette="{StaticResource WhitePalette}" ToolTip="{StaticResource ToolTipMonitor}" Focusable="False" IsTabStop="False"/>
            <v:IconButton Command="{Binding InverterInfoPanel.OpenModbusCmd}" Image="{StaticResource HubBTN}" Content="{StaticResource TxtBtnModbus}" FontSize="11" IconMargin="0,3,0,0" FontFamily="{StaticResource MainFont}" ColorPalette="{StaticResource WhitePalette}" ToolTip="{StaticResource ToolTipOpenModbus}" Visibility="{Binding ComPort.IsSimulated, Converter={StaticResource InverseBooleanToVisibility}}" Focusable="False" IsTabStop="False"/>
            <v:IconButton Width="50"  Command="{Binding InverterInfoPanel.GetAllParametersCmd}" ToolTip="{StaticResource ToolTipGetAll}" Image="{StaticResource ImportBTN}" Content="{StaticResource TxtBtnGetFInv}" FontSize="11" IconMargin="0,3,0,0" FontFamily="{StaticResource MainFont}" ColorPalette="{StaticResource WhitePalette}" Visibility="{Binding ComPort.IsSimulated, Converter={StaticResource InverseBooleanToVisibility}}" Focusable="False" IsTabStop="False"/>
            <v:IconButton Width="50" Command="{Binding InverterInfoPanel.SendAllParametersCmd}" ToolTip="{StaticResource ToolTipSendAll}" Image="{StaticResource ExportBTN}" Content="{StaticResource TxtBtnSend2Inv}" FontSize="11" IconMargin="0,3,0,0" FontFamily="{StaticResource MainFont}" ColorPalette="{StaticResource WhitePalette}" Visibility="{Binding ComPort.IsSimulated, Converter={StaticResource InverseBooleanToVisibility}}" Focusable="False" IsTabStop="False"/>
            <v:IconButton Image="{StaticResource ResetBTN}" Width="50" Content="{StaticResource TxtBtnReset}" ToolTip="{StaticResource ToolTipReset}" IconMargin="0,3,0,0" FontFamily="{StaticResource MainFont}" FontSize="11"  ColorPalette="{StaticResource WhitePalette}" Focusable="False" IsTabStop="False"/>
            <v:IconButton Image="{StaticResource OpenBTN}" Width="50" Content="{StaticResource TxtOpen}" ToolTip="{StaticResource ToolTipOpenFile}" IconMargin="0,3,0,0" FontFamily="{StaticResource MainFont}" FontSize="11" ColorPalette="{StaticResource WhitePalette}" Focusable="False" IsTabStop="False"/>
            <v:IconButton Image="{StaticResource SaveBTN}" Width="50" Content="{StaticResource TxtSave}" ToolTip="{StaticResource ToolTipSaveFile}" IconMargin="0,3,0,0" FontFamily="{StaticResource MainFont}" FontSize="11" ColorPalette="{StaticResource WhitePalette}" Focusable="False" IsTabStop="False"/>
            <v:IconButton Command="{Binding InverterInfoPanel.CloseCmd}" Image="{StaticResource CloseBTN}" ToolTip="{StaticResource ToolTipClose}" Width="50" Content="{StaticResource TxtClose}" FontSize="11" IconMargin="0,3,0,0" FontFamily="{StaticResource MainFont}" ColorPalette="{StaticResource WhitePalette}" Focusable="False" IsTabStop="False"/>
            <Grid Margin="5" Width="12" Height="42" VerticalAlignment="Top" HorizontalAlignment="Right"/>
            <Image Source="{StaticResource NFOLogo}" Margin="5" Width="42" Height="42" VerticalAlignment="Top" HorizontalAlignment="Right"/>
        </StackPanel>

        <DockPanel Margin="0,50,0,0" Background="{StaticResource Milky}" KeyboardNavigation.TabNavigation="Cycle">
            <v:InverterInfoPanelV x:Name="InverterPanel" VerticalAlignment="Stretch" HorizontalAlignment="Left"/>
            <Grid KeyboardNavigation.TabNavigation="Cycle">
                <v:ParamTabBarV VerticalAlignment="Top"/>
                <Frame Margin="16,32,0,0" Source="ParamFrameV.xaml" x:Name="pageFrame" KeyboardNavigation.TabNavigation="Cycle" NavigationUIVisibility="Hidden" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" BorderThickness="0" Background="{StaticResource Milky}"/>
            </Grid>
        </DockPanel>

        <Image Source="{Binding InverterInfoPanel.Image, TargetNullValue={StaticResource FileIcon}}" Margin="30,35,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" Width="64"/>
    </Grid>

    <ItemsControl ItemsSource="{Binding Path=ComPorts}" VerticalAlignment="Bottom" HorizontalAlignment="Left" Margin="195,0,0,0">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel IsItemsHost="True" Orientation="Horizontal"/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <v:TextStatusButton Status="{Binding Path=Status}" IsSelected="{Binding Path=IsSelected}" ColorPalette="{StaticResource DarkestGrayPalette}" Margin="1,0,1,0"  ColorPaletteFore="{StaticResource WhiteSolid}" Content="{Binding Path=Name}" FontSize="11" Command="{Binding Path=OpenComPortCmd}" Height="24" FontFamily="{StaticResource MainFont}" ToolTip="{Binding Path=FullName}"/>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</Grid>

参数框架:

<UserControl x:Class="SinusManager.View.ParamFrameV"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
  mc:Ignorable="d" 
  xmlns:v="clr-namespace:SinusManager.View"
  xmlns:vm="clr-namespace:SinusManager.ViewModel"
  xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
  x:Name="pf"
  KeyboardNavigation.TabNavigation="Cycle">

<UserControl.Resources>
    <Style TargetType="TreeViewItem">
        <Setter Property="IsTabStop" Value="True"/>
        <Setter Property="Focusable" Value="False"/>
        <Setter Property="KeyboardNavigation.TabNavigation" Value="Continue"/>
    </Style>
</UserControl.Resources>

<Grid KeyboardNavigation.TabNavigation="Cycle">
    <ScrollViewer VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Disabled" Margin="0,0,0,0" KeyboardNavigation.TabNavigation="Cycle">
        <i:Interaction.Behaviors>
            <v:BubbleScrollBehavior/>
        </i:Interaction.Behaviors>

        <StackPanel VerticalAlignment="Top" HorizontalAlignment="Left" KeyboardNavigation.TabNavigation="Cycle">
            <Grid Width="311" Height="64" VerticalAlignment="Top" HorizontalAlignment="Left">
                <TextBlock Text="{Binding Name}" FontSize="14" FontFamily="{StaticResource MainFont}" FontWeight="Bold" Margin="6,16,0,0" VerticalAlignment="Top" HorizontalAlignment="Left"/>
                <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" VerticalAlignment="Top" Height="50" Background="{StaticResource White}">
                    <v:IconButton IsTabStop="False" Command="{Binding GetParametersCmd}" Height="50" Width="55" Image="{StaticResource ImportBTN}" Content="{StaticResource TxtGetAll}" IconMargin="0,3,0,0" FontFamily="{StaticResource MainFont}" ColorPalette="{StaticResource MilkyPalette}" ToolTip="{StaticResource ToolTipGetParas}"/>
                    <v:IconButton IsTabStop="False" Command="{Binding SendParametersCmd}" Height="50" Width="55"  Image="{StaticResource ExportBTN}" Content="{StaticResource TxtSendAll}" IconMargin="0,3,0,0" FontFamily="{StaticResource MainFont}" ColorPalette="{StaticResource MilkyPalette}" ToolTip="{StaticResource ToolTipSendParas}"/>
                </StackPanel>
            </Grid>

            <TreeView Margin="0,16,0,0" KeyboardNavigation.TabNavigation="Cycle" Focusable="False" IsTabStop="False" ItemsSource="{Binding Nodes}" Background="{StaticResource Milky}" BorderThickness="0" ItemContainerStyle="{StaticResource TreeViewItemExpandedStyle}" HorizontalAlignment="Left" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Disabled">
                <TreeView.Resources>
                    <HierarchicalDataTemplate DataType="{x:Type vm:ParamTextNodeVM}" ItemsSource="{Binding Path=Nodes}">
                        <v:ParamNodeV Node="{Binding Path=NodeM}" Status="{Binding Status, Mode=TwoWay}" NameText="{Binding Path=Name}" UnitText="{Binding Path=Unit}" ValueText="{Binding Path=Value, Mode=TwoWay}" RangeText="{Binding Path=Range}" ValueTypeText="{Binding Path=ValueType}" DescriptionText="{Binding Path=Description}" ReadOnly="{Binding Path=ReadOnly}" GetParameter="{Binding GetParameterCmd}" SendParameter="{Binding SendParameterCmd}"/>
                    </HierarchicalDataTemplate>
                    <!--TabOrder="{Binding Path=TabOrder}"-->

                    <Style TargetType="{x:Type TreeViewItem}">
                        <Setter Property="IsExpanded" Value="{Binding Path=IsOpen}"/>
                    </Style>

                    <HierarchicalDataTemplate DataType="{x:Type vm:ParamGroupNodeVM}" ItemsSource="{Binding Path=Nodes}" >
                        <v:ParamGroupNodeV Node="{Binding Path=NodeM}" IsTabStop="False" Status="{Binding Status, Mode=TwoWay}" NameText="{Binding Path=Name}" ChildCount="{Binding Path=ChildCount}" ReadOnly="{Binding Path=ReadOnly}" IsOpen="{Binding IsExpanded, RelativeSource={RelativeSource AncestorType=TreeViewItem}, Mode=TwoWay}"/>
                    </HierarchicalDataTemplate>

                    <HierarchicalDataTemplate DataType="{x:Type vm:ParamComboNodeVM}" ItemsSource="{Binding Path=Nodes}">
                        <v:ParamComboNodeV Node="{Binding Path=NodeM}" IsTabStop="False" Status="{Binding Status, Mode=TwoWay}" NameText="{Binding Path=Name}" ComboBoxItems="{Binding Path=ComboBoxItems}" RangeText="{Binding Path=Range}" ValueTypeText="{Binding Path=ValueType}" DescriptionText="{Binding Path=Description}" SelectedItem="{Binding Path=SelectedItem}" ReadOnly="{Binding Path=ReadOnly}"  GetParameter="{Binding GetParameterCmd}" SendParameter="{Binding SendParameterCmd}"/>
                    </HierarchicalDataTemplate>
                </TreeView.Resources>
            </TreeView>
            <Grid Height="50"/>
        </StackPanel>
    </ScrollViewer>
</Grid>

参数节点:

<UserControl x:Class="SinusManager.View.ParamNodeV"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
         xmlns:local="clr-namespace:SinusManager"
         xmlns:v="clr-namespace:SinusManager.View"
         xmlns:r="clr-namespace:SinusManager.Resources"
         xmlns:vm="clr-namespace:SinusManager.ViewModel"
         mc:Ignorable="d" 
         x:Name="pn"
         KeyboardNavigation.TabNavigation="Continue" Focusable="False" IsTabStop="False">

<StackPanel Background="{StaticResource Milky}">
    <Grid Focusable="False">
        <!--<v:IconButton ColorPalette="{StaticResource TransparentSolid}" ColorPaletteFore="{StaticResource WhiteToBluePalette}" Image="{StaticResource MiscExpanderBack}" IconMargin="0" Content="" VerticalAlignment="Top" Margin="0" HorizontalAlignment="Left" FontSize="1" Height="26" IconWidth="26" IconHeight="26"/>-->
        <v:IconButton Focusable="False" IsTabStop="False" ColorPalette="{StaticResource WhiteToBluePalette}" ImageBack="{StaticResource MiscExpanderBack}" ColorPaletteFore="{StaticResource BlackSolid}" IconMargin="0" Content="" VerticalAlignment="Top" Margin="0" HorizontalAlignment="Left" FontSize="1" Height="26" IconWidth="26" IconHeight="26"  Click="IconButton_Click">
            <v:IconButton.Image>
                <MultiBinding Converter="{StaticResource BivalentConverter}">
                    <Binding Path="IsOpen" RelativeSource="{RelativeSource AncestorType=UserControl, Mode=FindAncestor}"/>
                    <Binding Source="{StaticResource CollapseBTN}"/>
                    <Binding Source="{StaticResource ExpandBTN}"/>
                </MultiBinding>
            </v:IconButton.Image>
            <v:IconButton.ToolTip>
                <MultiBinding Converter="{StaticResource BivalentConverter}">
                    <Binding Path="IsOpen" RelativeSource="{RelativeSource AncestorType=UserControl, Mode=FindAncestor}"/>
                    <Binding Source="{StaticResource TxtCollapse}"/>
                    <Binding Source="{StaticResource TxtExpand}"/>
                </MultiBinding>
            </v:IconButton.ToolTip>
        </v:IconButton>

        <TextBox Text="{Binding Path=NameText, ElementName=pn}" FontFamily="{StaticResource MainFont}" VerticalContentAlignment="Center" HorizontalAlignment="Left" Margin="26,0,0,0" Height="26" VerticalAlignment="Bottom" IsReadOnly="True" IsTabStop="False" BorderThickness="0" Background="{StaticResource Milky}" FontWeight="Bold"/>
        <TextBox x:Name="ValueBox" VerticalContentAlignment="Center" PreviewTextInput="NumberValidationTextBox" FontFamily="{StaticResource MonoFont}" Text="{Binding Path=ValueText, ElementName=pn, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" Margin="130,0,0,0" VerticalAlignment="Top" Height="26" Width="92"
                  Focusable="True" IsTabStop="True" KeyboardNavigation.TabNavigation="Continue" IsReadOnly="{Binding Path=ReadOnly, ElementName=pn}">
            <!--TabIndex="{Binding Path=TabOrder, ElementName=pn}"-->
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="LostFocus">
                    <i:InvokeCommandAction Command="{Binding LostFocusCmd}" />
                </i:EventTrigger>
                <i:EventTrigger EventName="TextChanged">
                    <i:InvokeCommandAction Command="{Binding ValueChangedCmd}" />
                </i:EventTrigger>
            </i:Interaction.Triggers>
            <TextBox.Background>
                <MultiBinding Converter="{StaticResource MultiValuedConverter}">
                    <Binding Path="Status" ElementName="pn"/>
                    <Binding Source="{StaticResource White}"/>
                    <Binding Source="{StaticResource White}"/>
                    <Binding Source="{StaticResource TextBoxRed}"/>
                    <Binding Source="{StaticResource TextBoxRed}"/>
                </MultiBinding>
            </TextBox.Background>
        </TextBox>

        <v:ColoredImage IsTabStop="False" Image="{StaticResource LockIcon}" Margin="130,0,0,0" Height="20" Width="20" Color="{StaticResource LightGrayGray}" Visibility="{Binding Path=ReadOnly, ElementName=pn, Converter={StaticResource BooleanToVisibility}}"/>

        <TextBox IsTabStop="False" Text="{Binding Path=UnitText, ElementName=pn}" FontFamily="{StaticResource MainFont}" VerticalContentAlignment="Center" HorizontalAlignment="Left" Margin="222,0,0,0" VerticalAlignment="Bottom" Height="26" Width="73" IsReadOnly="True" BorderThickness="0" Background="{StaticResource Milky}"/>

        <!--Image="{StaticResource BoxIcon}"-->
        <v:ColoredImage IsTabStop="False" Width="20" Height="20" HorizontalAlignment="Right" Color="{StaticResource White}">
            <v:ColoredImage.Image>
                <MultiBinding Converter="{StaticResource MultiValuedConverter}">
                    <Binding Path="Status" ElementName="pn"/>
                    <Binding Source="{StaticResource CircleGUI}"/>
                    <Binding Source="{StaticResource CircleGUI}"/>
                    <Binding Source="{StaticResource TriangleGUI}"/>
                    <Binding Source="{StaticResource CircleGUI}"/>
                </MultiBinding>
            </v:ColoredImage.Image>
        </v:ColoredImage>
        <v:ColoredImage IsTabStop="False" Width="20" Height="20" HorizontalAlignment="Right">
            <v:ColoredImage.Color>
                <MultiBinding Converter="{StaticResource MultiValuedConverter}">
                    <Binding Path="Status" ElementName="pn"/>
                    <Binding Source="{StaticResource GreenLight}"/>
                    <Binding Source="{StaticResource YellowLight}"/>
                    <Binding Source="{StaticResource RedLight}"/>
                    <Binding Source="{StaticResource BlueLight}"/>
                </MultiBinding>
            </v:ColoredImage.Color>
            <v:ColoredImage.Image>
                <MultiBinding Converter="{StaticResource MultiValuedConverter}">
                    <Binding Path="Status" ElementName="pn"/>
                    <Binding Source="{StaticResource OkIcon}"/>
                    <Binding Source="{StaticResource WarningIcon}"/>
                    <Binding Source="{StaticResource ErrorIcon}"/>
                    <Binding Source="{StaticResource UnidentifiedIcon}"/>
                </MultiBinding>
            </v:ColoredImage.Image>
            <v:ColoredImage.ToolTip>
                <MultiBinding Converter="{StaticResource MultiValuedConverter}">
                    <Binding Path="Status" ElementName="pn"/>
                    <Binding Source="{StaticResource ToolTipOK}"/>
                    <Binding Source="{StaticResource ToolTipNotSynced}"/>
                    <Binding Source="{StaticResource ToolTipInvalid}"/>
                    <Binding Source="{StaticResource ToolTipUnidentified}"/>
                </MultiBinding>
            </v:ColoredImage.ToolTip>
        </v:ColoredImage>
    </Grid>

    <Grid Margin="0,2,0,0">
        <Image Margin="8, -4, 0, 0" Source="{StaticResource DownRight}" Width="24" Height="24" VerticalAlignment="Top" HorizontalAlignment="Left" Visibility="{Binding Path=IsOpen, ElementName=pn, Converter={StaticResource BooleanToVisibility}}"/>
        <StackPanel>
            <Border Width="263" Name="hej" HorizontalAlignment="Right" VerticalAlignment="Bottom" Margin="0,0,0,0" BorderThickness="1" BorderBrush="{StaticResource Gray}" CornerRadius="5" Visibility="{Binding Path=IsOpen, ElementName=pn, Converter={StaticResource BooleanToVisibility}}">
                <Grid>
                    <WrapPanel Margin="0">
                        <TextBox Text="{Binding Path=DescriptionText, ElementName=pn}" IsTabStop="False" TextWrapping="Wrap" HorizontalAlignment="Left" Margin="0,0,0,0" VerticalAlignment="top" IsReadOnly="True" BorderThickness="0" Width="261" Background="{x:Null}" />
                        <TextBox Text="{StaticResource TxtValueType}" IsTabStop="False" HorizontalAlignment="Left" VerticalAlignment="top" IsReadOnly="True" BorderThickness="0" FontWeight="Bold" FontFamily="{StaticResource MainFont}" Background="{x:Null}"/>
                        <TextBox Text="{Binding Path=ValueTypeText, ElementName=pn}" IsTabStop="False" HorizontalAlignment="Left" VerticalAlignment="top" IsReadOnly="True" FontFamily="{StaticResource MainFont}" BorderThickness="0" Width="185" Background="{x:Null}"/>
                        <TextBox Text="{StaticResource TxtRange}" IsTabStop="False" HorizontalAlignment="Left" VerticalAlignment="top" IsReadOnly="True" BorderThickness="0" FontWeight="Bold" FontFamily="{StaticResource MainFont}" Background="{x:Null}"/>
                        <TextBox Text="{Binding Path=RangeText, ElementName=pn}" IsTabStop="False" HorizontalAlignment="Left" VerticalAlignment="top" IsReadOnly="True" FontFamily="{StaticResource MainFont}" BorderThickness="0" Width="210" Background="{x:Null}"/>
                        <Grid Height="62" Width="250"/>
                    </WrapPanel>
                    <v:IconButton IsTabStop="False" Margin="0,0,4,0" Command="{Binding Path=SendParameter, ElementName=pn}" HorizontalAlignment="Right" VerticalAlignment="Bottom" ColorPalette="{StaticResource MilkyPalette}" Foreground="Black" Content="{StaticResource BtnSendPara}" FontSize="9" FontFamily="{StaticResource MainFont}" Height="62" Image="{StaticResource ResourceKey=ExportBTN}"/>
                    <v:IconButton IsTabStop="False" Margin="4,0,0,0" Command="{Binding Path=GetParameter, ElementName=pn}" HorizontalAlignment="Left" VerticalAlignment="Bottom" ColorPalette="{StaticResource MilkyPalette}" Foreground="Black" Content="{StaticResource BtnGetPara}"  FontSize="9" FontFamily="{StaticResource MainFont}"  Height="62" Image="{StaticResource ResourceKey=ImportBTN}"/>
                </Grid>
            </Border>
            <Grid Height="3"/>
        </StackPanel>
    </Grid>
</StackPanel>

控制结构:

问题:

我将这部分添加到 元素中。它解决了问题。

                    <Style TargetType="{x:Type TreeViewItem}">
                        <Setter Property="IsExpanded" Value="{Binding Path=IsOpen}"/>
                        <Setter Property="IsTabStop" Value="True"/>
                        <Setter Property="Focusable" Value="False"/>
                        <Setter Property="KeyboardNavigation.TabNavigation" Value="Continue"/>
                    </Style>