我有一个网格列表和每个网格中的2个按钮。其中一个按钮是删除按钮,因此我需要将Coletor
发送到我的click事件以更改它的属性。如何将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)
{
}
}
答案 0 :(得分:2)
您可以将Coletor指定给按钮的标签属性
btnDelete.Tag = coletor;
然后在事件中取回:
var coletor = ((sender as Button).Tag as Coletor);
它有点笨重,但它会起作用。
答案 1 :(得分:0)
给按钮一个属性......
//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可能有误,但我认为一般的想法是正确的。