Loop in property in constructor - c#

If you have a look at my code below. Is there ANY way to write some kind of loop instead of having repeat Row and ColumnDefinitions?
var grid = new Grid
{
RowSpacing = 12,
ColumnSpacing = 12,
VerticalOptions = LayoutOptions.FillAndExpand,
RowDefinitions =
{
new RowDefinition { Height = new GridLength(1, GridUnitType.Star) },
new RowDefinition { Height = new GridLength(1, GridUnitType.Star) },
new RowDefinition { Height = new GridLength(1, GridUnitType.Star) },
},
ColumnDefinitions =
{
new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) },
new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) },
}
};

You can use loops to create an array of rows and an array of columns beforehand and assign these to the RowDefinitions and ColumnDefinitions properties.
I should have thought you'd need to call RowDefinitions.Add() and ColumnDefinitions.Add() in a loop to do so, though.

No, this is not possible because the only way this would work is if you could assign a completely new value to the RowDefinitions property, which you can't:
public RowDefinitionCollection RowDefinitions { get; }
^^^^
The syntax as shown in your question is just a handy way of calling .Add on the object in that property, so there is no way for you to inline in that syntax do this. Your code is just "short" for this:
var temp = new Grid();
temp.RowSpacing = 12;
temp.ColumnSpacing = 12;
temp.VerticalOptions = LayoutOptions.FillAndExpand;
temp.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
temp.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
temp.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
... same for columns
Specifically, your code is not doing this:
temp.RowDefinitions = ...
^
You would probably want code like this:
var grid = new Grid()
{
RowSpacing = 12,
ColumnSpacing = 12,
VerticalOptions = LayoutOptions.FillAndExpand,
RowDefinitions = Enumerable.Range(0, 100).Select(_ =>
new RowDefinition { Height = new GridLength(1, GridUnitType.Star) }),
ColumnDefinitions = Enumerable.Range(.....
But you cannot do this as this would require that RowDefinitions and ColumnDefinitions was writable.
The closest thing is like this:
var temp = new Grid
{
RowSpacing = 12,
ColumnSpacing = 12,
VerticalOptions = LayoutOptions.FillAndExpand,
};
for (int index = 0; index < rowCount; index++)
temp.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
... same for columns
var grid = temp;

RowDefinitions is RowDefinitionCollection. RowDefinitionCollection is internal which you cannot create outside Grid.

Related

Grid column to occupy entire screen

I'm doing a little project in Xamarin forms and I want the itens in the columns of my grid to space eachother so that they occupy the entire screen. I tried messing with the vertical layout options and it doesn't work (the best so far is using Fill/Fill and expand, but they simply stretch the hell out of the third row so it fills the gap). I'm using VStudio 2019 if that matters.
Also, if u can explain what u did instead of simply writing a code that works, I would be grateful.
public MainPage()
{
this.Padding = new Thickness(20);
var deviceWidht = DeviceDisplay.MainDisplayInfo.Width;
double buttonWidthandHeight = deviceWidht / 9;
int buttonCornerRadius = Convert.ToInt32(deviceWidht / 20);
Grid panel1 = new Grid();
panel1.HorizontalOptions = LayoutOptions.CenterAndExpand;
panel1.VerticalOptions = LayoutOptions.CenterAndExpand;
panel1.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto });
panel1.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto });
panel1.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto });
panel1.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
panel1.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
panel1.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
Button r1c1 = SetButton(1, 1);
Button r1c2 = SetButton(1, 2);
Button r1c3 = SetButton(1, 3);
Button r2c1 = SetButton(2, 1);
Button r2c2 = SetButton(2, 2);
Button r2c3 = SetButton(2, 3);
Button r3c1 = SetButton(3, 1);
Button r3c2 = SetButton(3, 2);
Button r3c3 = SetButton(3, 3);
Button SetButton (int row, int column)
{
Button createdButton = CreateButton(row, column);
Grid.SetRow(createdButton, row);
Grid.SetColumn(createdButton, column);
panel1.Children.Add(createdButton);
return createdButton;
}
Button CreateButton(int row, int column)
{
return new Button
{
Text = "Row " + row + " Column " + column,
WidthRequest = buttonWidthandHeight,
HeightRequest = buttonWidthandHeight,
CornerRadius = buttonCornerRadius
};
}
this.Content = panel1;
}
First,you could change the ColumnDefinitions and RowDefinitions like below:
panel1.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1,GridUnitType.Star) });
panel1.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
panel1.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
panel1.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
panel1.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
panel1.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
The three ColumnDefinition objects both set the Width to 1, meaning that the width of the screen is divided equally beneath the three columns (the same goes for rows).
Second,when you add the button,you should start from the position 0 ,not 1.
Button r1c1 = SetButton(0, 0);
Button r1c2 = SetButton(0, 1);
Button r1c3 = SetButton(0, 2);
Button r2c1 = SetButton(1, 0);
Button r2c2 = SetButton(1, 1);
Button r2c3 = SetButton(1, 2);
Button r3c1 = SetButton(2, 0);
Button r3c2 = SetButton(2, 1);
Button r3c3 = SetButton(2, 2);
Finally the codes you could check :
this.Padding = new Thickness(20);
var deviceWidht = DeviceDisplay.MainDisplayInfo.Width;
double buttonWidthandHeight = deviceWidht / 9;
int buttonCornerRadius = Convert.ToInt32(deviceWidht / 20);
Grid panel1 = new Grid();
panel1.HorizontalOptions = LayoutOptions.FillAndExpand;
panel1.VerticalOptions = LayoutOptions.FillAndExpand;
panel1.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1,GridUnitType.Star) });
panel1.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
panel1.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
panel1.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
panel1.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
panel1.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
Button r1c1 = SetButton(0, 0);
Button r1c2 = SetButton(0, 1);
Button r1c3 = SetButton(0, 2);
Button r2c1 = SetButton(1, 0);
Button r2c2 = SetButton(1, 1);
Button r2c3 = SetButton(1, 2);
Button r3c1 = SetButton(2, 0);
Button r3c2 = SetButton(2, 1);
Button r3c3 = SetButton(2, 2);
Button SetButton(int row, int column)
{
Button createdButton = CreateButton(row, column);
Grid.SetRow(createdButton, row);
Grid.SetColumn(createdButton, column);
panel1.Children.Add(createdButton);
return createdButton;
}
Button CreateButton(int row, int column)
{
return new Button
{
Text = "Row " + row + " Column " + column,
WidthRequest = buttonWidthandHeight,
HeightRequest = buttonWidthandHeight,
CornerRadius = buttonCornerRadius
};
}
this.Content = panel1;

TextBlock doesn't want to adjust grid columns

I'm preparing control to present some data. It is built in the following way:
-> ScrollViewer
--> StackPanel
---> Border
----> Grid
---> Border
----> Grid
...
---> Border
----> Grid
And there is my code for each item
public class PresenterItem : Border
{
// Variables
private Submission submission;
private int index;
private Grid grid = new Grid();
// Constructor
public PresenterItem(int i, Submission subm)
{
index = i;
submission = subm;
Child = grid;
Background = Global.GET_BRUSH("ItemBackground");
CornerRadius = new CornerRadius(5);
Margin = new Thickness(0, 0, 0, 10);
Padding = new Thickness(5);
grid.ShowGridLines = true;
grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(40, GridUnitType.Pixel) });
grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(2, GridUnitType.Star) });
grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(2, GridUnitType.Star) });
grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(40, GridUnitType.Pixel) });
grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(30, GridUnitType.Pixel) });
grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) });
InsertContent();
}
private Label CreateLabel(int row, int column, string content, int columnSpan = 1)
{
Label newLabel = new Label();
newLabel.Content = content;
Grid.SetRow(newLabel, row);
Grid.SetColumn(newLabel, column);
Grid.SetColumnSpan(newLabel, columnSpan);
grid.Children.Add(newLabel);
return newLabel;
}
private TextBlock CreateTextBlock(int row, int column, int columnSpan = 1)
{
TextBlock newTextBlock = new TextBlock();
newTextBlock.Foreground = Brushes.Silver;
Grid.SetRow(newTextBlock, row);
Grid.SetColumn(newTextBlock, column);
Grid.SetColumnSpan(newTextBlock, columnSpan);
grid.Children.Add(newTextBlock);
return newTextBlock;
}
private void InsertContent()
{
// Number
Label number = CreateLabel(0, 0, $"#{index + 1}");
number.HorizontalAlignment = HorizontalAlignment.Center;
number.VerticalAlignment = VerticalAlignment.Center;
number.FontSize = 17;
// Header
Label header = CreateLabel(0, 1, $"{submission.Name} ({submission.Rank})");
header.Foreground = Global.GET_BRUSH("HeaderForeground");
header.HorizontalAlignment = HorizontalAlignment.Left;
header.VerticalAlignment = VerticalAlignment.Center;
header.FontSize = 17;
// Timestamp
TextBlock timestamp = CreateTextBlock(0, 2);
timestamp.Inlines.Add(new Run("Timestamp"));
timestamp.Inlines.Add(new Run($"{submission.Timestamp}") { Foreground = Global.GET_BRUSH("HeaderForeground") });
timestamp.HorizontalAlignment = HorizontalAlignment.Right;
timestamp.VerticalAlignment = VerticalAlignment.Center;
timestamp.FontSize = 13.5;
// Range
TextBlock range = CreateTextBlock(1, 1);
range.Inlines.Add(new Run("Some text "));
range.Inlines.Add(new Run($"{submission.Range.ToStringWithDayNames()}") { Foreground = Global.GET_BRUSH("HeaderForeground") });
range.HorizontalAlignment = HorizontalAlignment.Left;
range.VerticalAlignment = VerticalAlignment.Center;
range.Margin = new Thickness(5, 0, 0, 0);
range.FontSize = 13.5;
// Conflict
Label conflict = CreateLabel(1, 2, "Nie wykryto konfliktu");
conflict.Foreground = Global.GET_BRUSH("GreenForeground");
conflict.HorizontalAlignment = HorizontalAlignment.Right;
conflict.VerticalAlignment = VerticalAlignment.Center;
conflict.FontSize = 13.5;
// Content
TextBlock content = CreateTextBlock(2, 1, 2);
content.Inlines.Add(new Run($"{submission.Content}"));
cotent.HorizontalAlignment = HorizontalAlignment.Left;
content.VerticalAlignment = VerticalAlignment.Top;
content.Margin = new Thickness(5, 0, 0, 0);
content.TextWrapping = TextWrapping.WrapWithOverflow;
content.FontSize = 13.5;
}
How it looks like
It perfectly works but when I added last TextBlock whole control is stretching to the right. In designer the same way of creating elements works, but in code no. What am I doing wrong?
I would like to achieve this effect with column 1 and 2 with the same width everywhere.
What I want
You have to set the dependency property Grid.IsSharedSizeScope to true in the StackPanel and then set the Property SharedSizeGroup for every ColumnDefinition to a string that definies the group with same size.
I dealt with this problem on my own. If someone will be having the same problem like me, I will write how to repair it.
In the place where my app initializes ScrollViewer I set property HorizontalScrollBarVisibility to Hidden. After setting this property to Disabled everything starts to work correctly.
There is something about this:
https://crmdev.wordpress.com/2010/01/16/how-to-deal-with-stubborn-silverlight-a-k-a-stubborn-me/

Pass Parameters to Tap Gesture Xamarin Forms

I am trying to pass parameters from a page to another page. These passed parameters will be used to select from an SQL table.
The page is built as follows: (The code behind)
private MainRoutePageViewModel mainroutepageviewmodel;
private List<RouteInfo> routeinfo;
Constructor:
public MainRoutePageViewDetail(MessagDatabase database)
{
InitializeComponent();
BindingContext = mainroutepageviewmodel = new MainRoutePageViewModel(database,Navigation);
//_listOfProperties = mainroutepageviewmodel.GetLabelInfo();
ScrollView scrollview = new ScrollView();
StackLayout mainstack = new StackLayout();
mainstack.Spacing = 0;
mainstack.Padding = 0;
//mainstack.HeightRequest = 2000;
routeinfo = mainroutepageviewmodel.GetLabelInfo();
string _routePlacer = "";
foreach (var i in routeinfo)
{
mainstack.Children.Add(NewRouteName(i.RouteName));
mainstack.Children.Add(BuildNewRoute(i.RouteStops,i));
_routePlacer = i.RouteName;
}
scrollview.Content = mainstack;
Content = scrollview;
}// end of constructor
The BuildNewRoute method:
public StackLayout BuildNewRoute(List<string> location, RouteInfo routeinfo)
{
StackLayout stackLayout = new StackLayout();
//stackLayout.HeightRequest = 1000;
foreach (var i in location) {
StackLayout stackLayout2 = new StackLayout();
stackLayout2.HeightRequest = 200;
Grid grid = new Grid();
grid.ColumnSpacing = 0;
grid.RowSpacing = 0;
grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(15, GridUnitType.Star) });
grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(20, GridUnitType.Star) });
grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(20, GridUnitType.Star) });
grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(20, GridUnitType.Star) });
grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(20, GridUnitType.Star) });
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(55, GridUnitType.Star) });
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(18, GridUnitType.Star) });
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(18, GridUnitType.Star) });
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(18, GridUnitType.Star) });
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(40, GridUnitType.Star) });
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(30, GridUnitType.Star) });
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(10, GridUnitType.Star) });
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(15, GridUnitType.Star) });
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(5, GridUnitType.Star) });
TapGestureRecognizer ArrowtapGesture = new TapGestureRecognizer();
ArrowtapGesture.Tapped += ArrowtapGesture_Tapped;
// Arrow icon
Image arrowimage = new Image();
arrowimage.Source = "Resources/arrow.png";
arrowimage.VerticalOptions = LayoutOptions.Center;
arrowimage.HorizontalOptions = LayoutOptions.Center;
arrowimage.GestureRecognizers.Add(ArrowtapGesture);
grid.Children.Add(arrowimage,7,6);
// total weight labels
Label weightlabel = new Label();
weightlabel.Text = "Total Weight [kg]: ";
grid.Children.Add(weightlabel,1,5,3,4);
// total items labels
Label itemsLabel = new Label();
itemsLabel.Text = "Total Items: ";
grid.Children.Add(itemsLabel, 1, 5, 4, 5);
// underline labels
Label firstunderline = new Label();
Label secondunderline = new Label();
firstunderline.BackgroundColor = Color.Black;
secondunderline.BackgroundColor = Color.Black;
grid.Children.Add(firstunderline,0,9,0,1);
grid.Children.Add(secondunderline,0,9,2,3);
// address label
Label labelLocation = new Label();
labelLocation.Text = i;
grid.Children.Add(labelLocation, 0, 3);
//sequence label
Label sequencelable = new Label();
sequencelable.Text = "Sequence: ";
sequencelable.VerticalTextAlignment = TextAlignment.Center;
grid.Children.Add(sequencelable, 0, 1);
// slot label
Label slotlabel = new Label();
slotlabel.Text = "ETA/Slot: ";
slotlabel.VerticalTextAlignment = TextAlignment.Center;
grid.Children.Add(slotlabel,1,4,1,2);
// time label
Label timelabel = new Label();
timelabel.Text = "Time: ";
timelabel.VerticalTextAlignment = TextAlignment.Center;
grid.Children.Add(timelabel, 4, 5,1,2);
// Status label
Label statuslabel = new Label();
statuslabel.Text = "Status: ";
statuslabel.VerticalTextAlignment = TextAlignment.Center;
grid.Children.Add(statuslabel, 5, 6,1,2);
//start button
Button startbutton = new Button();
startbutton.Text = "Pending";
startbutton.BackgroundColor = Color.Gray;
grid.Children.Add(startbutton,5,8,4,6);
// Phone book image
Image bookImage = new Image();
//bookImage.BackgroundColor = Color.White;
bookImage.Source = "Resources/phoneWithBook.png";
bookImage.VerticalOptions = LayoutOptions.Center;
bookImage.HorizontalOptions = LayoutOptions.Center;
grid.Children.Add(bookImage,1,2,6,7);
//Globe image
Image GlobeImage = new Image();
// GlobeImage.BackgroundColor = Color.White;
GlobeImage.Source = "Resources/globe.png";
GlobeImage.VerticalOptions = LayoutOptions.Center;
GlobeImage.HorizontalOptions = LayoutOptions.Center;
grid.Children.Add(GlobeImage, 2, 3, 6, 7);
stackLayout2.Children.Add(grid);
stackLayout.Children.Add(stackLayout2);
}
return stackLayout;
}
As you can probably see it loops through a list of collected data and adds grids and labels to a main StackLayout.
This is not the issue the page building works fine.
What you can see is the arrow icon image that has a tap gesture attached to it. This tap gesture uses the view model to open the next page.
The tap gesture:
private async void ArrowtapGesture_Tapped(object sender, EventArgs e)
{
await mainroutepageviewmodel.OpenStopDetail();
}
And the OpenStopDetail method:
public async Task OpenStopDetail()
{
await Navigation.PushAsync(new StopDetailPageView());
}
I want to know how to pass parameters from the tap event through to the StopDetailView page.
Specifically the text from the sequence label.
Some things I have tried, have been using the casting in the tap event but this seems to be bound to the item that is selected. In other words its giving me access to image properties. Which is no good for my situation.
I cant seem to find a way to access each label property individually to pass as a parameter. Sorry if this isn't clear, it was tough to explain. Let me know if more detail is needed.
You should be able to use the CommandParameter of the TapGestureRecognizer.
In XAML:-
<TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped"
CommandParameter="Value"/>
e.Parameter will be whatever you set in the CommandParameter.
private async void TapGestureRecognizer_Tapped(object sender, TappedEventArgs e)
Edit:
It has been pointed out that the above is not the right signature, the param, needs to be EventArgs and cast to TappedEventArgs.
private async void TapGestureRecognizer_Tapped(object sender, EventArgs e)
{
var param = ((TappedEventArgs)e).Parameter;
}
The sender of the Tapped event will be the control the gesture recognizer is attached to - in your case, an Image. So you can add your data to one of the Image's properties in order to access it from your event handler.
// assign parameter to ClassId (must be a string)
arrowimage.ClassId = "blah";
arrowimage.GestureRecognizers.Add(ArrowtapGesture);
private async void ArrowtapGesture_Tapped(object sender, EventArgs e)
{
// retrieve parameter from sender's ClassId
var parm = ((Image)sender).ClassId;
await mainroutepageviewmodel.OpenStopDetail();
}
<Image.GestureRecognizers>
<TapGestureRecognizer Tapped="Share_Tapped" CommandParameter="{Binding .}"/>
</Image.GestureRecognizers>
enter code here
private void Share_Tapped(object sender, TappedEventArgs e)
{
var contact = (e.Parameter) as DetailList;
}
Result

WPF grid size changed event firing only when increasing and not when decreasing

I have a grid with rowdefinitions
RowDefinition rd0 = new RowDefinition();
rd0.Height = new GridLength(1, GridUnitType.Auto);
RowDefinition rd1 = new RowDefinition();
rd1.Height = new GridLength(1, GridUnitType.Star);
RowDefinition rd2 = new RowDefinition();
rd2.Height = new GridLength(1, GridUnitType.Auto);
grdMain.RowDefinitions.Add(rd0);
grdMain.GetGrdPlugins().RowDefinitions.Add(rd1);
grdMain.GetGrdPlugins().RowDefinitions.Add(rd2);
now in the first row I add a textblock
var tbxTitle = new TextBlock(){};
Grid.SetRow(tbxTitle, 0);
grdMain.Children.Add(tbxTitle);
in the third a stack panel of buttons
StackPanel spButtons = new StackPanel() { Orientation = Orientation.Horizontal, HorizontalAlignment = HorizontalAlignment.Center, };
grdMain.Children.Add(spButtons);
Grid.SetRow(spButtons, 2);
...
in the second a stackpanel.
Now I want an event to be called all the times the grid changes in size:
var spMatrix_Volatile = new StackPanel() { HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Stretch, Background = Brushes.RosyBrown};
spMatrix_Volatile.SizeChanged += (sender, args) =>
{
Console.Beep();
double dHeight = spMatrix_Volatile.ActualHeight;
CreateCellMatrix(out strResult, ref spMatrix_Volatile, false, dHeight);
};
Grid.SetRow(spMatrix_Volatile, 1);
grdMain.Children.Add(spMatrix_Volatile);
now the peculiar thing is that the size changed event is called all the times the height of the grid is increased but never when decreased.
Thank you
That happens to me as well using your code.
I have therefore put the stackpanel in a grid and that didn't happen anymore.
That is the event is fired both when increasing and decreasing.

How to color a particular row of a grid in c# using silverlight

I am working on c# silverlight. I have to color(Green) the particular column which is created using c#.
I have grid with 6 rows and 3 columns like this:
Grid myGrid = new Grid();
myGrid.Width = 350;
myGrid.Height = 280;
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);
RowDefinition rowDef1 = new RowDefinition();
RowDefinition rowDef2 = new RowDefinition();
RowDefinition rowDef3 = new RowDefinition();
RowDefinition rowDef4 = new RowDefinition();
RowDefinition rowDef5 = new RowDefinition();
RowDefinition rowDef6 = new RowDefinition();
myGrid.RowDefinitions.Add(rowDef1);
myGrid.RowDefinitions.Add(rowDef2);
myGrid.RowDefinitions.Add(rowDef3);
myGrid.RowDefinitions.Add(rowDef4);
myGrid.RowDefinitions.Add(rowDef5);
myGrid.RowDefinitions.Add(rowDef6);
Now if i have to color second full row(i mean in 3 columns in this row as well) of this grid then how i will do this ?
var greenBackgroundBorder = new Border(){
Background=new SolidColorBrush(Colors.Green)};
myGrid.Children.Add(greenBackgroundBorder);
// stay always behind other elements
Canvas.SetZOder(greenBackgroundBorder, -100);
//entire second row
Grid.SetColumnSpan(greenBackgroundBorder,3);
Grid.SetRow(greenBackgroundBorder, 1 );

Categories