在运行时将网格添加为ListBox的项目最终会导致未知错误/崩溃

时间:2012-06-19 13:28:42

标签: c# windows-phone-7 listbox grid

在下面的代码中,我正在尝试...

  1. 在网格(mygrid)中放置一个TextBlock(txtDays)(作为行)
  2. 将网格放在ListBox(lsBox)
  3. 最后将ListBox放入另一个Grid(ContentPanel)
  4. 程序正在崩溃而没有正确的错误,并且我执行此代码时模拟器停止。如果我将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);
        }
    

2 个答案:

答案 0 :(得分: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);
    }
}

答案 1 :(得分:1)

您尝试在每次mygrid次迭代时将lstBox添加到foreach。将lstBox.Items.Add(mygrid);置于循环之外,使其仅添加一次。

如果您想要多个网格,请将mygrid的定义放在循环中。