2015-04-15 37 views
-1

我有一個網格和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) 
     { 

     } 
    } 
+0

你的問題真的很難理解。我假設你想刪除(list?grid?其他任何東西?)中的選定項並將其刪除,所以你不必傳遞任何東西到你的按鈕,檢查選擇哪個元素。 – Gusman

回答

2

您可以分配Coletor到該按鈕的Tag屬性

btnDelete.Tag = coletor; 

,然後在事件中得到回報:

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

這有點笨重,但它會工作。

+1

Tag屬性的常見用法是存儲與控件緊密關聯的數據。例如,如果您擁有顯示有關客戶信息的控件,則可以將包含客戶訂單歷史記錄的DataSet存儲在該控件的Tag屬性中,以便快速訪問數據。 [Control.Tag Property](https://msdn.microsoft.com/en-us/library/system.windows.forms.control.tag%28v=vs.110%29.aspx?f=255&MSPPError=- 2147217396) – MVCDS

+0

@MVCDS:我發現標籤中的數據有點「匿名」。他們沒有解釋他們是什麼,你必須知道。如果有不同的編碼器出現,他們可能會意外地將標籤用於別的東西。但使用它們並沒有錯。個人選擇和情況,我猜。 :) – Ulric

+0

但我唯一能想到的其他方式就是我所回答的問題,它落在了同一個問題上,但卻弄髒了HTML,更糟糕的是,這就是爲什麼我投票選出了答案。 – MVCDS

0

給該按鈕的屬性...

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

...恢復的內容uou需要發件人

//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可能是錯的,但我認爲總體思路是對的。

相關問題