C# 解密触发事件的控件
我有一个应用程序,其中有许多图像,它们看起来都一样,执行类似的任务:C# 解密触发事件的控件,c#,.net,wpf,xaml,event-handling,C#,.net,Wpf,Xaml,Event Handling,我有一个应用程序,其中有许多图像,它们看起来都一样,执行类似的任务: <Image Grid.Column="1" Grid.Row="0" Name="image_prog1_slot0" Stretch="Uniform" Source="bullet-icon.png" StretchDirection="Both" MouseDown="image_prog1_slot0_MouseDown"/> <Image Grid.Column="1" G
<Image Grid.Column="1" Grid.Row="0" Name="image_prog1_slot0" Stretch="Uniform" Source="bullet-icon.png" StretchDirection="Both" MouseDown="image_prog1_slot0_MouseDown"/>
<Image Grid.Column="1" Grid.Row="1" Name="image_prog1_slot1" Stretch="Uniform" Source="bullet-icon.png" StretchDirection="Both" />
<Image Grid.Column="1" Grid.Row="2" Name="image_prog1_slot2" Stretch="Uniform" Source="bullet-icon.png" StretchDirection="Both" />
显然,图像的程序号和插槽号是其名称的一部分。是否有方法在触发事件处理程序时提取此信息?是的,这是可能的
顾名思义,sender
参数包含触发事件的对象
您还可以使用网格
的附加属性方便地确定它所在的行和列。(也可以通过这种方式获取其他附着的属性。)
旁注:
您还可以使用
标记
属性来存储有关控件的自定义信息。(它可以存储任何对象。)+1。还可以将“sender”与每个成员变量进行比较,以查看它是哪个控件!是的,这也是可能的。
private void image_MouseDown(object sender, MouseButtonEventArgs e)
{
//this_program = ???;
//this_slot = ???;
//slots[this_program][this_slot] = some value;
}
private void image_MouseDown(object sender, MouseButtonEventArgs e)
{
// Getting the Image instance which fired the event
Image image = (Image)sender;
string name = image.Name;
int row = Grid.GetRow(image);
int column = Grid.GetRow(image);
// Do something with it
...
}