WPF TreeView选择代码中的下一个或上一个项目

时间:2013-12-04 21:03:58

标签: c# wpf xaml treeview

我想使用上一个/下一个箭头来选择树视图中的项目。我有3个父节点,有子节点。父节点将不会执行任何操作,因此如果我在父节点1中的子节点的末尾,我希望下一个箭头选择父节点1中的第一个子节点。与父节点2的第一个节点相反然后单击“上一步”将转到最后一个子节点或父节点1。

目前的树视图的xaml

<TreeView x:Name="tvMessages" HorizontalAlignment="Left" Height="363" Margin="10,37,0,0" VerticalAlignment="Top" Width="312" SelectedItemChanged="tvMessages_SelectedItemChanged">
                <TreeViewItem x:Name="itemsCritical">
                    <TreeViewItem.Header>
                        <StackPanel Orientation="Horizontal">
                            <Image Source="/FlashSystem/Images/Critical.png" Height="16px" Width="16px"/>
                            <TextBlock Margin="5,0" Text="Critical"/>
                        </StackPanel>
                    </TreeViewItem.Header>
                    <TreeViewItem.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <Image Source="/FlashSystem/Images/Message.png" Width="16px" Height="16px"/>
                                <TextBlock Margin="5,0" Text="{Binding Description}"/>
                                <TextBlock Text="{Binding ID}" Visibility="Hidden"/>
                                <TextBlock Text="{Binding Specility}" Visibility="Hidden"/>
                                <TextBlock Text="{Binding IssuedDate}" Visibility="Hidden"/>
                                <TextBlock Text="{Binding Severity}" Visibility="Hidden"/>
                            </StackPanel>
                        </DataTemplate>
                    </TreeViewItem.ItemTemplate>
                </TreeViewItem>
                <TreeViewItem x:Name="itemsAlert">
                    <TreeViewItem.Header>
                        <StackPanel Orientation="Horizontal">
                            <Image Source="/FlashSystem/Images/Alert.png" Height="16px" Width="16px"/>
                            <TextBlock Margin="5,0" Text="Alert"/>
                        </StackPanel>
                    </TreeViewItem.Header>
                    <TreeViewItem.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <Image Source="/FlashSystem/Images/Message.png" Width="16px" Height="16px"/>
                                <TextBlock Margin="5,0" Text="{Binding Description}"/>
                                <TextBlock Text="{Binding ID}" Visibility="Hidden"/>
                                <TextBlock Text="{Binding Specility}" Visibility="Hidden"/>
                                <TextBlock Text="{Binding IssuedDate}" Visibility="Hidden"/>
                                <TextBlock Text="{Binding Severity}" Visibility="Hidden"/>
                            </StackPanel>
                        </DataTemplate>
                    </TreeViewItem.ItemTemplate>
                </TreeViewItem>
                <TreeViewItem x:Name="itemsInformational">
                    <TreeViewItem.Header>
                        <StackPanel Orientation="Horizontal">
                            <Image Source="/FlashSystem/Images/Info.png" Height="16px" Width="16px"/>
                            <TextBlock Margin="5,0" Text="Informational"/>
                        </StackPanel>
                    </TreeViewItem.Header>
                    <TreeViewItem.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <Image Source="/FlashSystem/Images/Message.png" Width="16px" Height="16px"/>
                                <TextBlock Margin="5,0" Text="{Binding Description}"/>
                                <TextBlock Text="{Binding ID}" Visibility="Hidden"/>
                                <TextBlock Text="{Binding Specility}" Visibility="Hidden"/>
                                <TextBlock Text="{Binding IssuedDate}" Visibility="Hidden"/>
                                <TextBlock Text="{Binding Severity}" Visibility="Hidden"/>
                            </StackPanel>
                        </DataTemplate>
                    </TreeViewItem.ItemTemplate>
                </TreeViewItem>
            </TreeView>

1 个答案:

答案 0 :(得分:0)

我会尝试模拟按键事件,看它是否有效。

如果您模拟箭头键向下事件,TreeViewItem将为您完成工作。它将自我扩展或将选择移动到其Children [0]项目。

内部TreeViewItem侦听KeyDown,它与ItemContainerGenerator一起使用以查找Children [0]项。

以下是在WPF中模拟箭头键的方法:

var backKey = new KeyEventArgs(Keyboard.PrimaryDevice, Keyboard.PrimaryDevice.ActiveSource, 0, Key.Back);  
backKey.RoutedEvent = Keyboard.KeyDownEvent;  
InputManager.Current.ProcessInput(backKey);

您不需要后退键,您需要右箭头或左箭头,您还需要InputManager来处理输入:)