2015-11-24 89 views
0

我想獲取索引或永久更改我的按鈕的值。 我得到了二維列表Button[,] bt; 我試圖通過bt.Indexof得到它,但它不適用於二維數組。我想永久更改值h1,這是我的數據庫中的一列。獲取索引或更改Button的值。

private void cbCinemaHall_SelectionChanged(object sender, SelectionChangedEventArgs e) 
    { 

     grdMain.Children.Clear(); 


     f1 = CHallObj.GetHallList(); 
     h1 = CHallObj.halls2(); 
     bt = new Button[h1[cbCinemaHall.SelectedIndex].GetLength(0), h1[cbCinemaHall.SelectedIndex].GetLength(1)]; 
     double x = 0, y = 0; 
     int numberToTagCounter = 0; 
     for (int i = 0; i < bt.GetLength(0); i++) 
     { 

      int columnCounter = 0; 
      for (int j = 0; j < bt.GetLength(1); j++) 
      { 
       if (Convert.ToInt32(h1[cbCinemaHall.SelectedIndex][i, j]) == 0) 
       { //0=wolna przestrzeń 
        y = i * height; 
        if (j == 0) 
         x = 0; 
        x += (width + 1); 
       } 
       if (Convert.ToInt32(h1[cbCinemaHall.SelectedIndex][i, j]) == 1) 
       { //1=pierwsza grupa cenowa 
        columnCounter++; 
        numberToTagCounter++; 
        bt[i, j] = new Button(); 
        bt[i, j].Height = height; 
        bt[i, j].Width = width; 
        bt[i, j].Content = columnCounter.ToString(); 
        bt[i, j].Tag = numberToTagCounter; 
        bt[i, j].Background = Brushes.Green; 
        y = i * height; 
        if (j == 0) 
         x = 0; 
        x += (width + 1); 
        bt[i, j].Margin = new Thickness(x, y, 0, 0); 
        bt[i, j].Click += Button_Click; 
        //bt[i, j].Click += button_Click_1; 
        //bt[i, j].MouseDoubleClick += Generating_MouseDoubleClick; 
       } 
       if (Convert.ToInt32(h1[cbCinemaHall.SelectedIndex][i, j]) == 2) 
       { //2=premiumqualityplaces 
        columnCounter++; 
        numberToTagCounter++; 
        bt[i, j] = new Button(); 
        bt[i, j].Height = height; 
        bt[i, j].Width = width; 
        bt[i, j].Content = columnCounter.ToString(); 
        bt[i, j].Tag = numberToTagCounter; 
        bt[i, j].Background = Brushes.Coral; 
        y = i * height; 
        if (j == 0) 
         x = 0; 
        x += (width + 1); 
        bt[i, j].Margin = new Thickness(x, y, 0, 0); 
        bt[i, j].Click += Button_Click; 

        //bt[i, j].MouseDoubleClick += Generating_MouseDoubleClick; 

       } 
      } 
     } 
     for (int i = 0; i < bt.GetLength(0); i++) 
     { 
      for (int j = 0; j < bt.GetLength(1); j++) 
      { 
       if (bt[i, j] == null) 
        continue;     //jeśli jest zerowe pole 
       grdMain.Children.Add(bt[i, j]); //dodajemy miejsce 
      } 
     } 

    } 



    private void Button_Click(object sender, RoutedEventArgs e) 
    { 


     int ind = bt.Indexof 


     // context.SaveChanges(); 


    } 

}

回答

1

個人而言,我會創建一個包含所有ButtonData一類的ObservableCollection<T>,並使用ItemsControl繪製。然後很容易找到物品的索引,或者對物品進行任何類型的操作,例如移動行/列值。

例如,如果你想使用AttachedProperty,像this

public class MyButtonData() 
{ 
    public string Content { get; set; } 
    public int RowIndex { get; set; } 
    public int ColumnIndex { get; set; } 
} 

XAML爲ItemsControl的

<ItemsControl ItemsSource="{Binding MyCollection}"> 
    <!-- ItemsPanelTemplate --> 
    <ItemsControl.ItemsPanel> 
     <ItemsPanelTemplate> 
      <Grid> 
       <Grid.RowDefinitions> 
        <RowDefinition /> 
        <RowDefinition /> 
        <RowDefinition /> 
       </Grid.RowDefinitions> 
       <Grid.ColumnDefinitions> 
        <ColumnDefinition /> 
        <ColumnDefinition /> 
        <ColumnDefinition /> 
       </Grid.ColumnDefinitions> 
      </Grid> 
     </ItemsPanelTemplate> 
    </ItemsControl.ItemsPanel> 

    <!-- ItemContainerStyle --> 
    <ItemsControl.ItemContainerStyle> 
     <Style> 
      <Setter Property="Grid.Column" Value="{Binding ColumnIndex}" /> 
      <Setter Property="Grid.Row" Value="{Binding RowIndex}" /> 
     </Style> 
    </ItemsControl.ItemContainerStyle> 

    <!-- ItemTemplate --> 
    <ItemsControl.ItemTemplate> 
     <DataTemplate> 
      <Button Content="{Binding Content}" ... /> 
     </DataTemplate> 
    </ItemsControl.ItemTemplate> 
</ItemsControl> 

您還可以結合您的網格的行/列計數