我有一個網格和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)
{
}
}
你的問題真的很難理解。我假設你想刪除(list?grid?其他任何東西?)中的選定項並將其刪除,所以你不必傳遞任何東西到你的按鈕,檢查選擇哪個元素。 – Gusman