■ TreeViewItem 클래스의 ExpandSubtree 메소드를 사용해 하위 노드를 확장하는 방법을 보여준다.
▶ MainWindow.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="800" Height="600" Title="TreeViewItem 클래스 : ExpandSubtree 메소드를 사용해 하위 노드 확장하기" FontFamily="나눔고딕코딩" FontSize="16"> <StackPanel Margin="10"> <StackPanel.Resources> <XmlDataProvider x:Key="XmlDataProviderKey" XPath="Company/Employee"> <x:XData> <Company xmlns=""> <Employee Name="Don Hall"> <Employee Name="Alice Ciccu"> <Employee Name="David Pelton"> <Employee Name="Vivian Atlas" /> </Employee> <Employee Name="Jeff Price"> <Employee Name="Kari Hensien" /> </Employee> <Employee Name="Andy Jacobs" /> </Employee> <Employee Name="Bill Malone"> <Employee Name="Maurice Taylor"> <Employee Name="Sunil Uppal"> <Employee Name="Qiang Wang" /> </Employee> </Employee> </Employee> </Employee> </Company> </x:XData> </XmlDataProvider> <HierarchicalDataTemplate x:Key="EmployeeTemplateKey" ItemsSource="{Binding XPath=Employee}"> <TextBlock Text="{Binding XPath=@Name}" /> </HierarchicalDataTemplate> </StackPanel.Resources> <TreeView Name="treeView" ItemTemplate="{StaticResource EmployeeTemplateKey}" ItemsSource="{Binding Source={StaticResource XmlDataProviderKey}}" /> <Button Name="expandButton" Margin="10" Padding="5" Content="선택 항목 확장하기" /> </StackPanel> </Window> |
▶ MainWindow.xaml.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
using System.Windows; using System.Windows.Controls; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); this.expandButton.Click += expandButton_Click; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 선택 항목 확장하기 버튼 클릭시 처리하기 - expandButton_Click(sender, e) /// <summary> /// 선택 항목 확장하기 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void expandButton_Click(object sender, RoutedEventArgs e) { if(this.treeView.SelectedItem == null) { return; } TreeViewItem item = GetTreeViewItem(this.treeView, treeView.SelectedItem); if(item != null) { item.ExpandSubtree(); } } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 트리 뷰 항목 구하기 - GetTreeViewItem(itemsControl, item) /// <summary> /// 트리 뷰 항목 구하기 /// </summary> /// <param name="itemsControl">항목 컨트롤</param> /// <param name="item">항목</param> /// <returns>트리 뷰 항목</returns> private TreeViewItem GetTreeViewItem(ItemsControl itemsControl, object item) { TreeViewItem treeViewItem = itemsControl.ItemContainerGenerator.ContainerFromItem(item) as TreeViewItem; if(treeViewItem == null) { foreach(object childItem in itemsControl.Items) { TreeViewItem childTreeViewItem = itemsControl.ItemContainerGenerator.ContainerFromItem(childItem) as TreeViewItem; if(childTreeViewItem != null) { treeViewItem = GetTreeViewItem(childTreeViewItem, item); } } } return treeViewItem; } #endregion } } |