你好,我有一个问题。我需要知道如何为许多按钮添加一个事件。每个按钮都会使 labelfloor 不同的文本值(例如1-4)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApp1
{
/// <summary>
/// Interakční logika pro MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
bool Aktive;
bool lights;
int actualyfloor;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
labelFloor.Content = button(1-4).Tag.ToString(); // When i click at 1-4 button its will change value in labelFloor to (1-4)
}
}
答案 0 :(得分:1)
点击的按钮应该在参数sender
中提供给您。检查它是否为Button
类型,然后从中读取Tag
属性。
private void Button_Click(object sender, RoutedEventArgs e)
{
Button button = sender as Button;
if (button == null) return;
labelFloor.Content = button.Tag.ToString();
}