无法在silverlight中创建按钮数组



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

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

如果您坚持使用SetValue方法,请尝试以下方法:

        btnMonday[i].SetValue(Button.WidthProperty, 38);
        btnMonday[i].SetValue(Button.HeightProperty, 256);
        btnMonday[i].SetValue(Button.ContentProperty, timeslot[i]);

我认为它一定得到了空引用错误。

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

但每个按钮都需要分配。

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].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]);
    }

试试这个

        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]);
        }

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

相关内容

  • 没有找到相关文章

最新更新