0

这是我使用的代码,但我不确定为什么它可以通过我以窗口形式完成的方式来实现。

        Button[] btnMonday = new Button[20];
        string[] timeslot = { "08:00 AM", "08:30 AM", "09:00 AM", "09:30 AM", "10:00 AM", "10:30 AM", "11:00 AM", "11:30 AM", "12:00 PM", "12:30 PM", "01:00 PM", "01:30 PM", "02:00 PM", "02:30 PM", "03:00 PM", "03:30 PM", "04:00 PM", "04:30 PM", "05:00 PM", "05:30 PM" };

        #region Monday
        for (int i = 0; i < 20; i++)
        {
            btnMonday[i].SetValue(Height, 38);
            btnMonday[i].SetValue(Width, 256);
            btnMonday[i].SetValue(Content, timeslot[i]);
            btnMonday[i].SetValue(Background, 0xFF, 0xB1, 0xB1, 0xB1);

            // Sets dependency properties
            btnMonday[i].SetValue(Grid.ColumnProperty, 0);
            btnMonday[i].SetValue(Grid.RowProperty, i + 1);
            // Adds the dynamically created control to the canvas
            LayoutRoot.Children.Add(btnMonday[i]);
        }

更新

这部分代码我仍然收到错误::

 Button[] btnMonday = new Button[20];
            string[] timeslot = { "08:00 AM", "08:30 AM", "09:00 AM", "09:30 AM", "10:00 AM", "10:30 AM", "11:00 AM", "11:30 AM", "12:00 PM", "12:30 PM", "01:00 PM", "01:30 PM", "02:00 PM", "02:30 PM", "03:00 PM", "03:30 PM", "04:00 PM", "04:30 PM", "05:00 PM", "05:30 PM" };

            #region Monday
            for (int i = 0; i < 20; i++)
            {
                btnMonday[i] = new Button();
                btnMonday[i].SetValue(Height, 38);
                btnMonday[i].SetValue(Width, 256);
                btnMonday[i].SetValue(Content, timeslot[i]);

                // Sets dependency properties
                btnMonday[i].SetValue(Grid.ColumnProperty, 0);
                btnMonday[i].SetValue(Grid.RowProperty, i + 1);
                // Adds the dynamically created control to the canvas
                LayoutRoot.Children.Add(btnMonday[i]);
            }

有没有语法错误的可能性?我可以知道如何将背景值设置为按钮吗?它似乎不适用于以前定义背景颜色的样式。我得到的错误::

Error   2   Argument 1: cannot convert from 'double' to 'System.Windows.DependencyProperty' 

Error   1   The best overloaded method match for 'System.Windows.DependencyObject.SetValue(System.Windows.DependencyProperty, object)' has some invalid arguments

对于这几行

        btnMonday[i].SetValue(Height, 38);
        btnMonday[i].SetValue(Width, 256);
        btnMonday[i].SetValue(Content, timeslot[i]);
        btnMonday[i].SetValue(Background, 0xFF, 0xB1, 0xB1, 0xB1);
4

4 回答 4

1
    Button[] btnMonday = new Button[20];
    string[] timeslot = { "08:00 AM", "08:30 AM", "09:00 AM", "09:30 AM", "10:00 AM", "10:30 AM", "11:00 AM", "11:30 AM", "12:00 PM", "12:30 PM", "01:00 PM", "01:30 PM", "02:00 PM", "02:30 PM", "03:00 PM", "03:30 PM", "04:00 PM", "04:30 PM", "05:00 PM", "05:30 PM" };

    #region Monday
    for (int i = 0; i < 20; i++)
    {
        btnMonday[i] = new Button();
        btnMonday[i].Height = 38;
        btnMonday[i].Width = 256;
        btnMonday[i].Content = timeslot[i];

        // Sets dependency properties
        Grid.SetColumn(btnMonday[i], 0);
        Grid.SetRow(btnMonday[i], i + 1);
        // Adds the dynamically created control to the canvas
        LayoutRoot.Children.Add(btnMonday[i]);
    }

试试这个

于 2011-11-09T01:44:01.567 回答
1

我认为它必须得到空引用错误。

您首先声明并分配给 Button 数组。

但是每个按钮都需要分配。

btnMonday[i] = new Button();

于 2011-11-09T01:44:09.417 回答
1
        Button[] btnMonday = new Button[20];
        string[] timeslot = { "08:00 AM", "08:30 AM", "09:00 AM", "09:30 AM", "10:00 AM", "10:30 AM", "11:00 AM", "11:30 AM", "12:00 PM", "12:30 PM", "01:00 PM", "01:30 PM", "02:00 PM", "02:30 PM", "03:00 PM", "03:30 PM", "04:00 PM", "04:30 PM", "05:00 PM", "05:30 PM" };

        for (int i = 0; i < 20; i++)
        {
            btnMonday[i] = new Button();
            btnMonday[i].Height = 38;
            btnMonday[i].Width = 256;
            btnMonday[i].Content = timeslot[i];
            btnMonday[i].Margin = new Thickness(0, i * 68, 0, 0);

            // Sets dependency properties
            btnMonday[i].SetValue(Grid.ColumnProperty, 0);
            btnMonday[i].SetValue(Grid.RowProperty, i + 1);

            // Adds the dynamically created control to the canvas
            LayoutRoot.Children.Add(btnMonday[i]);
        }

我不完全理解你的问题,但你的代码没有编译。如上所述,我正确修复了它。

于 2011-11-09T02:12:23.060 回答
1

如果你坚持使用 SetValue 方法,试试这个:

        btnMonday[i].SetValue(Button.WidthProperty, 38);
        btnMonday[i].SetValue(Button.HeightProperty, 256);
        btnMonday[i].SetValue(Button.ContentProperty, timeslot[i]);
于 2011-11-09T02:20:02.703 回答