How to display gridlines in code behind - c#

Here is my code:
Grid gameboard = new Grid();
gameboard.HorizontalAlignment = HorizontalAlignment.Left;
gameboard.VerticalAlignment = VerticalAlignment.Top;
gameboard.Width = Window.Current.Bounds.Width;
gameboard.Height = Window.Current.Bounds.Width;
Border border = new Border();
border.BorderThickness = new Thickness(1);
border.BorderBrush = new SolidColorBrush(Colors.Blue);
for (int j=0;j<7;j++)
{
gameboard.ColumnDefinitions.Add(new ColumnDefinition());
}
for (int i = 0; i < 7; i++)
{
gameboard.RowDefinitions.Add(new RowDefinition());
}
I am a learner, now i want to show my grid lines, can someone help me?
thanks a lot!

Since you are learning, I will help kick start your efforts with something to get you and others in a similar situation to the next step.
Start with code like the following, and tweak it, learn it, research it, and most of all have fun.
XAML
<Grid Name="LayoutRoot" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" Margin="30" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
</Grid>
CODE
public MainPage()
{
this.InitializeComponent();
DataContext = this;
Loaded += MainPage_Loaded;
}
private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
Grid gameboard = new Grid();
gameboard.HorizontalAlignment = HorizontalAlignment.Stretch;
gameboard.VerticalAlignment = VerticalAlignment.Stretch;
for (int j = 0; j < 7; j++)
{
var cd = new ColumnDefinition();
cd.Width = new GridLength(1, GridUnitType.Star);
var rd = new RowDefinition();
rd.Height = new GridLength(1, GridUnitType.Star);
gameboard.ColumnDefinitions.Add(cd);
gameboard.RowDefinitions.Add(rd);
}
for (int j = 0; j < 7; j++)
{
for (int i = 0; i < 7; i++)
{
Border border = new Border();
border.BorderThickness = new Thickness(1);
border.BorderBrush = new SolidColorBrush(Colors.Blue);
border.HorizontalAlignment = HorizontalAlignment.Stretch;
border.VerticalAlignment = VerticalAlignment.Stretch;
var tb = new TextBlock();
tb.Text = string.Format($"i={i}; j={j}");
tb.Margin = new Thickness(4);
Grid.SetColumn(border, j);
Grid.SetRow(border, i);
border.Child = tb;
gameboard.Children.Add(border);
}
}
LayoutRoot.Children.Add(gameboard);
}
RESULT
SUMMARY
It's a start. It's not perfect, and to get the inner borders to not be thicker than the edges will take a small amount of effort, but should not be too difficult. Hint: think about how to use border.BorderThickness = new Thickness(l, t, r, b); where l/t/r/b are 1 or 0 depending on i/j. I might even make this an interview question; could be a fun discussion.

You can use Grid.ShowGridLines Property and add grid lines.
gameboard.ShowGridLines = true;

Related

Grid does not fill the whole window, why?

I have written a UI for an Game of Life (Automation Theory) and the algorithm works just fine. I have also created a pop-up which will start first and where you can set the amount of rows and columns the game should have. This works also fine. Somehow my Grid only fills a small amount of the space in the window (Like it would only be in the left top cell of another Grid). I cannot work out why this is happening. Help would be much appreciated. I am aware that the code is quit complex and messy at this point. The GameCreation() Method is calling the popup window first. After that I declare some variables and my grid. Then I create rows and columns and button which I store in the grid as well. There are no errors or exceptions.
This is how the window looks like:
private void GameCreation()
{
//Pop-up Fenster aufrufen und eingaben in rowCount und colCount speichern
List<int> list = new List<int>();
list = OptionsWindow();
//Variablen deklarieren
int nameNum = 1;
int rowCount = list[0];
int colCount = list[1];
Button[,] buttons = new Button[rowCount, colCount];
Grid MainGrid = new Grid();
Window1.Content = MainGrid;
for (int i = 0; i < rowCount; i++)
{
for (int j = 0; j < colCount; j++)
{
// create Grid.RowDefinition and Grid.ColumnDefinition based on rowCount and colCount
RowDefinition row = new RowDefinition();
GridLengthConverter gridLengthConverter = new GridLengthConverter();
row.Height = (GridLength)gridLengthConverter.ConvertFrom("*");
MainGrid.RowDefinitions.Add(row);
ColumnDefinition column = new ColumnDefinition();
column.Width = (GridLength)gridLengthConverter.ConvertFrom("*");
MainGrid.ColumnDefinitions.Add(column);
// Button erstellen
Button button = new Button
{
Name = "B" + nameNum,
Background = Brushes.Gray,
HorizontalAlignment = HorizontalAlignment.Stretch,
VerticalAlignment = VerticalAlignment.Stretch,
Padding = new Thickness(1, 1, 1, 1)
};
button.Click += ChangeColor;
//den Button zum Grid hinzufügen an der richtigen Stelle
Grid.SetRow(button, i);
Grid.SetColumn(button, j);
MainGrid.Children.Add(button);
//Button buttons[,] hinzufügen
buttons[i, j] = button;
nameNum++;
}
}
//resetButton deklarieren
Button resetButton = new()
{
Name = "resetButton",
Background = Brushes.DodgerBlue,
HorizontalAlignment = HorizontalAlignment.Stretch,
VerticalAlignment = VerticalAlignment.Stretch,
Padding = new Thickness(1, 1, 1, 1),
Content = new Image
{
Source = new BitmapImage(new Uri("C:\\Users\\myname\\source\\repos\\GameOfLife\\GameOfLifeApp\\ResetButton2.png"))
}
};
resetButton.Click += Reset;
Grid.SetRow(resetButton, rowCount);
Grid.SetColumn(resetButton, 0);
Grid.SetColumnSpan(resetButton, 1);
//StartButton deklarieren
RowDefinition row2 = new RowDefinition();
GridLengthConverter gridLengthConverter2 = new GridLengthConverter();
row2.Height = (GridLength)gridLengthConverter2.ConvertFrom("*");
MainGrid.RowDefinitions.Add(row2);
Button startButton = new()
{
Name = "startButton",
Background = Brushes.DodgerBlue,
HorizontalAlignment = HorizontalAlignment.Stretch,
VerticalAlignment = VerticalAlignment.Stretch,
Padding = new Thickness(1, 1, 1, 1),
Content = "Start",
HorizontalContentAlignment = HorizontalAlignment.Center,
VerticalContentAlignment = VerticalAlignment.Center,
Foreground = Brushes.White
};
startButton.Click += Start;
Grid.SetRow(startButton, rowCount);
Grid.SetColumn(startButton, 1);
int colSpan = colCount - 2;
Grid.SetColumnSpan(startButton, colSpan);
//clearButton deklarieren
Button clearButton = new()
{
Name = "clearButton",
Background = Brushes.DodgerBlue,
HorizontalAlignment = HorizontalAlignment.Stretch,
VerticalAlignment = VerticalAlignment.Stretch,
Padding = new Thickness(1, 1, 1, 1),
Content = new Image
{
Source = new BitmapImage(new Uri("C:\\Users\\myname\\source\\repos\\GameOfLife\\GameOfLifeApp\\ClearButton.png"))
}
};
clearButton.Click += Clear;
Grid.SetRow(clearButton, rowCount);
Grid.SetColumn(clearButton, colCount - 1);
Grid.SetColumnSpan(clearButton, 1);
//Button Array richtig besetzen
int[,] startArray = new int[rowCount, colCount];
int rowLength = startArray.GetLength(0);
int colLength = startArray.GetLength(1);
startArray = Phasen.StartArray(startArray);
buttons = TranslatetoButtonArray(startArray, buttons, rowLength, colLength);
//Funktionsbutton MainGrid hinzufügen
MainGrid.Children.Add(startButton);
MainGrid.Children.Add(clearButton);
MainGrid.Children.Add(resetButton);
}
I have tried setting the heigth and width of the Grid to auto but that doesnt change anything. When i had a fix amount of buttons and declared the Grid aswell as the buttons in the .xaml file there was no such problem.
The reason I think you have this issue is because your code runs after measure and arrange have been done for your window.
I'm not sure why adding the grid doesn't force that again but I have a suggestion could make your problem go away.
Add
Window1.InvalidateVisual();
To the end of your code.
If that still doesn't work then try instead:
Dispatcher.CurrentDispatcher.InvokeAsync(new Action(() =>
{
Window1.InvalidateVisual();
}), DispatcherPriority.ContextIdle);
Still no joy then try not showing whatever this popup is. Maybe you're showing that as modal.
This approach of building everything out in code is inadvisable. Datatemplating is perfectly capable of working fast enough for basic games.

in C# windows universal app, I need to create a grid and divide it to number of columns and rows. I need to do that in the code, not in the XAML

Grid myGrid = new Grid();
myGrid.Width = 1000;
myGrid.Height = 1000;
myGrid.Visibility = Visibility.Visible;
for (int i = 0; i < 100; i++)
{
RowDefinition rd = new RowDefinition();
rd.Height = new GridLength(10);
ColumnDefinition cd = new ColumnDefinition();
cd.Width = new GridLength(10);
myGrid.RowDefinitions.Add(rd);
myGrid.ColumnDefinitions.Add(cd);
}
after I ran this code i don't see any change in the app.
Am i doing something wrong?
In XAML
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" x:Name="fatherGrid">
</Grid>
In C#
public MainPage()
{
this.InitializeComponent();
Grid myGrid = new Grid();
myGrid.Width = 1000;
myGrid.Height = 1000;
myGrid.Visibility = Visibility.Visible;
for (int i = 0; i < 10; i++)
{
RowDefinition rd = new RowDefinition();
//rd.Height = new GridLength(10);
ColumnDefinition cd = new ColumnDefinition();
//cd.Width = new GridLength(10);
myGrid.RowDefinitions.Add(rd);
myGrid.ColumnDefinitions.Add(cd);
TextBlock tb = new TextBlock();
tb.Text = "Hello";
Grid.SetRow(tb, i);
Grid.SetColumn(tb, i);
myGrid.Children.Add(tb);
}
this.fatherGrid.Children.Add(myGrid);
}
Result:

How to display only row border of a grid (not column) using Border()

I am using c# in silverlight application.
I have a grid with 1 row and 3 columns.
There are 2 things that i am not able to know how to do:
(1) I have to display the boundaries of just row (not columns, only rows). How to do that?
Currently i have a grids like this:
//The p in function call below is yhe object obtained on deserialixing xml.
private static Grid GenerateGrid(Parameters p)
{
Grid myGrid = new Grid();
myGrid.Width = 650;
myGrid.HorizontalAlignment = HorizontalAlignment.Left;
myGrid.VerticalAlignment = VerticalAlignment.Top;
myGrid.ShowGridLines = false;
ColumnDefinition colDef1 = new ColumnDefinition();
ColumnDefinition colDef2 = new ColumnDefinition();
ColumnDefinition colDef3 = new ColumnDefinition();
myGrid.ColumnDefinitions.Add(colDef1);
myGrid.ColumnDefinitions.Add(colDef2);
myGrid.ColumnDefinitions.Add(colDef3);
int totalRows = p.Parameter.Count() + p.Separator.Count();
for (int i = 0; i < totalRows; i++)
{
myGrid.RowDefinitions.Add(new RowDefinition());
}
return (myGrid);
}
Call to this function is :
XmlParameterClasses.Parameters parameter =
(XmlParameterClasses.Parameters)deserializer.Deserialize(reader);
Grid BigGrid = GenerateGrid(parameter);
My try to achieve is this: (I used Border to do this, see at the end of the function)
private static Grid GenerateComboBox(ViewModel.XmlParameterClasses.Parameter param, int LoopCount, Grid g)
{ //param is the object of the class Parameter
StackPanel sp1 = new StackPanel(); //These three stackpanels are inside the grid cell
StackPanel sp2 = new StackPanel();
StackPanel sp3 = new StackPanel();
ComboBox cb = new ComboBox();
TextBlock txtblk1 = new TextBlock();
TextBlock txtblkLabel = new TextBlock();
////////////////////////////////////
//Label Display
txtblkLabel.Text = param.Label;
txtblkLabel.VerticalAlignment = System.Windows.VerticalAlignment.Center;
txtblkLabel.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
txtblkLabel.TextAlignment = System.Windows.TextAlignment.Center;
txtblkLabel.FontWeight = FontWeights.Bold;
txtblkLabel.FontSize = 15;
txtblkLabel.FontStyle = FontStyles.Normal;
txtblkLabel.Padding = new Thickness(5, 10, 5, 10);
sp1.Orientation = Orientation.Horizontal;
sp1.Children.Add(txtblkLabel);
sp1.Width = 100;
sp1.Height = 50;
Grid.SetRow(sp1, LoopCount);
Grid.SetColumn(sp1, 0);
g.Children.Add(sp1);
foreach(var item in param.Component.Attributes.Items) {
cb.Items.Add(item);
}
cb.SelectionChanged += new SelectionChangedEventHandler(comboBox1_SelectionChanged);
cb.SelectedIndex = cb.Items.Count - 1;
//For text Display
txtblk1.Text = cb.SelectedValue.ToString() + " millions";
txtblk1.VerticalAlignment = System.Windows.VerticalAlignment.Center;
txtblk1.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
txtblk1.TextAlignment = System.Windows.TextAlignment.Center;
txtblk1.FontWeight = FontWeights.Bold;
txtblk1.FontSize = 15;
txtblk1.FontStyle = FontStyles.Normal;
txtblk1.Padding = new Thickness(5, 10, 5, 10);
sp2.Orientation = Orientation.Horizontal;
sp2.Children.Add(txtblk1);
Grid.SetColumn(sp2, 2);
Grid.SetRow(sp2, LoopCount);
g.Children.Add(sp2);
//For combo box display
cb.Width = 45;
cb.Height = 25;
sp3.Orientation = Orientation.Horizontal;
sp3.Children.Add(cb);
sp3.Width = 50;
sp3.Height = 50;
Grid.SetColumn(sp3, 1);
Grid.SetRow(sp3, LoopCount);
g.Children.Add(sp3);
////////////Here is the Border Display ////////////////////////////
Border rect = new Border();
rect.Width = g.Width;
rect.Height = g.Height;
rect.BorderThickness = new Thickness(5);
rect.BorderBrush = new SolidColorBrush(Colors.Black);
g.Children.Add(rect);
////////////////////////////////////////////////////////////////////
return (g);
}
but the output obtained is like this : (It just cover the border of first cell not the other two in that row, whereas i just want one border over one row(not over the column in that row, just row boundary))
Could some one please help me in achieving this step ? Is it possible to implement what i want trying to do?
Note: Please note that code has to be implemented using c# only , not xaml.
I have done it by creating a grid of 1 column and rows (instead of 3*3 cell it must be 1*3 (row*column)). Then creating Border in each row and then again creating grid with 1 row and 3 columns and then creating border of this small grid.
code is:
Border rect = new Border();
rect.Width = g.Width;
rect.Height = g.Height;
rect.BorderThickness = new Thickness(2);
rect.BorderBrush = new SolidColorBrush(Colors.Black);
Grid childGrid = new Grid();
ColumnDefinition colDef1 = new ColumnDefinition();
ColumnDefinition colDef2 = new ColumnDefinition();
ColumnDefinition colDef3 = new ColumnDefinition();
childGrid.ColumnDefinitions.Add(colDef1);
childGrid.ColumnDefinitions.Add(colDef2);
childGrid.ColumnDefinitions.Add(colDef3);
TextBlock txtblk3 = new TextBlock();
var border = new Border()
{
Background = new SolidColorBrush(Colors.LightGray)
};
border.Height = 14;
var border1 = new Border()
{
Background = new SolidColorBrush(Colors.White)
};
border1.Height = 14;
Grid.SetColumnSpan(border, 3);
Grid.SetRow(childGrid, LoopCount);
childGrid.Children.Add(border);
txtblk3.FontSize = 14;
txtblk3.FontWeight = FontWeights.Bold;
txtblk3.Text = param.Separator[SeparatorPosition];
Grid.SetColumn(border1, 1);
Grid.SetRow(border1,LoopCount);
border1.Child = txtblk3;
childGrid.Children.Add(border1);
g.Children.Add(childGrid);
return (g);
Where "g" has only 1 column and "LoopCount" numbers of row. And it worked for me.
You should override CellPainting event of DataGridView control as the code snippet below:
private void grid_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
e.AdvancedBorderStyle.Left = DataGridViewAdvancedCellBorderStyle.None;
e.AdvancedBorderStyle.Right = DataGridViewAdvancedCellBorderStyle.None;
}

c# Why stackpanel don't resize?

I am using silverlight and have to code in c# not xaml and i have a grid (myGrid) with 1 row and 1 column. This Grid further contain Border(rect) and this rect contains another grid (childGrid)inside having 1 row and 3 columns.
And this smallGrid further contains a stackpanel (sp) in second column whose size formed dynamically but smallGrid dont resize according to the growing size of stackpanel sp.
My code is like this:
Grid myGrid = new Grid();
myGrid.Width = 750;
myGrid.HorizontalAlignment = HorizontalAlignment.Left;
myGrid.VerticalAlignment = VerticalAlignment.Top;
myGrid.ShowGridLines = false;
ColumnDefinition colDef1 = new ColumnDefinition();
myGrid.ColumnDefinitions.Add(colDef1);
RowDefinition rowDef1 = new RowDefinition();
myGrid.RowDefinitions.Add(rowDef1);
Border rect = new Border();
rect.Width = g.Width;
rect.Height = g.Height;
rect.BorderThickness = new Thickness(2);
rect.BorderBrush = new SolidColorBrush(Colors.LightGray);
Grid childGrid = new Grid();
ColumnDefinition colDef1 = new ColumnDefinition();
ColumnDefinition colDef2 = new ColumnDefinition();
ColumnDefinition colDef3 = new ColumnDefinition();
childGrid.ColumnDefinitions.Add(colDef1);
childGrid.ColumnDefinitions.Add(colDef2);
childGrid.ColumnDefinitions.Add(colDef3);
int NumberOfRadioButton = 0;
StackPanel sp = new StackPanel();
foreach(var item in param.Component.Attributes.Items) {
NumberOfRadioButton++;
RadioButton rb = new RadioButton();
rb.GroupName = item;
rb.Content = item;
sp.Children.Add(rb);
}
Grid.SetRow(sp, 1);
Grid.SetColumn(sp, 1);
childGrid.Height = sp.Height;
childGrid.Children.Add(sp);
TextBlock txtblk1ShowStatus = new TextBlock();
TextBlock txtblkLabel = new TextBlock();
txtblkLabel.Text = param.Label;
Grid.SetColumn(txtblkLabel, 0);
Grid.SetRow(txtblkLabel, 1);
childGrid.Children.Add(txtblkLabel);
txtblk1ShowStatus.Text = param.Name;
Grid.SetColumn(txtblk1ShowStatus, 2);
Grid.SetRow(txtblk1ShowStatus, 1);
childGrid.Children.Add(txtblk1ShowStatus);
rect.Child = childGrid;
Grid.SetRow(rect, 1);
myGrid.Children.Add(rect);
The size of myGrid and childGrid must grow dynamically according to the increasing stackpanel size on dynamically created radio buttons? Because all the radio buttons are not displayed in column 2 please see the last radiobutton in snapshot belowis not displayed because of border crossing ("Very High", just after "High"):
EDIT: I even tried these 3 steps but it still the same:
(1)Remove rect.Width = g.Width; rect.Height = g.Height; childGrid.Height = sp.Height;
(2)add rowdef.Height= new GridLength(1, GridUnitType.Auto);(because i am expanding vertically)
(3) add sp.Orientation = Orientation.Vertical;
Edit2 : myGrid is liek this:
Grid myGrid = new Grid();
myGrid.Width = 750;
myGrid.HorizontalAlignment = HorizontalAlignment.Left;
myGrid.VerticalAlignment = VerticalAlignment.Top;
myGrid.ShowGridLines = false;
ColumnDefinition colDef1 = new ColumnDefinition();
myGrid.ColumnDefinitions.Add(colDef1);
int totalRows = p.Parameter.Count() + p.Separator.Count();
for (int i = 0; i < totalRows; i++)
{
myGrid.RowDefinitions.Add(new RowDefinition());
}
EDIT3: (my changed code after bit's suggestion)
Border rect = new Border();
// rect.Width = g.Width;
// rect.Height = g.Height;
rect.BorderThickness = new Thickness(2);
rect.BorderBrush = new SolidColorBrush(Colors.LightGray);
Grid childGrid = new Grid();
ColumnDefinition colDef1 = new ColumnDefinition();
ColumnDefinition colDef2 = new ColumnDefinition();
ColumnDefinition colDef3 = new ColumnDefinition();
RowDefinition rowdef = new RowDefinition();
childGrid.ColumnDefinitions.Add(colDef1);
childGrid.ColumnDefinitions.Add(colDef2);
childGrid.ColumnDefinitions.Add(colDef3);
childGrid.RowDefinitions.Add(rowdef);
rowdef.Height= new GridLength(1, GridUnitType.Auto);
int NumberOfRadioButton =0;
StackPanel sp = new StackPanel();
sp.Orientation = Orientation.Vertical;
foreach (var item in param.Component.Attributes.Items)
{
NumberOfRadioButton++;
RadioButton rb = new RadioButton();
rb.GroupName = item;
rb.Content = item;
sp.Children.Add(rb);
}
Grid.SetRow(sp, LoopCount);
Grid.SetColumn(sp, 1);
childGrid.ShowGridLines = true;
// rect.Height = double.NaN;
childGrid.Children.Add(sp);
TextBlock txtblk1ShowStatus = new TextBlock();
TextBlock txtblkLabel = new TextBlock();
txtblkLabel.VerticalAlignment = System.Windows.VerticalAlignment.Center;
txtblkLabel.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
txtblkLabel.TextAlignment = System.Windows.TextAlignment.Center;
txtblkLabel.FontWeight = FontWeights.Bold;
txtblkLabel.FontSize = 15;
txtblkLabel.FontStyle = FontStyles.Normal;
txtblkLabel.Padding = new Thickness(5, 10, 5, 10);
txtblkLabel.Text = param.Label;
Grid.SetColumn(txtblkLabel, 0);
Grid.SetRow(txtblkLabel, LoopCount);
childGrid.Children.Add(txtblkLabel);
txtblk1ShowStatus.VerticalAlignment = System.Windows.VerticalAlignment.Center;
txtblk1ShowStatus.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
txtblk1ShowStatus.TextAlignment = System.Windows.TextAlignment.Center;
txtblk1ShowStatus.FontWeight = FontWeights.Bold;
txtblk1ShowStatus.FontSize = 15;
txtblk1ShowStatus.FontStyle = FontStyles.Normal;
txtblk1ShowStatus.Padding = new Thickness(5, 10, 5, 10);
txtblk1ShowStatus.Text = param.Name;
Grid.SetColumn(txtblk1ShowStatus, 2);
Grid.SetRow(txtblk1ShowStatus, LoopCount);
childGrid.Children.Add(txtblk1ShowStatus);
rect.Child = childGrid;
Grid.SetRow(rect, LoopCount);
myGrid.Children.Add(rect);
There a bunch of things you need to consider here..
In case you want a Horizontally expanding smallGrid, set the
sp.Orientation=Orientation.Horizontal
Then set the widths of the columns in smallGrid to Auto.
colDef.Width = new GridLength(1, GridUnitType.Auto); // Auto
You don't really need the myGrid, directly using the Border should suffice.
Get rid of assigning the Height s eg:
rect.Width = g.Width;
rect.Height = g.Height;
childGrid.Height = sp.Height;
Else, if you wanted a vertically expanding smallGrid, make sure the parent of the Border rect (or myGrid, in case you decide to keep it), allows for expansion Height (mostly check that height is not hard coded to a fix number or something)
Also, set the
smallGrid.ShowGridLines = true;
to give you a better idea in regards to what is actually taking up the space.
Lastly, do this too
int totalRows = p.Parameter.Count() + p.Separator.Count();
for (int i = 0; i < totalRows; i++)
{
var rowDef=new RowDefinition();
rowdef.Height = new GridLength(1, GridUnitType.Auto);
myGrid.RowDefinitions.Add(rowDef);
}

only the first listpicker opens windows phone

I am using Windows Phone Toolkit - Nov 2011 (7.1 SDK) and I want to display multiple listpickers each
within a grid and each grid in a listbox. The problem is, only the first listpicker pops open and the rest don't. secondly how do I customize the full mode page of the listpicker to be displayed exclusively in Landscape orientation and set the page's "shell:SystemTray.IsVisible" to false.
Sorry I couldn't post a screenshot. error "Earn more than 10 reputation to post images".
Thanks
public MainPage()
{
InitializeComponent();
for (int g = 0; g < 10; g++)
{
// Define the margins template dor elements
Thickness elemThick = new Thickness();
// Create the Grid that will hold all the elements of a single entry
Grid entryGrd = new Grid();
entryGrd.VerticalAlignment = VerticalAlignment.Top;
entryGrd.HorizontalAlignment = HorizontalAlignment.Left;
elemThick.Left = 0;
elemThick.Bottom = 0;
elemThick.Right = 0;
elemThick.Top = 0;
entryGrd.Margin = elemThick;
entryGrd.Tag = lstbxPH.Items.Count;
// Setup the backgound value of the letBoder element
LinearGradientBrush elemLGB = new LinearGradientBrush();
elemLGB.EndPoint = new Point(0.5, 1);
elemLGB.StartPoint = new Point(0.5, 0);
GradientStop AquamarineGS = new GradientStop();
AquamarineGS.Color = Color.FromArgb(255, 127, 255, 212);
AquamarineGS.Offset = 0;
GradientStop greenLikeGS = new GradientStop();
greenLikeGS.Color = Color.FromArgb(255, 101, 250, 193);
greenLikeGS.Offset = 0.988;
elemLGB.GradientStops.Add(AquamarineGS);
elemLGB.GradientStops.Add(greenLikeGS);
// Draw the letter
for (int x = 0; x < 9; x++)
{
Border letBoder = new Border();
letBoder.Width = 53;
letBoder.Height = 51;
letBoder.VerticalAlignment = VerticalAlignment.Top;
letBoder.HorizontalAlignment = HorizontalAlignment.Left;
elemThick.Left = x * 60 + 71;
elemThick.Top = lstbxPH.Items.Count * 1 + 20;
letBoder.Margin = elemThick;
letBoder.Background = elemLGB;
// The Texblock
TextBlock let = new TextBlock();
let.VerticalAlignment = VerticalAlignment.Center;
let.HorizontalAlignment = HorizontalAlignment.Center;
let.FontSize = 25;
let.FontWeight = FontWeights.Bold;
let.Foreground = new SolidColorBrush(Color.FromArgb(200, 255, 255, 255));
let.Text = x.ToString();
let.Tag = x;
letBoder.Child = let;
entryGrd.Children.Add(letBoder);
}
// Draw the List picker element for the draw types
ListPicker DType = new ListPicker();
DType.Width = 48;
DType.VerticalAlignment = VerticalAlignment.Top;
DType.HorizontalAlignment = HorizontalAlignment.Left;
DType.Background = new SolidColorBrush(Color.FromArgb(200, 255, 255, 255));
DType.BorderBrush = new SolidColorBrush(Color.FromArgb(200, 255, 255, 255));
elemThick.Left = 17;
elemThick.Top = lstbxPH.Items.Count * 1 + 10;
DType.Margin = elemThick;
DType.FontSize = 18;
ListPickerItem item1 = new ListPickerItem() { Content = "A" };
ListPickerItem item2 = new ListPickerItem() { Content = "B" };
ListPickerItem item3 = new ListPickerItem() { Content = "C" };
DType.Items.Add(item1);
DType.Items.Add(item2);
DType.Items.Add(item3);
entryGrd.Children.Add(DType);
if (lstbxPH.Items.Count != 0)
{
// The delete button and related image
Button btnDel = new Button();
btnDel.Height = 65;
btnDel.Width = 60;
btnDel.Tag = lstbxPH.Items.Count;
btnDel.VerticalAlignment = VerticalAlignment.Top;
btnDel.HorizontalAlignment = HorizontalAlignment.Left;
btnDel.VerticalContentAlignment = VerticalAlignment.Top;
btnDel.HorizontalContentAlignment = HorizontalAlignment.Left;
elemThick.Left = 600;
elemThick.Top = lstbxPH.Items.Count + 13;
btnDel.Margin = elemThick;
elemThick.Left = 0;
elemThick.Bottom = 0;
elemThick.Right = 0;
elemThick.Top = 0;
btnDel.Name = "btnDel";
btnDel.Padding = elemThick;
btnDel.BorderBrush = new SolidColorBrush(Color.FromArgb(0, 0, 0, 0));
Image imgDel = new Image();
imgDel.Source = new BitmapImage(new Uri("/appbar.delete.rest.png", UriKind.RelativeOrAbsolute));
imgDel.VerticalAlignment = VerticalAlignment.Top;
imgDel.HorizontalAlignment = HorizontalAlignment.Left;
imgDel.Height = 35;
imgDel.Width = 30;
elemThick.Left = 0;
elemThick.Bottom = 0;
elemThick.Right = 0;
elemThick.Top = 0;
imgDel.Margin = elemThick;
imgDel.Stretch = Stretch.UniformToFill;
btnDel.Content = imgDel;
entryGrd.Children.Add(btnDel);
}
// Add the grid with to the list box
this.lstbxPH.Items.Add(entryGrd);
}
}
This issue was actually solved by the Windows Phone Toolkit - Nov 2011 (7.1 SDK).
Here is how I worked it out.
Download the November release of the toolkit http://silverlight.codeplex.com/releases/view/75888
if you had a previous release of the toolkit installed, please uninstall it and unreferenced the tool kit on you project
Install the November release of the toolkit
Reference the toolkit on your project, debug and boom! it woks.

Categories

Resources