0

okay, so I have this code that generically builds a screen and then after the 3rd column in grid is added, I break the for loop , and then it looks like this .

CODE:

 private void buildScreen()
 {
     try
     {
         foreach (var block in WorkflowBase.ViewModel_WFTransaction.Transaction.Form.Blocks.Where(item => item.blockType == AccTech.WP8.Workflow.DataModels.BlockAttributes.BlockType.DetailGrid))
         {
             myscroller = new ScrollViewer();
             myscroller.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
             myscroller.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
             myscroller.HorizontalScrollBarVisibility = ScrollBarVisibility.Visible;

             Grid grid = new Grid();
             grid.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
             grid.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;

             foreach (var field in block.Fields)
             {
                 if (field.PhoneField == true)
                 {
                     int count = block.Fields.IndexOf(field);
                     ColumnDefinition c1 = new ColumnDefinition();
                     //c1.Width = new GridLength(1000, GridUnitType.Star);                        
                     grid.ColumnDefinitions.Add(c1);

                     TextBlock headers = new TextBlock();
                     headers.Tap += headers_Tap;
                     headers.Text = field.FieldDescLoc.ToString();
                     headers.Name = field.ColumnName;
                     headers.Foreground = new SolidColorBrush(Colors.Black);
                     headers.FontSize = 22;
                     headers.TextWrapping = TextWrapping.Wrap;
                     headers.SetValue(Grid.ColumnProperty, count);
                     headers.Width = 100;
                     headers.Margin = new Thickness(0, 0, 10, 0);

                     if (field.FieldType == FieldAttributes.DataType.dtString)
                     {
                        headers.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
                     }
                     else if (field.FieldType == FieldAttributes.DataType.dtInteger || field.FieldType == FieldAttributes.DataType.dtDate)
                     {
                        headers.HorizontalAlignment = System.Windows.HorizontalAlignment.Right;
                     }
                     grid.Children.Add(headers);

                     foreach (var row in block.Rows)
                     {
                            RowDefinition r1 = new RowDefinition();
                            r1.Height = new GridLength(50, GridUnitType.Star);
                            grid.RowDefinitions.Add(r1);

                            int i = block.Rows.IndexOf(row);
                            int count2 = block.Fields.IndexOf(field);

                            TextBlock item = new TextBlock();
                            item.Margin = new Thickness(5, 5, 5, 10);
                            item.Text = block.Rows[i].getKeyValue(field.ColumnName);
                            item.SetValue(Grid.RowProperty, i + 1);
                            item.SetValue(Grid.ColumnProperty, count2);

                            if (field.FieldType == FieldAttributes.DataType.dtString)
                            {
                                item.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
                            }
                            else if (field.FieldType == FieldAttributes.DataType.dtInteger || field.FieldType == FieldAttributes.DataType.dtDate)
                            {
                                item.HorizontalAlignment = System.Windows.HorizontalAlignment.Right;
                            }
                            grid.Children.Add(item);
                        }

                        if (this.Orientation == PageOrientation.Portrait || this.Orientation == PageOrientation.PortraitDown || this.Orientation == PageOrientation.PortraitUp)
                        {
                            if (count >= 3)
                            {
                                myscroller.HorizontalScrollBarVisibility = ScrollBarVisibility.Disabled;
                                break;
                            }
                        }
                    }
                }                   
                myscroller.Content = grid;
                this.PageTilte.Text = block.BlockName.ToString();
                //ParentControl.Children.Clear();
                this.ParentControl.Children.Add(myscroller);                  
            }

Screen Looks like this :

Detail screen

so that works fine, now i turn the screen sideways to landscape view , this is the code.

    private void PhoneApplicationPage_OrientationChanged(object sender, OrientationChangedEventArgs e)
    {
        this.ParentControl.Children.Clear();
        buildScreen();            
    }

as you can see i clear the parent control, then i call the method to build screen again!.

and this is my error. enter image description here

and the innier exeption is null! can anyone please tell me what is wrong here?

2
  • This is why I never build UI in code ! Jul 16, 2013 at 7:54
  • if you have generic data and controls you dont really have a choice hehe :P
    – Arrie
    Jul 17, 2013 at 6:38

1 Answer 1

0

i found it! , after a few minutes of carefull reading!, i found that my textblock.Name property became null, so just put in a null check and made the name "", and it worked!

 TextBlock headers = new TextBlock();
                        headers.Tap += headers_Tap;
                        headers.Text = field.FieldDescLoc.ToString();
                        //headers.Name = field.FieldDescLoc;

                        if (!string.IsNullOrEmpty(field.ColumnName))
                            headers.Name = field.ColumnName;
                        else
                            headers.Name = field.FieldDesc;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.