我知道足夠多的WPF是非常危險的,但我很確定HierarchicalDataTemplate是解決您的問題的方法。我的XAML技能很差,所以我無法爲您編寫好的示例代碼。以下是我在項目中使用HierarchicalDataTemplate的方法。我希望這給你一些好點子。
<TreeView Grid.Row="1"
Grid.Column="1"
ItemsSource="{Binding Children}"
SelectedItemChanged="TreeView_SelectedItemChanged"
ContextMenu="{StaticResource MenuContextMenu}">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Children}" >
<!--<TextBlock Text="{Binding Path=ItemName}" Margin="5,0,5,0" />-->
<Grid ContextMenu="{StaticResource ContextMenu}" >
<TextBlock Name="ShownItem"
Text="{Binding Path=ItemName}"
Margin="0,0,0,0" />
<TextBox Name="EditBox"
Text="{Binding Path=ItemName}"
Visibility="{Binding Path=Visibility}"
Style="{StaticResource EditableTextBox}"
IsVisibleChanged="EditBox_IsVisibleChanged"
PreviewKeyDown="EditBox_KeyDown"
Margin="0,2,0,2"
>
</TextBox>
</Grid>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>