这是我使用的代码,但我不确定为什么它可以通过我在窗口形式中完成的方式实现。
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);
答案 0 :(得分: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]);
}
试试这个
答案 1 :(得分:1)
我认为它必须得到空引用错误。
首先声明并分配给Button数组。
但每个按钮都需要分配。
btnMonday [i] = new Button();
答案 2 :(得分: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]);
}
我完全不明白你的问题,但你的代码不能编译。如上所述,我正确修复了它。
答案 3 :(得分:1)
如果你坚持使用SetValue方法,试试这个:
btnMonday[i].SetValue(Button.WidthProperty, 38);
btnMonday[i].SetValue(Button.HeightProperty, 256);
btnMonday[i].SetValue(Button.ContentProperty, timeslot[i]);