treeview
This commit is contained in:
9
PacticeSolution/TreeViewSample/App.xaml
Normal file
9
PacticeSolution/TreeViewSample/App.xaml
Normal file
@@ -0,0 +1,9 @@
|
||||
<Application x:Class="TreeViewSample.App"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:local="clr-namespace:TreeViewSample"
|
||||
StartupUri="MainWindow.xaml">
|
||||
<Application.Resources>
|
||||
|
||||
</Application.Resources>
|
||||
</Application>
|
17
PacticeSolution/TreeViewSample/App.xaml.cs
Normal file
17
PacticeSolution/TreeViewSample/App.xaml.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Configuration;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
|
||||
namespace TreeViewSample
|
||||
{
|
||||
/// <summary>
|
||||
/// Interaction logic for App.xaml
|
||||
/// </summary>
|
||||
public partial class App : Application
|
||||
{
|
||||
}
|
||||
}
|
10
PacticeSolution/TreeViewSample/AssemblyInfo.cs
Normal file
10
PacticeSolution/TreeViewSample/AssemblyInfo.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
using System.Windows;
|
||||
|
||||
[assembly: ThemeInfo(
|
||||
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
|
||||
//(used if a resource is not found in the page,
|
||||
// or application resource dictionaries)
|
||||
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
|
||||
//(used if a resource is not found in the page,
|
||||
// app, or any theme specific resource dictionaries)
|
||||
)]
|
66
PacticeSolution/TreeViewSample/MainWindow.xaml
Normal file
66
PacticeSolution/TreeViewSample/MainWindow.xaml
Normal file
@@ -0,0 +1,66 @@
|
||||
<Window x:Class="TreeViewSample.MainWindow"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:local="clr-namespace:TreeViewSample"
|
||||
mc:Ignorable="d"
|
||||
Title="MainWindow" Height="400" Width="400">
|
||||
<Grid>
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="*"/>
|
||||
<RowDefinition Height="*"/>
|
||||
<RowDefinition Height="*"/>
|
||||
</Grid.RowDefinitions>
|
||||
|
||||
<TreeView Grid.Row="0" Margin="10">
|
||||
<TreeViewItem Header="Level 1" IsExpanded="True">
|
||||
<TreeViewItem Header="Level 2.1"/>
|
||||
<TreeViewItem Header="Level 2.2" IsExpanded="True">
|
||||
<TreeViewItem Header="Level 3.1"/>
|
||||
<TreeViewItem Header="Level 3.2"/>
|
||||
</TreeViewItem>
|
||||
<TreeViewItem Header="Level 2.3"/>
|
||||
</TreeViewItem>
|
||||
</TreeView>
|
||||
|
||||
<TreeView Grid.Row="1" Margin="10">
|
||||
<TreeViewItem IsExpanded="True">
|
||||
<TreeViewItem.Header>
|
||||
<StackPanel Orientation="Horizontal">
|
||||
<Ellipse Width="10" Height="10" Margin="0 0 10 0" Fill="LightBlue"/>
|
||||
<TextBlock Text="Level 1 (Blue)"/>
|
||||
</StackPanel>
|
||||
</TreeViewItem.Header>
|
||||
<TreeViewItem>
|
||||
<TreeViewItem.Header>
|
||||
<StackPanel Orientation="Horizontal">
|
||||
<TextBlock Text="Level 2.1" Foreground="Blue"/>
|
||||
</StackPanel>
|
||||
</TreeViewItem.Header>
|
||||
</TreeViewItem>
|
||||
<TreeViewItem IsExpanded="True">
|
||||
<TreeViewItem.Header>
|
||||
<StackPanel Orientation="Horizontal">
|
||||
<Ellipse Width="10" Height="10" Margin="0 0 10 0" Fill="LightGreen"/>
|
||||
<TextBlock Text="Level 2.2 (Green)" Foreground="Green"/>
|
||||
</StackPanel>
|
||||
</TreeViewItem.Header>
|
||||
<TreeViewItem>
|
||||
<TreeViewItem.Header>
|
||||
<TextBlock Text="Level 3.1" Foreground="Red"/>
|
||||
</TreeViewItem.Header>
|
||||
</TreeViewItem>
|
||||
<TreeViewItem>
|
||||
<TreeViewItem.Header>
|
||||
<TextBlock Text="Level 3.2" Foreground="Red"/>
|
||||
</TreeViewItem.Header>
|
||||
</TreeViewItem>
|
||||
</TreeViewItem>
|
||||
</TreeViewItem>
|
||||
</TreeView>
|
||||
|
||||
<TreeView x:Name="trvStructure" Grid.Row="2" Margin="10"
|
||||
TreeViewItem.Expanded="trvStructure_Expanded"/>
|
||||
</Grid>
|
||||
</Window>
|
72
PacticeSolution/TreeViewSample/MainWindow.xaml.cs
Normal file
72
PacticeSolution/TreeViewSample/MainWindow.xaml.cs
Normal file
@@ -0,0 +1,72 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Data;
|
||||
using System.Windows.Documents;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Media.Imaging;
|
||||
using System.Windows.Navigation;
|
||||
using System.Windows.Shapes;
|
||||
|
||||
namespace TreeViewSample
|
||||
{
|
||||
/// <summary>
|
||||
/// Interaction logic for MainWindow.xaml
|
||||
/// </summary>
|
||||
public partial class MainWindow : Window
|
||||
{
|
||||
public MainWindow()
|
||||
{
|
||||
InitializeComponent();
|
||||
InitInstance();
|
||||
}
|
||||
|
||||
private void InitInstance()
|
||||
{
|
||||
DriveInfo[] drives = DriveInfo.GetDrives();
|
||||
foreach (DriveInfo drive in drives)
|
||||
{
|
||||
trvStructure.Items.Add(CreateTreeItem(drive));
|
||||
}
|
||||
}
|
||||
|
||||
private TreeViewItem CreateTreeItem(object obj)
|
||||
{
|
||||
TreeViewItem item = new TreeViewItem();
|
||||
item.Header = obj.ToString();
|
||||
item.Tag = obj;
|
||||
item.Items.Add("Loading...");
|
||||
return item;
|
||||
}
|
||||
|
||||
private void trvStructure_Expanded(object sender, RoutedEventArgs e)
|
||||
{
|
||||
TreeViewItem item = e.Source as TreeViewItem;
|
||||
if (item.Items.Count != 1 || !(item.Items[0] is string))
|
||||
return;
|
||||
|
||||
item.Items.Clear();
|
||||
DirectoryInfo expandedDir = null;
|
||||
if (item.Tag is DriveInfo)
|
||||
expandedDir = (item.Tag as DriveInfo).RootDirectory;
|
||||
if (item.Tag is DirectoryInfo)
|
||||
expandedDir = (item.Tag as DirectoryInfo);
|
||||
|
||||
try
|
||||
{
|
||||
foreach (DirectoryInfo subDir in expandedDir.GetDirectories())
|
||||
item.Items.Add(CreateTreeItem(subDir));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show(ex.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
10
PacticeSolution/TreeViewSample/TreeViewSample.csproj
Normal file
10
PacticeSolution/TreeViewSample/TreeViewSample.csproj
Normal file
@@ -0,0 +1,10 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>WinExe</OutputType>
|
||||
<TargetFramework>net6.0-windows</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<UseWPF>true</UseWPF>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
Reference in New Issue
Block a user