这是我使用的代码,但我不确定为什么它可以通过我以窗口形式完成的方式来实现。
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);