0

我得到了这些代码:

在 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"
  },
}

在 XAML(适用于的样式模板Images/Buttons)中:

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

Button当我创建多个时,如何检查按下的是哪个1 Button?我的猜测是检查传递对象的名称,但我真的不知道。

点击方式:

private void Button_Click_1(object sender, RoutedEventArgs e)
{

}
4

1 回答 1

4

You can use a very useful in this case: the property Tag. You set the Tag in XAML with a binding, then check its value.

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
    }
}

In 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>
于 2013-01-25T00:02:29.980 回答