【问题标题】:I need a responsive grid (cell can be clicked) on the form, what control should be used?我需要表单上的响应式网格(可以单击单元格),应该使用什么控件?
【发布时间】:2016-11-10 10:29:09
【问题描述】:

我需要在表单上显示一个网格,显示为 32 x 32 个单元格,每个单元格都可以单击以执行一些操作(如 windows 日历应用程序)。我考虑过按钮和面板,但那将是 1024 个控件。有更好的方法吗?

【问题讨论】:

  • wpf 还是 winforms?一种有效的方法是让一个面板监听点击事件,然后使用点击的位置来确定接下来应该发生什么。
  • wpf 或 winform,无论哪个有办法解决这个问题。我需要这些单元格单独行动,例如,显示不同的颜色
  • 您可以使用TableLayoutPanel 或DataGridView。请参阅this example 或this one 对单元格有什么要求?它们会包含什么?
  • 它们只显示颜色(黑色或白色),就像扫雷游戏一样
  • @BrianHolsen 您应该使用 DataGridView。一个控件,你就搞定了。

标签: c# .net wpf winforms


【解决方案1】:

这是一种在 WPF 中使用按钮创建响应式网格的方法:

响应是用GridLength(1, GridUnitType.Star)创建的

MainWindow.xaml.cs

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        for (int x = 0; x < 32; x++)
        {
            buttonGrid.ColumnDefinitions.Add(new ColumnDefinition
            {
                Width = new GridLength(1, GridUnitType.Star)
            });
            buttonGrid.RowDefinitions.Add(new RowDefinition
            {
                Height = new GridLength(1, GridUnitType.Star)
            });

            for (int y = 0; y < 32; y++)
            {
                var bt = new Button();
                bt.Content = x + "" + y;
                Grid.SetRow(bt, x);
                Grid.SetColumn(bt, y);
                buttonGrid.Children.Add(bt);
                bt.Click += Bt_Click;
            }
        }
    }

    private void Bt_Click(object sender, RoutedEventArgs e)
    {
        Button bt = (Button)sender;
        bt.Background = Brushes.Black;
    }
}

MainWindow.xaml

<Grid Name="buttonGrid"></Grid>

请注意,调整大小会很慢。

【讨论】:

    猜你喜欢
    • 2021-08-23
    • 1970-01-01
    • 2013-09-17
    • 1970-01-01
    • 2023-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-23
    相关资源
    最近更新 更多