简体   繁体   中英

how to set tab index to xaml tree view items?

I have a tree view in which I am dynamically setting checkboxes as treeview items using the c# code. Now I want to iterate through all the treeview items when I press the tab button. Currently, my tree view item is getting focused when I press the tab button, but to indexing any of my tree view items. xaml code

<TreeView x:Name="resultTree" Grid.Column="2" Height="280" 
Margin="0,60,89,0" VerticalAlignment="Top" Grid.ColumnSpan="3" 
HorizontalAlignment="Right" Width="345" Grid.RowSpan="2" TabIndex="3" />

C# code:

TreeViewItem accountTree = new TreeViewItem();
accountTree.Focus();
accountTree.IsExpanded = true;
j = 4;
CheckBox currentAccountChBx = new System.Windows.Controls.CheckBox();
currentAccountChBx.Content = "A";
currentAccountChBx.Name = "accountChBx" + i;
currentAccountChBx.TabIndex = j;
currentAccountChBx.Focusable = true;
currentAccountChBx.IsTabStop = false;
j++;

标签索引流应该是这样的 Is there a way to accomplish this? Thanks.

You should use recursion: for each node, iterate through its children. For example, show each node content in message box:

void ExecuteOnEachNode(TreeView tree, Action<TreeViewItem> func) // Executes 'func' on each item with it as parameter
{
    foreach (TreeViewItem item in tree.Items)
    {
        func(item);

        foreach (TreeViewItem subitem in item.Items)
        {
            ExecuteOnEachNode(item, func); // Trasvel over current item children
        }
    }
}

ExecuteOnEachNode(item => MessageBox.Show(item.Header.ToString()));

Then, you should set the tab indx for everyone. For example,

int counter = 2;
ExecuteOnEachNode(item => item.TabIndex = counter++);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM