如何更改我的点击事件中的属性?

How to change the atribute in my click event?

我有一个网格列表和每个网格中的 2 个按钮。这些按钮之一是删除按钮,因此我需要将 Coletor 发送到我的点击事件以更改它的属性。如何将我的 Coletor 传递给我的点击事件?

我创建网格的方法:

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
        if (NavigationContext.QueryString["email"] != null)
        {
            email = NavigationContext.QueryString["email"];
        }

        List<Grid> listaGrids = new List<Grid>();

        int i = 0;

        AppDataContext db = new AppDataContext();

        Pessoa pessoa = db.Pessoas.Single(c => c.Email == email);

        foreach (Coletor coletor in pessoa.Coletores)
        {
            if (coletor.Ativado == true)
            {
                Grid aux = new Grid();
                ColumnDefinition c1 = new ColumnDefinition();
                ColumnDefinition c2 = new ColumnDefinition();
                ColumnDefinition c3 = new ColumnDefinition();

                aux.Width = 440;
                aux.Height = 80;

                aux.VerticalAlignment = VerticalAlignment.Center;

                c1.Width = new System.Windows.GridLength(220);
                c2.Width = new System.Windows.GridLength(80);
                c3.Width = new System.Windows.GridLength(80);



                coletorAux = coletor;

                aux.ColumnDefinitions.Add(c1);
                aux.ColumnDefinitions.Add(c2);
                aux.ColumnDefinitions.Add(c3);

                RowDefinition r1 = new RowDefinition();

                aux.RowDefinitions.Add(r1);

                HyperlinkButton aux2 = new HyperlinkButton();
                aux2.Content = coletor.Nome;
                aux2.FontSize = 42;
                aux2.NavigateUri = new Uri("/OcorrenciasPage.xaml?coletorId=" + coletor.Id.ToString(), UriKind.RelativeOrAbsolute);
                i++;

                Button btnEdit = new Button();

                var brush = new ImageBrush();

                brush.ImageSource = new System.Windows.Media.Imaging.BitmapImage(new Uri("/icons/edit.png", UriKind.RelativeOrAbsolute));

                btnEdit.Background = brush;

                btnEdit.Width = 80;
                btnEdit.Height = 80;

                btnEdit.Click += btnEdit_Click;

                Button btnDelete = new Button();

                ImageBrush brush2 = new ImageBrush();

                brush2.ImageSource = new System.Windows.Media.Imaging.BitmapImage(new Uri("/icons/delete.png", UriKind.RelativeOrAbsolute));

                btnDelete.Background = brush2;

                btnDelete.Width = 80;
                btnDelete.Height = 80;

                btnDelete.Click += btnDelete_Clickrr;

                Grid.SetColumn(aux2, 0);
                Grid.SetColumn(btnEdit, 1);
                Grid.SetColumn(btnDelete, 2);
                Grid.SetRow(aux2, 0);
                Grid.SetRow(btnEdit, 0);
                Grid.SetRow(btnDelete, 0);



                aux.Children.Add(aux2);
                aux.Children.Add(btnEdit);
                aux.Children.Add(btnDelete);

                listaGrids.Add(aux);
            }
        }

        ListBox coletores = new ListBox();
        coletores.ItemsSource = listaGrids;
        stcList.Children.Add(coletores);

        base.OnNavigatedTo(e);
    }

还有我的点击事件:

void btnDelete_Click(object sender, RoutedEventArgs e)
    {
        if (MessageBox.Show("Deseja realmente excluir esse coletor?", "Confirmação", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
        {

        }
    }

您可以将 Coletor 分配给按钮的标签 属性

btnDelete.Tag = coletor;

然后在活动中取回:

var coletor = ((sender as Button).Tag as Coletor);

它有点笨拙,但它会起作用。

给按钮一个属性...

//after creating the button
btnDelete.Attributes.Add('coletorId', coletorId.ToString());

...在发件人上恢复您需要的内容

//inside btnDelete_Click
Button btnDelete = (Button) sender;
string attr = btnDelete.Attributes['coletorId'];
Guid id = Guid.Parse(attr);
Coletor = db.Coletores.SingleOrDefault(p => p.id == id);

.NEt API可能有误,但我觉得大体思路是对的。