我有一个树状视图,在其中我使用C#代码将复选框动态设置为树状视图项。 现在,当我按下选项卡按钮时,我想遍历所有树视图项。 当前,当我按下选项卡按钮时,我的树状视图项目将变得聚焦,但要索引我的任何树状视图项目。 xaml代码
<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#代码:
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++;
答案 0 :(得分:0)
您应该使用递归:对于每个节点,遍历其子节点。例如,在消息框中显示每个节点的内容:
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()));
然后,您应该为所有人设置标签indx。例如,
int counter = 2;
ExecuteOnEachNode(item => item.TabIndex = counter++);