正在获取Button的索引或更改值

本文关键字:索引 获取 Button | 更新日期: 2023-09-27 18:24:03

我想获取索引或永久更改按钮的值。我得到了二维列表Button[,] bt;我试图通过bt.Indexof获得它,但它在二维数组上不起作用。我想永久更改值h1,它是我数据库中的一列。

 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();

    }

}

正在获取Button的索引或更改值

就我个人而言,我会创建一个包含所有ButtonData的类的ObservableCollection<T>,并使用ItemsControl绘制它。然后很容易找到项的索引,或者对项进行任何类型的操作,例如移动行/列值。

例如,

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

ItemsControl 的XAML

<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>

如果您想使用AttachedProperty,也可以绑定网格的行/列计数,如