編輯:我想我問了一下一個XY問題。我真的不關心怎樣隧道事件的工作,我關心的是得到一個事件從代碼中提出的父窗口的後面由那就是窗口的子控件被拾起和反應而不需要明確地告訴孩子他們的父母是誰並手動訂閱該事件。正確的方法做一個隧道事件
我試圖提高在父控件的事件,並具有子控件監聽該事件並做出反應的。從我的研究中,我認爲我只需要做一個RoutedEvent
,但我做了一些不正確的事情。
這裏是在什麼我已經嘗試了MCVE,這是一個簡單的程序有一個窗口,它內部的一個用戶控件。
<Window x:Class="RoutedEventsTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:RoutedEventsTest"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Button Name="button" Click="ButtonBase_OnClick" HorizontalAlignment="Left"
VerticalAlignment="Top">Unhandled in parent</Button>
<local:ChildControl Grid.Row="1"/>
</Grid>
</Window>
using System.Windows;
namespace RoutedEventsTest
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
TestEventHandler += MainWindow_TestEventHandler;
}
void MainWindow_TestEventHandler(object sender, RoutedEventArgs e)
{
button.Content = "Handeled in parent";
e.Handled = false;
}
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
RaiseEvent(new RoutedEventArgs(TestEvent));
}
public static readonly RoutedEvent TestEvent = EventManager.RegisterRoutedEvent("TestEvent", RoutingStrategy.Tunnel, typeof(RoutedEventHandler), typeof(MainWindow));
public event RoutedEventHandler TestEventHandler
{
add { AddHandler(TestEvent, value); }
remove { RemoveHandler(TestEvent, value); }
}
}
}
<UserControl x:Class="RoutedEventsTest.ChildControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<TextBlock Name="textBlock">Unhandeled in child</TextBlock>
</Grid>
</UserControl>
using System.Windows;
using System.Windows.Controls;
namespace RoutedEventsTest
{
public partial class ChildControl : UserControl
{
public ChildControl()
{
InitializeComponent();
AddHandler(MainWindow.TestEvent, new RoutedEventHandler(TestEventHandler));
}
private void TestEventHandler(object sender, RoutedEventArgs routedEventArgs)
{
textBlock.Text = "Handled in child";
routedEventArgs.Handled = false;
}
}
}
當我運行像我期望的父窗口反應方案,但孩子用戶控件從來沒有運行它代表的是我通過中AddHandler
。
更改子控件是
public partial class ChildControl : UserControl
{
public ChildControl()
{
InitializeComponent();
AddHandler(TestEvent, new RoutedEventHandler(TestEventHandler));
}
public static readonly RoutedEvent TestEvent = EventManager.RegisterRoutedEvent("TestEvent", RoutingStrategy.Tunnel, typeof(RoutedEventHandler), typeof(ChildControl));
private void TestEventHandler(object sender, RoutedEventArgs routedEventArgs)
{
textBlock.Text = "Handled in child";
routedEventArgs.Handled = false;
}
}
沒有任何解決問題。我搜索了很多,發現了很多關於如何從孩子到父母進行冒泡事件的例子,但是我找不到一個完整的例子來展示如何從父母到孩子進行隧道事件。
好吧,所以我正在使用隧道事件不正確。我真的不在乎如何正確地做一個隧道事件。我希望在父窗口上引發的事件**可以在子控件上拾取,而不需要明確告訴孩子它是父類的。這在WPF中可能嗎?我已經更新了我的問題,而你的回答非常好,並解釋了爲什麼我的示例沒有工作,但它不能解決我正在嘗試解決的問題。 –
如果您更新顯示如何解決我的真實問題的問題,請留下原始答案。你目前的答案是我在互聯網上看到的最好的解釋隧道事件的答案。 –