Question

This is the code i using, but i not sure why it can be implement via the way i done in window form.

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

update

i still getting error with this portion of code ::

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

is there any possibility of syntac error ?? and may i know how to set value of background to button ?? it seem not work well with the previous style of define background color.The error i get ::

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

and

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

for this few line

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

Solution

if you insist using SetValue method, try this:

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

OTHER TIPS

I think it's gotta get null reference error.

You first declare and allocate to Button array.

But each button needs allocation.

btnMonday[i] = new Button();

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

try this

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

I don't understand your question totally, but your code dosen't compile. As above, I fixed it properly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top