feat: 5-WPF模板和自定义元素

This commit is contained in:
Bunny 2025-01-23 22:01:10 +08:00
commit de49cb117c
10 changed files with 181 additions and 0 deletions

38
.gitignore vendored Normal file
View File

@ -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/

View File

@ -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

View File

@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net8.0-windows</TargetFramework>
<RootNamespace>_5_WPF模板和自定义元素</RootNamespace>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UseWPF>true</UseWPF>
</PropertyGroup>
</Project>

View File

@ -0,0 +1,9 @@
<Application x:Class="_5_WPF模板和自定义元素.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:_5_WPF模板和自定义元素"
StartupUri="MainWindow.xaml">
<Application.Resources>
</Application.Resources>
</Application>

View File

@ -0,0 +1,12 @@
using System.Configuration;
using System.Data;
using System.Windows;
namespace _5_WPF模板和自定义元素;
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
}

View 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)
)]

View File

@ -0,0 +1,12 @@
<Window x:Class="_5_WPF模板和自定义元素.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"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="450">
<StackPanel Margin="5">
<Button Padding="5" Margin="5" Click="ButtonBase_OnClick">第一个按钮</Button>
<Button Padding="5" Margin="5">第二个按钮</Button>
</StackPanel>
</Window>

View File

@ -0,0 +1,21 @@
using System.Windows;
namespace _5_WPF模板和自定义元素;
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
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();
}
}

View File

@ -0,0 +1,11 @@
<Window x:Class="_5_WPF模板和自定义元素.VisualTreeDisplayWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
Title="VisualTreeDisplayWindow" Height="450" Width="800">
<Grid>
<TreeView Name="TreeViewElement" />
</Grid>
</Window>

View File

@ -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);
}
}