Quantcast
Viewing all articles
Browse latest Browse all 18858

How to set Row size or percentage in code?

I am creating a Grid in code and returning the generated markup to be used as the Content of a ContentPresenter. My problem is that I have clear guidelines as to the proportions of the Grid rows. Basically, out of 7 rows, I need the 1st, 2nd, and 7th rows to all be of a set percentage of the total and stay that way during all resizing of the main window. The 4th 5th, and 6th rows need to be a set percentage of the total height but set to a different value than rows 1, 2, and 7. The 3rd row, in which a DataGrid is placed, needs to be 40% of the Grid Height or height of the DataGrid's rows, whichever is less.

I see no documentation on how to do this in code, only in XAML. Any help would be greatly appreciated.

        public override UIElement GetMarkup
        {
            get
            {
				//
				var oasisContentControl = new Grid() { Name = "OasisOrders", VerticalAlignment = VerticalAlignment.Stretch, HorizontalAlignment = HorizontalAlignment.Stretch};

				//
				var colDef1 = new ColumnDefinition();
				var colDef2 = new ColumnDefinition();
				var colDef3 = new ColumnDefinition();
				oasisContentControl.ColumnDefinitions.Add(colDef1);
				oasisContentControl.ColumnDefinitions.Add(colDef2);
				oasisContentControl.ColumnDefinitions.Add(colDef3);

				//
				var rowDef1 = new RowDefinition() { Height = GridLength.Auto }; ;
				var rowDef2 = new RowDefinition();
				var rowDef3 = new RowDefinition();
				var rowDef4 = new RowDefinition();
				var rowDef5 = new RowDefinition();
				var rowDef6 = new RowDefinition();
				var rowDef7 = new RowDefinition();
				oasisContentControl.RowDefinitions.Add(rowDef1);
				oasisContentControl.RowDefinitions.Add(rowDef2);
				oasisContentControl.RowDefinitions.Add(rowDef3);
				oasisContentControl.RowDefinitions.Add(rowDef4);
				oasisContentControl.RowDefinitions.Add(rowDef5);
				oasisContentControl.RowDefinitions.Add(rowDef6);
				oasisContentControl.RowDefinitions.Add(rowDef7);

				//
				var border1 = new Border() { Background = Brushes.Moccasin, BorderBrush = Brushes.Black, BorderThickness = new Thickness(1) };
				border1.SetValue(Grid.RowProperty, 0);
				border1.SetValue(Grid.ColumnProperty, 0);
				var txt1 = new TextBlock() { Text = "Oasis Order Pick List", FontSize = 36, VerticalAlignment = VerticalAlignment.Center, HorizontalAlignment = HorizontalAlignment.Stretch };
            	border1.Child = txt1;
            	oasisContentControl.Children.Add(border1);

				//
				var border2 = new Border() { Background = Brushes.Moccasin, BorderBrush = Brushes.Black, BorderThickness = new Thickness(1) };
				border2.SetValue(Grid.RowProperty, 0);
				border2.SetValue(Grid.ColumnProperty, 1);
				var lbl1 = new Label() { Name = "CurrentDateLabel", FontSize = 18, VerticalAlignment = VerticalAlignment.Center, HorizontalAlignment = HorizontalAlignment.Center };
				// Set the current date on the Oasis screen.
				var dateString = DateTime.Today.GetDateTimeFormats();
				lbl1.Content = dateString[7];
				border2.Child = lbl1;
				oasisContentControl.Children.Add(border2);

				//
				var border3 = new Border() { Background = Brushes.Moccasin, BorderBrush = Brushes.Black, BorderThickness = new Thickness(1) };
				border3.SetValue(Grid.RowProperty, 0);
				border3.SetValue(Grid.ColumnProperty, 2);
				var logoControl = new UcLogoControl();
				border3.Child = logoControl;
				oasisContentControl.Children.Add(border3);

				//
				var border4 = new Border() { Background = Brushes.Moccasin, BorderBrush = Brushes.Black, BorderThickness = new Thickness(1) };
				border4.SetValue(Grid.RowProperty, 1);
				border4.SetValue(Grid.ColumnProperty, 0);
				border4.SetValue(Grid.ColumnSpanProperty, 3);
				var dockPanel1 = new DockPanel();
				var lbl2 = new Label() { Name = "ChooseDeptLabel", Content = "Choose Dept. / Pack Line:" };
				DockPanel.SetDock(lbl2, Dock.Top);
				dockPanel1.Children.Add(lbl2);
				var comboBox1 = new ComboBox() { Height = 20, Width = 50, HorizontalAlignment = HorizontalAlignment.Left };
				DockPanel.SetDock(comboBox1, Dock.Bottom);
				var pickLists = GetPickLists(PackLine);
				foreach (var findPickListsForPackLine in pickLists)
				{
					int index = comboBox1.Items.Add(findPickListsForPackLine.PickList);
				}
				//comboBox1.DisplayMemberPath = "PickList";
				// TODO: Get this working correctly. See: http://social.msdn.microsoft.com/Forums/vstudio/en-US/0575234b-0e7c-4e5b-8a13-13ce82667971/how-do-i-bind-an-observablecollection-to-a-combobox-in-code
				//ObservableCollection<FindPickListsForPackLine>  pickLists1 = GetPickLists(PackLine);
				//var binding1 = new Binding { Source = pickLists1 };
				//comboBox1.SetBinding(ItemsControl.ItemsSourceProperty, binding1);
				//comboBox1.DisplayMemberPath = "PickList";

				if (comboBox1.Items.Count == 1) comboBox1.SelectedItem = comboBox1.Items[0];
				dockPanel1.Children.Add(comboBox1);
				border4.Child = dockPanel1;
				oasisContentControl.Children.Add(border4);

				//
				//var stack = new StackPanel();
				var scroller = new ScrollViewer() { HorizontalScrollBarVisibility = ScrollBarVisibility.Auto, VerticalScrollBarVisibility = ScrollBarVisibility.Auto, CanContentScroll = true };
				var dgOrderData = new DataGrid
				{
					Name = "OasisOrdersData",
					HorizontalScrollBarVisibility = ScrollBarVisibility.Auto,
					VerticalScrollBarVisibility = ScrollBarVisibility.Auto,
					IsReadOnly = true,
					CanUserReorderColumns = false
				};
				dgOrderData.SetValue(Grid.RowProperty, 2);
				dgOrderData.SetValue(Grid.ColumnProperty, 0);
				dgOrderData.SetValue(Grid.ColumnSpanProperty, 3);
				dgOrderData.ItemsSource = GetPlannedOrders((string)comboBox1.SelectedItem);
				//dgOrderData.Columns[1].Visibility = Visibility.Hidden;
				//stack.Children.Add(dgOrderData);
				//scroller.Content = stack;
				scroller.Content = dgOrderData;
				scroller.SetValue(Grid.RowProperty, 2);
				scroller.SetValue(Grid.ColumnProperty, 0);
				scroller.SetValue(Grid.ColumnSpanProperty, 3);

				oasisContentControl.Children.Add(scroller);

				//
				var border5 = new Border() { Background = Brushes.Moccasin, BorderBrush = Brushes.Black, BorderThickness = new Thickness(1) };
				border5.SetValue(Grid.RowProperty, 3);
				border5.SetValue(Grid.ColumnProperty, 0);
				border5.SetValue(Grid.ColumnSpanProperty, 3);
				var txt2 = new TextBlock() { Text = "Selected Order Information", FontSize = 18, VerticalAlignment = VerticalAlignment.Center, HorizontalAlignment = HorizontalAlignment.Stretch };
				border5.Child = txt2;
				oasisContentControl.Children.Add(border5);

				//
				var border6 = new Border() { Background = Brushes.Moccasin, BorderBrush = Brushes.Black, BorderThickness = new Thickness(1) };
				border6.SetValue(Grid.RowProperty, 4);
				border6.SetValue(Grid.ColumnProperty, 0);
				var dockPanel2 = new DockPanel();
				var txt3 = new TextBlock() { Text = "Packing Order Information", FontSize = 14, VerticalAlignment = VerticalAlignment.Center, HorizontalAlignment = HorizontalAlignment.Stretch };
				DockPanel.SetDock(txt3, Dock.Top);
				dockPanel2.Children.Add(txt3);
				var orderInfoGrid = new Grid() { Name = "OrderInfoGrid" };

				var colDef11 = new ColumnDefinition() { Width = GridLength.Auto };
				var colDef12 = new ColumnDefinition() { Width = GridLength.Auto };
				orderInfoGrid.ColumnDefinitions.Add(colDef11);
				orderInfoGrid.ColumnDefinitions.Add(colDef12);

				var rowDef11 = new RowDefinition() { Height = GridLength.Auto };
				var rowDef12 = new RowDefinition() { Height = GridLength.Auto };
				var rowDef13 = new RowDefinition() { Height = GridLength.Auto };
				var rowDef14 = new RowDefinition() { Height = GridLength.Auto };
				orderInfoGrid.RowDefinitions.Add(rowDef11);
				orderInfoGrid.RowDefinitions.Add(rowDef12);
				orderInfoGrid.RowDefinitions.Add(rowDef13);
				orderInfoGrid.RowDefinitions.Add(rowDef14);
				var dockPanel3 = new DockPanel();
				dockPanel3.SetValue(Grid.RowProperty, 0);
				dockPanel3.SetValue(Grid.ColumnProperty, 0);
				var lbl3 = new Label() { Name = "CustomerLabel", VerticalAlignment = VerticalAlignment.Center, HorizontalAlignment = HorizontalAlignment.Left, Content = "Customer: " };
				dockPanel3.Children.Add(lbl3);

				orderInfoGrid.Children.Add(dockPanel3);

				border6.Child = dockPanel2;
				oasisContentControl.Children.Add(border6);

				//
				var processButton = new Button() { Name = "ProcessPackLine", FontSize = 18, Content = "Run Pack Line", ClickMode = ClickMode.Press };
				processButton.SetValue(Grid.RowProperty, 6);
				processButton.SetValue(Grid.ColumnProperty, 2);
				oasisContentControl.Children.Add(processButton);

				return oasisContentControl;
            }
        }

 


Viewing all articles
Browse latest Browse all 18858

Trending Articles