[英]C# Remove custom MouseClick EventHandler from control
我試圖向某些按鈕添加然后刪除自定義MouseClick事件處理程序。 窗口上的控件是在運行時構建的。 我可以根據其他回答的問題添加我的自定義處理程序。 localBtn.MouseClick += new MouseEventHandler((se, e) => Move_MouseClick(se, e, center, type));
我閱讀了有關刪除EventHandlers的其他問題,但它們似乎適用於“標准”事件處理程序。 我終於得到了這個來編譯object se=null; MouseEventArgs e=null; localBtn.MouseClick -= new MouseEventHandler((se1, e1) => Move_MouseClick( se, e, center, type));
object se=null; MouseEventArgs e=null; localBtn.MouseClick -= new MouseEventHandler((se1, e1) => Move_MouseClick( se, e, center, type));
(在參數中傳遞了center和type的地方。)我不知道這是否有效……因為我今天的時間已經用完了……
我正在創建一個按鈕網格(8x4)(將來可能會更大),當單擊一個按鈕時,周圍的按鈕會“更改”為動作按鈕。 想一想井字游戲,單擊中心按鈕是一個,其他所有按鈕都變為“活動”狀態。 創建自定義處理程序時,將參考“中心”按鈕,然后是一個枚舉,該枚舉指示該處理程序用於的8個按鈕中的哪個,頂部,左側,右側,topLeft,BottomRight等。按下“發生了某些事情”,需要刪除處理程序,並且按鈕將恢復為“非活動”狀態。 這些按鈕派生自一個對象,該對象對其周圍的所有其他“按鈕”都有“引用”。
我閱讀了如何從控件中刪除所有事件處理程序,以及如何C#刪除 事件處理程序 。
也許更好的控件更適合我的工作?
[edit]我已經嘗試了-=,如上所示,但是沒有用。 該控件仍然具有兩個事件處理程序。 該程序無論如何都沒有失敗,所以我不確定-=做了什么? 我猜沒事。
聽起來您應該對要完成的操作使用用戶控件。 這是一個如何在后面的代碼中完成此操作的示例(對於您正在做的事情可能會有更好的方法;這只是給您一個有效的示例):
ButtonGrid.xaml:
<UserControl x:Class="WpfApplication1.ButtonGrid"
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"
d:DesignHeight="300" d:DesignWidth="300">
<Grid x:Name="PART_Grid">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
</Grid>
</UserControl>
ButtonGrid.cs(也可以在此處制作網格,因為其他所有操作都在此處完成):
public partial class ButtonGrid : UserControl
{
private int rows, cols;
private Button[] buttons;
public ButtonGrid()
{
InitializeComponent();
cols = 4;
rows = 8;
buttons = new Button[cols * rows];
Button button;
for (int i = 0; i < cols; i++)
{
for (int j = 0; j < rows; j++)
{
// Make the button, set its place on the grid, set the necessary properties, give it OnClick, then add it to the grid
button = new Button();
button.SetValue(Grid.ColumnProperty, i);
button.SetValue(Grid.RowProperty, j);
button.Name = "Button" + (j + (rows * (i))).ToString();
button.Content = j + (rows * (i));
button.IsEnabled = false;
button.Click += OnClick;
buttons[j + (rows * (i))] = button;
PART_Grid.Children.Add(button);
}
}
buttons[12].IsEnabled = true;
}
private void OnClick(object sender, RoutedEventArgs e)
{
int index;
Button button = sender as Button;
// Determine which button was pressed by finding the index of the button that called this from the button name
if (button.Name.Length == 7)
{
index = Int32.Parse(button.Name.Substring(6, 1));
}
else
{
index = Int32.Parse(button.Name.Substring(6, 2));
}
// Make sure the buttons that are being affected are within bounds
if ((index - 1) >= 0)
{
buttons[index - 1].IsEnabled = true;
}
if ((index - rows) >= 0)
{
buttons[index - rows].IsEnabled = true;
}
if ((index + 1) <= (cols * rows - 1))
{
buttons[index + 1].IsEnabled = true;
}
if ((index + rows) <= (cols * rows - 1))
{
buttons[index + rows].IsEnabled = true;
}
}
}
我只是將它放在<local:ButtonGrid Width="500" Height="500" />
的主窗口中。
當按下時:
而且,您可以繼續單擊按鈕以查看其周圍的按鈕是如何啟用的(或您想對它們進行的操作)。
您可以將另一個mouseclick事件創建為空,然后將按鈕的點擊分配給該空按鈕,
但這一次不使用+ =,只需使用=
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.