0

在下面的代码中,我试图...

  1. 在 Grid(mygrid) 内放置一个 TextBlock(txtDays) (作为行)
  2. 将 Grid 放在 ListBox(lsBox) 内
  3. 最后将 ListBox 放入另一个 Grid(ContentPanel)

该程序只是在没有适当错误的情况下崩溃,并且当我执行此代码时模拟器停止。如果我将 Grid(在 2.中)放置在 stackPanel 中或直接放入另一个 Grid(在 3.中)而不将其放置在 ListBox 中,则代码有效,但不适用于 ListBox。

任何意见,帮助表示赞赏。

谢谢你。

    private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
    {
        string[] lstDays = new string[] { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };

        //Creating a ListBox
        ListBox lstBox = new ListBox();
        //Grid definition
        Grid mygrid = new Grid();
        ColumnDefinition c1 = new ColumnDefinition(); //creating a column
        mygrid.ColumnDefinitions.Add(c1);

        int i = -1;
        foreach (string item in lstDays)
        {
            i += 1;
            TextBlock txtDays = new TextBlock(); //Creating a TextBlock

            RowDefinition r1 = new RowDefinition(); //Creating a row
            mygrid.RowDefinitions.Add(r1);

            txtDays.Text = item;
            txtDays.TextAlignment = TextAlignment.Left;
            if (item == "Thursday")
            {
                txtDays.TextAlignment = TextAlignment.Right;
                txtDays.Foreground = new SolidColorBrush(Colors.Green);
            }

            mygrid.Children.Add(txtDays); //Adding the TextBlock into the grid
            Grid.SetRow(txtDays,i);       //Placing the item in a particular row inside the grid          
            lstBox.Items.Add(mygrid);     //Placing grid inside a listBox (ERROR here)

        }

        RowDefinition rNewRow = new RowDefinition();
        ContentPanel.RowDefinitions.Add(rNewRow);
        ContentPanel.Children.Add(lstBox);
    }
4

2 回答 2

2

修复错误后的代码。对于初学者来说,这是一段很好的代码。

public partial class MainPage : PhoneApplicationPage
{
    // Constructor
    public MainPage()
    {
        InitializeComponent();
    }

    private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
    {
        string[] lstDays = new string[] { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };

        //Creating a ListBox
        ListBox lstBox = new ListBox();

        foreach (string item in lstDays)
        {
            //Grid definition
            Grid mygrid = new Grid();
            mygrid.Width = 400;
            ColumnDefinition c1 = new ColumnDefinition(); //creating a column
            c1.Width = new GridLength(200);
            mygrid.ColumnDefinitions.Add(c1);

            RowDefinition r1 = new RowDefinition(); //Creating a row
            mygrid.RowDefinitions.Add(r1);

            TextBlock txtDays = new TextBlock(); //Creating a TextBlock
            txtDays.Text = item;
            txtDays.TextAlignment = TextAlignment.Left;
            if (item == "Thursday")
            {
                txtDays.TextAlignment = TextAlignment.Right;
                txtDays.Foreground = new SolidColorBrush(Colors.Green);
            }

            mygrid.Children.Add(txtDays); //Adding the TextBlock into the grid
            Grid.SetRow(txtDays,0);       //Placing the item in a particular row inside the grid          

            lstBox.Items.Add(mygrid);     //Placing grid inside a listBox    
        }

        RowDefinition rNewRow = new RowDefinition();
        ContentPanel.RowDefinitions.Add(rNewRow);
        ContentPanel.Children.Add(lstBox);
    }
}
于 2012-06-19T14:56:33.810 回答
1

您正在尝试mygridlstBox每次foreach迭代中添加。带lstBox.Items.Add(mygrid);出循环,使其仅添加一次。

mygrid如果你想要多个网格,要么将定义放在循环内。

于 2012-06-19T13:34:05.607 回答