C# WPF中按下了哪个按钮?

C# WPF中按下了哪个按钮?,c#,wpf,image,list,button,C#,Wpf,Image,List,Button,我得到了这些代码: 在cs中: 一类get和一组ImgSmall,Name,ImgLarge: List<Img> sectionList = new List<Img> { new Img { ImgSmall="Img/NG.png", Name="New Game", ImgLarge="Img/NG.png" }, new Img { ImgSmall="Img/HS.png", Name="High Score"

我得到了这些代码:

在cs中:

一类get和一组
ImgSmall
Name
ImgLarge

List<Img> sectionList = new List<Img> 
{ 
  new Img
  {
      ImgSmall="Img/NG.png", Name="New Game", ImgLarge="Img/NG.png"
  },

  new Img
  {
      ImgSmall="Img/HS.png", Name="High Score", ImgLarge="Img/HS.png"
  },
}
当我创建多个按钮时,如何检查按下了哪个按钮?我的猜测是检查传递对象的名称,但我真的没有任何线索

单击方法:

private void Button_Click_1(object sender, RoutedEventArgs e)
{

}

在这种情况下,您可以使用一个非常有用的标记:属性
标记
。使用绑定在XAML中设置
标记
,然后检查其值

private void Button_Click_1(object sender, RoutedEventArgs e)
{
    FrameworkElement frameworkElement = sender as FrameworkElement;
    if(sender != null)
    {
        Img tag = frameworkElement.Tag as Img;

        // You directly have the Img that correspond to the button you have clicked
    }
}
在XAML中:

<Button BorderThickness="0" Click="Button_Click_1" Tag="{Binding}" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" >
    <Image x:Name="image" Width="64" Height="64" Source="{Binding ImgSmall}"/>
</Button>

在这种情况下,您可以使用一个非常有用的标记:属性
标记
。使用绑定在XAML中设置
标记
,然后检查其值

private void Button_Click_1(object sender, RoutedEventArgs e)
{
    FrameworkElement frameworkElement = sender as FrameworkElement;
    if(sender != null)
    {
        Img tag = frameworkElement.Tag as Img;

        // You directly have the Img that correspond to the button you have clicked
    }
}
在XAML中:

<Button BorderThickness="0" Click="Button_Click_1" Tag="{Binding}" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" >
    <Image x:Name="image" Width="64" Height="64" Source="{Binding ImgSmall}"/>
</Button>


您的代码只有一个按钮。你能提供更多的代码来实际演示你的问题是什么吗?好吧,我只是为一个按钮=P多加了一行。现在更清楚了吗?我想,最重要的信息是“(应用于图像/按钮的样式模板)”。是的,我后来找到了。但塞德里克想出了一个完美的解决方案!您的代码只有一个按钮。你能提供更多的代码来实际演示你的问题是什么吗?好吧,我只是为一个按钮=P多加了一行。现在更清楚了吗?我想,最重要的信息是“(应用于图像/按钮的样式模板)”。是的,我后来找到了。但塞德里克想出了一个完美的解决方案!这看起来是一个很好的解决方案!所以传递的对象不会自动包含图像的name变量?有趣的。。。谢谢你,伙计!是的,这样,标记是对按钮关联的Img对象的引用。如果它能解决你的问题,别忘了接受;)这看起来是一个很好的解决方案!所以传递的对象不会自动包含图像的name变量?有趣的。。。谢谢你,伙计!是的,这样,标记是对按钮关联的Img对象的引用。如果它能解决你的问题,别忘了接受;)