我遇到了動態處理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'就像一個魅力。非常感謝。附註 - 如何獲取附加事件處理程序的列表? –