From de49cb117c36307de378af7548250a63efd84620 Mon Sep 17 00:00:00 2001
From: Bunny <1319900154@qq.com>
Date: Thu, 23 Jan 2025 22:01:10 +0800
Subject: [PATCH] =?UTF-8?q?feat:=205-WPF=E6=A8=A1=E6=9D=BF=E5=92=8C?=
=?UTF-8?q?=E8=87=AA=E5=AE=9A=E4=B9=89=E5=85=83=E7=B4=A0?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.gitignore | 38 ++++++++++++++++++
5-WPF模板和自定义元素.sln | 16 ++++++++
.../5-WPF模板和自定义元素.csproj | 12 ++++++
5-WPF模板和自定义元素/App.xaml | 9 +++++
5-WPF模板和自定义元素/App.xaml.cs | 12 ++++++
5-WPF模板和自定义元素/AssemblyInfo.cs | 10 +++++
5-WPF模板和自定义元素/MainWindow.xaml | 12 ++++++
5-WPF模板和自定义元素/MainWindow.xaml.cs | 21 ++++++++++
.../VisualTreeDisplayWindow.xaml | 11 +++++
.../VisualTreeDisplayWindow.xaml.cs | 40 +++++++++++++++++++
10 files changed, 181 insertions(+)
create mode 100644 .gitignore
create mode 100644 5-WPF模板和自定义元素.sln
create mode 100644 5-WPF模板和自定义元素/5-WPF模板和自定义元素.csproj
create mode 100644 5-WPF模板和自定义元素/App.xaml
create mode 100644 5-WPF模板和自定义元素/App.xaml.cs
create mode 100644 5-WPF模板和自定义元素/AssemblyInfo.cs
create mode 100644 5-WPF模板和自定义元素/MainWindow.xaml
create mode 100644 5-WPF模板和自定义元素/MainWindow.xaml.cs
create mode 100644 5-WPF模板和自定义元素/VisualTreeDisplayWindow.xaml
create mode 100644 5-WPF模板和自定义元素/VisualTreeDisplayWindow.xaml.cs
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..ffd1659
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,38 @@
+HELP.md
+target/
+!.mvn/wrapper/maven-wrapper.jar
+!**/src/main/**/target/
+!**/src/test/**/target/
+logs/
+application-prod.yml
+[b|B]in
+[o|O]bj
+
+### STS ###
+.apt_generated
+.classpath
+.factorypath
+.project
+.settings
+.springBeans
+.sts4-cache
+
+
+### IntelliJ IDEA ###
+.idea
+*.iws
+*.iml
+*.ipr
+
+### NetBeans ###
+/nbproject/private/
+/nbbuild/
+/dist/
+/nbdist/
+/.nb-gradle/
+build/
+!**/src/main/**/build/
+!**/src/test/**/build/
+
+### VS Code ###
+.vscode/
diff --git a/5-WPF模板和自定义元素.sln b/5-WPF模板和自定义元素.sln
new file mode 100644
index 0000000..264db2f
--- /dev/null
+++ b/5-WPF模板和自定义元素.sln
@@ -0,0 +1,16 @@
+
+Microsoft Visual Studio Solution File, Format Version 12.00
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "5-WPF模板和自定义元素", "5-WPF模板和自定义元素\5-WPF模板和自定义元素.csproj", "{8A23D3B8-8A39-41B7-A7E7-59DE38318ED3}"
+EndProject
+Global
+ GlobalSection(SolutionConfigurationPlatforms) = preSolution
+ Debug|Any CPU = Debug|Any CPU
+ Release|Any CPU = Release|Any CPU
+ EndGlobalSection
+ GlobalSection(ProjectConfigurationPlatforms) = postSolution
+ {8A23D3B8-8A39-41B7-A7E7-59DE38318ED3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {8A23D3B8-8A39-41B7-A7E7-59DE38318ED3}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {8A23D3B8-8A39-41B7-A7E7-59DE38318ED3}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {8A23D3B8-8A39-41B7-A7E7-59DE38318ED3}.Release|Any CPU.Build.0 = Release|Any CPU
+ EndGlobalSection
+EndGlobal
diff --git a/5-WPF模板和自定义元素/5-WPF模板和自定义元素.csproj b/5-WPF模板和自定义元素/5-WPF模板和自定义元素.csproj
new file mode 100644
index 0000000..0c446de
--- /dev/null
+++ b/5-WPF模板和自定义元素/5-WPF模板和自定义元素.csproj
@@ -0,0 +1,12 @@
+
+
+
+ WinExe
+ net8.0-windows
+ _5_WPF模板和自定义元素
+ enable
+ enable
+ true
+
+
+
diff --git a/5-WPF模板和自定义元素/App.xaml b/5-WPF模板和自定义元素/App.xaml
new file mode 100644
index 0000000..5475b13
--- /dev/null
+++ b/5-WPF模板和自定义元素/App.xaml
@@ -0,0 +1,9 @@
+
+
+
+
+
diff --git a/5-WPF模板和自定义元素/App.xaml.cs b/5-WPF模板和自定义元素/App.xaml.cs
new file mode 100644
index 0000000..6213ac9
--- /dev/null
+++ b/5-WPF模板和自定义元素/App.xaml.cs
@@ -0,0 +1,12 @@
+using System.Configuration;
+using System.Data;
+using System.Windows;
+
+namespace _5_WPF模板和自定义元素;
+
+///
+/// Interaction logic for App.xaml
+///
+public partial class App : Application
+{
+}
\ No newline at end of file
diff --git a/5-WPF模板和自定义元素/AssemblyInfo.cs b/5-WPF模板和自定义元素/AssemblyInfo.cs
new file mode 100644
index 0000000..4a05c7d
--- /dev/null
+++ b/5-WPF模板和自定义元素/AssemblyInfo.cs
@@ -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)
+)]
\ No newline at end of file
diff --git a/5-WPF模板和自定义元素/MainWindow.xaml b/5-WPF模板和自定义元素/MainWindow.xaml
new file mode 100644
index 0000000..5e08b99
--- /dev/null
+++ b/5-WPF模板和自定义元素/MainWindow.xaml
@@ -0,0 +1,12 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/5-WPF模板和自定义元素/MainWindow.xaml.cs b/5-WPF模板和自定义元素/MainWindow.xaml.cs
new file mode 100644
index 0000000..3837801
--- /dev/null
+++ b/5-WPF模板和自定义元素/MainWindow.xaml.cs
@@ -0,0 +1,21 @@
+using System.Windows;
+
+namespace _5_WPF模板和自定义元素;
+
+///
+/// Interaction logic for MainWindow.xaml
+///
+public partial class MainWindow : Window
+{
+ public MainWindow()
+ {
+ InitializeComponent();
+ }
+
+ private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
+ {
+ var visualTreeDisplayWindow = new VisualTreeDisplayWindow();
+ visualTreeDisplayWindow.ShowVisualTree(this);
+ visualTreeDisplayWindow.Show();
+ }
+}
\ No newline at end of file
diff --git a/5-WPF模板和自定义元素/VisualTreeDisplayWindow.xaml b/5-WPF模板和自定义元素/VisualTreeDisplayWindow.xaml
new file mode 100644
index 0000000..cd1d5e3
--- /dev/null
+++ b/5-WPF模板和自定义元素/VisualTreeDisplayWindow.xaml
@@ -0,0 +1,11 @@
+
+
+
+
+
\ No newline at end of file
diff --git a/5-WPF模板和自定义元素/VisualTreeDisplayWindow.xaml.cs b/5-WPF模板和自定义元素/VisualTreeDisplayWindow.xaml.cs
new file mode 100644
index 0000000..9fec693
--- /dev/null
+++ b/5-WPF模板和自定义元素/VisualTreeDisplayWindow.xaml.cs
@@ -0,0 +1,40 @@
+using System.Windows;
+using System.Windows.Controls;
+using System.Windows.Media;
+
+namespace _5_WPF模板和自定义元素;
+
+public partial class VisualTreeDisplayWindow : Window
+{
+ public VisualTreeDisplayWindow()
+ {
+ InitializeComponent();
+ }
+
+ public void ShowVisualTree(DependencyObject element)
+ {
+ // 清除树
+ TreeViewElement.Items.Clear();
+
+ // 递归添加元素及其子元素
+ ProcessElement(element, null);
+ }
+
+ private void ProcessElement(DependencyObject element, TreeViewItem? previousItem)
+ {
+ var item = new TreeViewItem
+ {
+ Header = element.GetType().Name,
+ IsExpanded = true
+ };
+
+ // 检查跟节点,如果不为空添加子节点
+ if (previousItem == null)
+ TreeViewElement.Items.Add(item);
+ else
+ previousItem.Items.Add(item);
+
+ for (var i = 0; i < VisualTreeHelper.GetChildrenCount(element); i++)
+ ProcessElement(VisualTreeHelper.GetChild(element, i), item);
+ }
+}
\ No newline at end of file