feat: WPF路由事件-3-冒泡路由事件

This commit is contained in:
Bunny 2025-01-01 21:46:07 +08:00
parent 29f9db5920
commit ec184c6e5f
9 changed files with 128 additions and 0 deletions

View File

@ -56,6 +56,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WPF路由事件-1-引发路
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WPF路由事件-2-事件路由", "WPF路由事件-2-事件路由\WPF路由事件-2-事件路由.csproj", "{AB34E12C-1E74-45AA-9085-6CAA81361E90}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WPF路由事件-3-冒泡路由事件", "WPF路由事件-3-冒泡路由事件\WPF路由事件-3-冒泡路由事件.csproj", "{9300CA90-12F2-418C-BA84-4A774716977C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -174,5 +176,9 @@ Global
{AB34E12C-1E74-45AA-9085-6CAA81361E90}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AB34E12C-1E74-45AA-9085-6CAA81361E90}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AB34E12C-1E74-45AA-9085-6CAA81361E90}.Release|Any CPU.Build.0 = Release|Any CPU
{9300CA90-12F2-418C-BA84-4A774716977C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9300CA90-12F2-418C-BA84-4A774716977C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9300CA90-12F2-418C-BA84-4A774716977C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9300CA90-12F2-418C-BA84-4A774716977C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal

View File

@ -0,0 +1,9 @@
<Application x:Class="WPF路由事件_3_冒泡路由事件.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WPF路由事件_3_冒泡路由事件"
StartupUri="MainWindow.xaml">
<Application.Resources>
</Application.Resources>
</Application>

View File

@ -0,0 +1,12 @@
using System.Configuration;
using System.Data;
using System.Windows;
namespace WPF路由事件_3_冒泡路由事件;
/// <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,34 @@
<Window x:Class="WPF路由事件_3_冒泡路由事件.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"
Icon="public/logo.ico" MouseUp="MainWindow_OnMouseUp"
Title="WPF路由事件_3_冒泡路由事件" Height="800" Width="400">
<Grid Margin="3" MouseUp="MainWindow_OnMouseUp">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Label Margin="5" Grid.Row="0" HorizontalAlignment="Left" Background="AliceBlue " BorderBrush="Black"
BorderThickness="1" MouseUp="MainWindow_OnMouseUp">
<StackPanel MouseUp="MainWindow_OnMouseUp">
<TextBlock Margin="3" MouseUp="MainWindow_OnMouseUp">Image and text label </TextBlock>
<Image Source="public/test-image.jpg" Stretch="Uniform" MouseUp="MainWindow_OnMouseUp" Width="322" />
<TextBlock Margin="3" MouseUp="MainWindow_OnMouseUp">Courtesy of the stackPanel </TextBlock>
</StackPanel>
</Label>
<ListBox Grid.Row="1" Margin="5" Name="ListBoxMessage" />
<CheckBox Grid.Row="2" Name="CheckBoxHandle">Handle first event</CheckBox>
<Button Grid.Row="3" Margin="5" Padding="3" HorizontalAlignment="Right" Name="CmdClear"
Click="CmdClear_OnClick">
清除列表
</Button>
</Grid>
</Window>

View File

@ -0,0 +1,36 @@
using System.Windows;
using System.Windows.Input;
namespace WPF路由事件_3_冒泡路由事件;
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow
{
private int _eventCount;
public MainWindow()
{
InitializeComponent();
// 如果最后一个参数设置为 true即使让 e.Handled 为true也还是会接受到
CmdClear.AddHandler(MouseUpEvent, new MouseButtonEventHandler(CmdClear_OnClick), true);
}
private void MainWindow_OnMouseUp(object sender, MouseButtonEventArgs e)
{
_eventCount++;
var message = $"#{_eventCount}:\r\nSender:{sender}\r\nSource:{e.Source}\r\nOriginal Source:{e.OriginalSource}";
ListBoxMessage.Items.Add(message);
// 阻止事件冒泡
e.Handled = (bool)CheckBoxHandle.IsChecked!;
}
private void CmdClear_OnClick(object sender, RoutedEventArgs e)
{
ListBoxMessage.Items.Clear();
}
}

View File

@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net8.0-windows</TargetFramework>
<RootNamespace>WPF路由事件_3_冒泡路由事件</RootNamespace>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UseWPF>true</UseWPF>
</PropertyGroup>
<ItemGroup>
<Resource Include="public\logo.ico">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Resource>
<Resource Include="public\test-image.jpg">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Resource>
</ItemGroup>
</Project>

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 97 KiB