我遇到了动态处理MouseUp
事件的问题。动态事件处理 - 无法解雇
WPF:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button x:Name="Button1" Content="Button1" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_1"/>
<Button x:Name="Button2" Content="Button2" HorizontalAlignment="Left" Margin="10,37,0,0" VerticalAlignment="Top" Width="75"/>
</Grid>
</Window>
C#背后:
using System.Windows;
using System.Windows.Input;
namespace WpfApplication1
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
Button2.MouseLeftButtonUp += something;
}
private void something(object sender, MouseEventArgs e)
{
MessageBox.Show("TEST");
Button2.MouseLeftButtonUp -= something;
}
}
}
现在,我想MessageBox
文本 “TEST” 只显示我第一次点击Button2
后出具检测代码,我点击了Button1
后。它没有出现。相同的代码适用于Click
事件,但我需要MouseUp
来获取鼠标位置。我已经确认第一个函数会触发,但是第二个函数不管我尝试什么。帮帮我?
+1。如果你真的想要处理冒泡事件,你可以使用UIElement.AddHandler方法在后面的代码中附加处理程序,并指定即使事件已被处理,也要调用你的处理程序。 http://msdn.microsoft.com/en-us/library/ms598899.aspx – Sisyphe
是的抱歉:)输入那个有点太快:) – Sisyphe
使用'PreviewLeftMouseButtonUp'就像一个魅力。非常感谢。附注 - 如何获取附加事件处理程序的列表? –