How to create a table with multiple columns in ios - c#

I am a beginner in xamarin ios, I want to create a table with 3 columns and multiple rows, and the no of row is depends on the data coming from the API .How can I do this...
Please help me...
I want to create the table like this...

You can simulate drawing the chart by following steps.
Custom TableHeaderView with three black rectangle ,just need add three label with its black border.
Do the same work as step1 with TableViewCell, and then place a textfield in first rectangle, place a button and a label in second rectangle,place two button in third rectangle.
Sample Code
TableHeaderView
public class headerView : UIView
{
public headerView()
{
this.Frame = new CGRect(0, 0, UIScreen.MainScreen.Bounds.Width, 50);
nfloat width = UIScreen.MainScreen.Bounds.Width / 3;
UILabel title = new UILabel();
title.Text = "Title";
title.TextColor = UIColor.Black;
title.TextAlignment = UITextAlignment.Center;
title.Font = UIFont.SystemFontOfSize(20);
title.Layer.BorderColor = UIColor.Black.CGColor;
title.Layer.BorderWidth = 1;
title.Frame = new CGRect(0, 0, width, 50);
this.Add(title);
UILabel File = new UILabel();
File.Text = "File";
File.TextColor = UIColor.Black;
File.TextAlignment = UITextAlignment.Center;
File.Font = UIFont.SystemFontOfSize(20);
File.Layer.BorderColor = UIColor.Black.CGColor;
File.Layer.BorderWidth = 1;
File.Frame = new CGRect(width, 0, UIScreen.MainScreen.Bounds.Width / 3, 50);
this.Add(File);
UILabel Option = new UILabel();
Option.Text = "Option";
Option.TextColor = UIColor.Black;
Option.TextAlignment = UITextAlignment.Center;
Option.Font = UIFont.SystemFontOfSize(20);
Option.Layer.BorderColor = UIColor.Black.CGColor;
Option.Layer.BorderWidth = 1;
Option.Frame = new CGRect(width * 2, 0, UIScreen.MainScreen.Bounds.Width / 3, 50);
this.Add(Option);
}
}
table.TableHeaderView = new headerView();
TableViewCell
public CustomVegeCell (NSString cellId) : base (UITableViewCellStyle.Default, cellId)
{
SelectionStyle = UITableViewCellSelectionStyle.Gray;
nfloat width = UIScreen.MainScreen.Bounds.Width / 3;
UIView headingLabel = new UIView();
headingLabel.Layer.BorderColor = UIColor.Black.CGColor;
headingLabel.Layer.BorderWidth = 1;
headingLabel.Frame = new CGRect(0, 0, width, 100);
UIView subheadingLabel = new UIView();
subheadingLabel.Layer.BorderColor = UIColor.Black.CGColor;
subheadingLabel.Layer.BorderWidth = 1;
subheadingLabel.Frame = new CGRect(width, 0, width, 100);
UIView imageView = new UIView();
imageView.Layer.BorderColor = UIColor.Black.CGColor;
imageView.Layer.BorderWidth = 1;
imageView.Frame = new CGRect(width *2, 0, width, 100);
ContentView.Add (headingLabel);
ContentView.Add (subheadingLabel);
ContentView.Add (imageView);
///add additional control here
}

Related

Change the border color in individual cells in Spire.PDF grid C#

I am using Spire.PDF library to create a PDF in server side and return it to mobile side. Everything is working great. But I wanna access to each cell's border in the grid. I wanna change the color of the border.
https://www.e-iceblue.com/Tutorials/Spire.PDF/Spire.PDF-Program-Guide/Table/How-to-Change-the-Color-of-Grid-Border-in-PDF-in-C.html
Current code..
public HttpResponseMessage GET_MOTOR_RENWAL_PDF()
{
AgentDAL agntDAL = new AgentDAL();
//Create a pdf document.
PdfDocument doc = new PdfDocument();
PdfSection sec = doc.Sections.Add();
sec.PageSettings.Width = PdfPageSize.A4.Width;
sec.PageSettings.Height = PdfPageSize.A4.Height;
sec.PageSettings.Margins = new PdfMargins(20f, 5f, 20f, 5f);
PdfPageBase page = sec.Pages.Add();
//title
PdfBrush brush1 = PdfBrushes.Black;
PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Times New Roman", 11f, FontStyle.Bold));
PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Center);
page.Canvas.DrawString("Monthly", font1, brush1, page.Canvas.ClientSize.Width / 2, 60, format1);
page.Canvas.DrawString("Report generated", font1, brush1, page.Canvas.ClientSize.Width / 2, 76, format1);
//grid
PdfGrid grid = new PdfGrid();
grid.Columns.Add(5);
float gridWidth = page.Canvas.ClientSize.Width - (grid.Columns.Count + 1);
for (int j = 0; j < grid.Columns.Count; j++)
{
grid.Columns[j].Width = gridWidth * 0.20f;
}
PdfGridRow row0 = grid.Rows.Add();
PdfGridRow row1 = grid.Rows.Add();
PdfGridRow row2 = grid.Rows.Add();
PdfGridRow row3 = grid.Rows.Add();
row0.Style.Font = new PdfTrueTypeFont(new Font("Times New Roman", 11f, FontStyle.Regular), true);
row1.Style.Font = new PdfTrueTypeFont(new Font("Times New Roman", 11f, FontStyle.Regular), true);
row2.Style.Font = new PdfTrueTypeFont(new Font("Times New Roman", 11f, FontStyle.Regular), true);
row3.Style.Font = new PdfTrueTypeFont(new Font("Times New Roman", 11f, FontStyle.Regular), true);
row0.Cells[2].ColumnSpan = 3;
row1.Cells[2].ColumnSpan = 3;
row2.Cells[2].ColumnSpan = 3;
row3.Cells[2].ColumnSpan = 3;
row0.Cells[2].RowSpan = 4;
row0.Cells[0].Value = "Policy";
row0.Cells[1].Value = "VM5115001510000009";
row1.Cells[0].Value = "Vehicle";
row1.Cells[1].Value = "BBP";
row2.Cells[0].Value = "Premium";
row2.Cells[1].Value = "7,366.10";
row3.Cells[0].Value = "Date";
row3.Cells[1].Value = "03-Jan-2020";
float gridHeight = 20.0f;
for (int i = 0; i < grid.Rows.Count; i++)
{
grid.Rows[i].Height = gridHeight;
}
grid.Draw(page, new PointF(0, 120));
MemoryStream stream = new MemoryStream();
doc.SaveToStream(stream);
HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
result.Content = new ByteArrayContent(stream.ToArray());
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
doc.Close();
return result;
}
What I get from this...
What I want...
I tried to do this, but it didn't work.
row0.Cells[0].Style.Borders.Right.Color = Color.DarkRed;
row1.Cells[0].Style.Borders.Right.Color = Color.DarkRed;
row2.Cells[0].Style.Borders.Right.Color = Color.DarkRed;
row3.Cells[0].Style.Borders.Right.Color = Color.DarkRed;
Try the below code:
float gridHeight = 20.0f;
for (int i = 0; i < grid.Rows.Count; i++)
{
grid.Rows[i].Height = gridHeight;
grid.Rows[i].Cells[0].Style.Borders.Right = new PdfPen(PdfBrushes.DarkRed);
grid.Rows[i].Cells[1].Style.Borders.Left = new PdfPen(PdfBrushes.DarkRed);
}

Viewing applications in WPF with errors

I have an application that breaks the image when the screen size changes
Below is the screen
Where can the mistake be? What can cause this situation? I do not even know where to look because it is not a permanent error only once and once is gone
This is how I load items with a line and number per view:
<ItemsControl ItemsSource="{Binding ConnectorsGridsHorizontal, Mode=TwoWay}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Grid />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
Function drawing 1 element:
public Grid DrawConnector1(Thickness margin, int nr, bool rotate, bool rightSide)
{
Grid grid = new Grid();
grid.Margin = margin;
grid.HorizontalAlignment = HorizontalAlignment.Left;
grid.Width = S10;
grid.Height = 128;
grid.VerticalAlignment = System.Windows.VerticalAlignment.Center;
Line line = new Line();
line.X1 = S10HALF;
line.X2 = S10HALF;
line.Y1 = 0;
line.Y2 = 128;
line.StrokeDashArray = new System.Windows.Media.DoubleCollection() { 4, 2, 1, 2 };
line.Stroke = System.Windows.Media.Brushes.Green;
line.StrokeThickness = 4;
grid.Children.Add(line);
Grid inGrid = new Grid();
inGrid.Width = S10;
inGrid.Height = HF;
inGrid.Background = System.Windows.Media.Brushes.Black;
grid.Children.Add(inGrid);
Border br = new Border();
br.BorderThickness = new Thickness(0);
br.CornerRadius = new CornerRadius(10);
br.Background = System.Windows.Media.Brushes.White;
br.Margin = new Thickness(-10, -10, -10, -10);
br.Width = 20;
br.Height = 20;
br.VerticalAlignment = rightSide ? VerticalAlignment.Top : VerticalAlignment.Bottom;
br.RenderSize = new System.Windows.Size(br.ActualWidth + 1, br.ActualHeight + 1);
if (SelectedConnector + 1 == nr) br.Background = System.Windows.Media.Brushes.Green;
else br.Background = System.Windows.Media.Brushes.White;
if (DisableGreenMark) br.Background = System.Windows.Media.Brushes.White;
TextBlock txtBlock = new TextBlock();
txtBlock.FontSize = 16;
txtBlock.Text = nr.ToString();
txtBlock.Foreground = new SolidColorBrush(Colors.Black);
txtBlock.VerticalAlignment = VerticalAlignment.Center;
txtBlock.HorizontalAlignment = HorizontalAlignment.Center;
br.Child = txtBlock;
grid.Children.Add(br);
Binding b = new Binding("S60_20");
b.Mode = BindingMode.Default;
grid.SetBinding(Canvas.TopProperty, b);
return grid;
}
GIF:
I'm also using SnapsToDevicePixels="true"
To start with, try doing this:
br.HorizontalAlignment = HorizontalAlignment.Stretch;
br.Margin = new Thickness(0, 0, 0, 0);
Do not use margin with a negative value
Stretch will provide you with an element extension
Side cut is probably caused by too narrow a strip for the element:
You have:
grid.Width = S10;
line.X1 = S10HALF;
line.X2 = S10HALF;
This indicates that you have the width set to probably 10
and Here you have set to 20
br.Width = 20;
br.Height = 20;
I also suggest changing here:
grid.Width = S10 * 2;
line.X1 = S10HALF * 2;
line.X2 = S10HALF * 2;

How to resize the row height of a UITableView automatically in xamarin ios

I am new in xamarin.ios I want to resize the row height according to the content length. How can I do that.
Here is my ViewController.Cs
public override void ViewWillAppear(bool animated)
{
base.ViewWillAppear(animated);
nfloat _width = UIScreen.MainScreen.Bounds.Width / 3;
DataBinding();
_table = new UITableView(new CGRect(0, 0, View.Frame.Width, View.Frame.Height));
_table.Source = new DetailsTableViewSource(_caseDesign);
_table.RowHeight = UITableView.AutomaticDimension;
_table.EstimatedRowHeight = 100f;
_table.ReloadData();
View.AddSubview(_table);
}
DetailsTableViewSource.Cs
public class DetailsTableViewSource : UITableViewSource
{
public List<CaseDetails> tabledata;
public DetailsTableViewSource(List<CaseDetails> items)
{
tabledata = items;
}
public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
{
NSString cellIdentifier = new NSString();
var cell = tableView.DequeueReusableCell(cellIdentifier) as CaseDetailsTableCell;
if (cell == null)
cell = new CaseDetailsTableCell(cellIdentifier);
cell.UpdateCell(tabledata[indexPath.Row].Title
, tabledata[indexPath.Row].Description
);
return cell;
}
public override nint RowsInSection(UITableView tableview, nint section)
{
return tabledata.Count;
}
}
CaseDetailsTableCell.cs
public class CaseDetailsTableCell:UITableViewCell
{
UILabel _qst1, _answer1, _seperator;
nfloat _width;
public CaseDetailsTableCell(NSString cellId) : base(UITableViewCellStyle.Default, cellId)
{
_width = UIScreen.MainScreen.Bounds.Width / 3;
_qst1 = new UILabel();
_qst1.Font = _qst1.Font.WithSize(15);
_seperator = new UILabel();
_seperator.TextAlignment = UITextAlignment.Center;
_seperator.Text = ":";
_seperator.Font = _seperator.Font.WithSize(20);
_answer1 = new UILabel();
_answer1.Font = _answer1.Font.WithSize(15);
ContentView.AddSubviews(new UIView[] { _qst1, _seperator, _answer1 });
}
public void UpdateCell(string Quetion, string Answer)
{
_qst1.Text = Quetion;
_answer1.Text = Answer;
}
public override void LayoutSubviews()
{
base.LayoutSubviews();
_qst1.Frame = new CGRect(10, 10, _width + 10, 30);
_qst1.Lines = 0;
_qst1.SizeToFit();
_qst1.LineBreakMode = UILineBreakMode.Clip;
_qst1.TextAlignment = UITextAlignment.Left;
_answer1.Frame = new CGRect(_width * 2, 10, UIScreen.MainScreen.Bounds.Width / 3 - 10, 30);
_answer1.SizeToFit();
_answer1.TextAlignment = UITextAlignment.Justified;
_answer1.LineBreakMode = UILineBreakMode.WordWrap;
_seperator.Frame = new CGRect(_width + 10, 10, UIScreen.MainScreen.Bounds.Width / 3, 30);
// _date.Frame = new CGRect(17, 50, 50, 20);
}
}
I want to bind some data into the table and the length of the each item is different, according to the length of the data, the row height is automatically resize . How can i do that.
Now the table look like this
I want the table like this
If you want to automatically resize the tableView's row height, you should use autoLayout to place your controls in the cell instead of the Frame. Also put the constraints code in the constructor of the cell will be better:
public CaseDetailsTableCell(NSString cellId) : base(UITableViewCellStyle.Default, cellId)
{
_width = UIScreen.MainScreen.Bounds.Width / 3;
_qst1 = new UILabel();
_qst1.Font = _qst1.Font.WithSize(15);
_seperator = new UILabel();
_seperator.TextAlignment = UITextAlignment.Center;
_seperator.Text = ":";
_seperator.Font = _seperator.Font.WithSize(20);
_answer1 = new UILabel();
_answer1.Font = _answer1.Font.WithSize(15);
ContentView.AddSubviews(new UIView[] { _qst1, _seperator, _answer1 });
// Disable this to enable autoLayout
_qst1.TranslatesAutoresizingMaskIntoConstraints = false;
var qstLeading = NSLayoutConstraint.Create(_qst1, NSLayoutAttribute.Leading, NSLayoutRelation.Equal, ContentView, NSLayoutAttribute.Leading, 1, 10);
var qstTop = NSLayoutConstraint.Create(_qst1, NSLayoutAttribute.Top, NSLayoutRelation.Equal, ContentView, NSLayoutAttribute.Top, 1, 10);
var qstWidth = NSLayoutConstraint.Create(_qst1, NSLayoutAttribute.Width, NSLayoutRelation.Equal, null, NSLayoutAttribute.NoAttribute, 0, _width + 10);
var qstBottom = NSLayoutConstraint.Create(_qst1, NSLayoutAttribute.Bottom, NSLayoutRelation.LessThanOrEqual, ContentView, NSLayoutAttribute.Bottom, 1, -10);
_qst1.Lines = 0;
_qst1.SizeToFit();
_qst1.LineBreakMode = UILineBreakMode.Clip;
_qst1.TextAlignment = UITextAlignment.Left;
_qst1.BackgroundColor = UIColor.Blue;
_seperator.TranslatesAutoresizingMaskIntoConstraints = false;
var sepLeading = NSLayoutConstraint.Create(_seperator, NSLayoutAttribute.Leading, NSLayoutRelation.Equal, _qst1, NSLayoutAttribute.Trailing, 1, 10);
var sepTop = NSLayoutConstraint.Create(_seperator, NSLayoutAttribute.Top, NSLayoutRelation.Equal, ContentView, NSLayoutAttribute.Top, 1, 10);
var sepWidth = NSLayoutConstraint.Create(_seperator, NSLayoutAttribute.Width, NSLayoutRelation.Equal, null, NSLayoutAttribute.NoAttribute, 0, UIScreen.MainScreen.Bounds.Width / 3);
_seperator.BackgroundColor = UIColor.Yellow;
_answer1.TranslatesAutoresizingMaskIntoConstraints = false;
var ansLeading = NSLayoutConstraint.Create(_answer1, NSLayoutAttribute.Leading, NSLayoutRelation.Equal, _seperator, NSLayoutAttribute.Trailing, 1, 10);
var ansTop = NSLayoutConstraint.Create(_answer1, NSLayoutAttribute.Top, NSLayoutRelation.Equal, ContentView, NSLayoutAttribute.Top, 1, 10);
var ansTrailing = NSLayoutConstraint.Create(_answer1, NSLayoutAttribute.Trailing, NSLayoutRelation.Equal, ContentView, NSLayoutAttribute.Trailing, 1, -10);
var ansBottom = NSLayoutConstraint.Create(_answer1, NSLayoutAttribute.Bottom, NSLayoutRelation.LessThanOrEqual, ContentView, NSLayoutAttribute.Bottom, 1, -10);
_answer1.SizeToFit();
_answer1.TextAlignment = UITextAlignment.Justified;
_answer1.LineBreakMode = UILineBreakMode.WordWrap;
_answer1.BackgroundColor = UIColor.Red;
_answer1.Lines = 0;
ContentView.AddConstraints(new NSLayoutConstraint[] { qstTop, qstLeading, qstWidth, qstBottom, sepLeading, sepTop, sepWidth, ansLeading, ansTop, ansTrailing, ansBottom });
}
Here I add the LessThanOrEqual NSLayoutRelation to bottom constraint of the _qst1 and _answer1 labels, the cell will adjust its height depending on the tallest one. You can adjust the constraints to fit your requirements.

Moving a group of elements on Canvas

I want to draw a signal image as shown below at runtime on canvas.
sample code which I used to draw this signal is as below.
Ellipse Ellipse1 = new Ellipse();
Ellipse Ellipse2 = new Ellipse();
Ellipse Ellipse3 = new Ellipse();
Line lineV = new Line();
Line lineH = new Line();
lineV.Stroke = SystemColors.WindowFrameBrush;
lineV.X1 = EndPosition.X;
lineV.Y1 = EndPosition.Y;
lineV.X2 = StartPosition.X;
lineV.Y2 = EndPosition.Y;
SolidColorBrush redBrush = new SolidColorBrush();
redBrush.Color = Colors.Black;
lineV.StrokeThickness = 2;
lineV.Stroke = redBrush;
canvas1.Children.Add(lineV);
lineH.Stroke = SystemColors.WindowFrameBrush;
lineH.X1 = StartPosition.X;
lineH.Y1 = EndPosition.Y;
lineH.X2 = StartPosition.X;
lineH.Y2 = StartPosition.Y;
redBrush.Color = Colors.Black;
lineH.StrokeThickness = 2;
lineH.Stroke = redBrush;
canvas1.Children.Add(lineH);
SolidColorBrush mySolidColorBrush1 = new SolidColorBrush();
mySolidColorBrush1.Color = Colors.Red; //FromArgb(255, 255, 255, 0);
Ellipse1.Fill = mySolidColorBrush1;
Ellipse1.StrokeThickness = 2;
Ellipse1.Stroke = Brushes.Black;
Ellipse1.Width = 30;
Ellipse1.Height = 30;
Ellipse1.Margin = new Thickness(EndPosition.X, EndPosition.Y - 15, EndPosition.X + 50, EndPosition.Y + 50);
canvas1.Children.Add(Ellipse1);
SolidColorBrush mySolidColorBrush2 = new SolidColorBrush();
mySolidColorBrush2.Color = Colors.Green; //FromArgb(255, 255, 255, 0);
Ellipse2.Fill = mySolidColorBrush2;
Ellipse2.StrokeThickness = 2;
Ellipse2.Stroke = Brushes.Black;
Ellipse2.Width = 30;
Ellipse2.Height = 30;
Ellipse2.Margin = new Thickness(EndPosition.X + 30, EndPosition.Y - 15, EndPosition.X + 60, EndPosition.Y + 50);
canvas1.Children.Add(Ellipse2);
SolidColorBrush mySolidColorBrush3 = new SolidColorBrush();
mySolidColorBrush3.Color = Colors.Yellow; // FromArgb(255, 255, 255, 0);
Ellipse3.Fill = mySolidColorBrush3;
Ellipse3.StrokeThickness = 2;
Ellipse3.Stroke = Brushes.Black;
Ellipse3.Width = 30;
Ellipse3.Height = 30;
Ellipse3.Margin = new Thickness(EndPosition.X + 60, EndPosition.Y - 15, EndPosition.X + 150, EndPosition.Y + 50);
canvas1.Children.Add(Ellipse3);
**Now I want the user to be able to interactively move this signal on the canvas on mouse move events.
How can I able to do this ?**
Iam using C# WPF.
If want to implement a canvas to drag elements, a DragCanvas is your choice.
Dragging Elements in a Canvas from Josh Smith and after Dragging Elements in a Canvas from igkutikov that adapt Josh Smith code. There are articles with #mustread category.
With the dragCanvas you can implement a full functionality Dragging Element Canvas, and better adapt to your code. Happy coding!

Rectangle shape won't update left or top positions with canvas

It won't change the position, the position is fixed at runtime even though i change the value 50 below:
System.Windows.Shapes.Rectangle myRectangle = new System.Windows.Shapes.Rectangle();
mainGrid.Children.Add(myRectangle);
Canvas.SetLeft(myRectangle, 50);
Canvas.SetTop(myRectangle, 50);
myRectangle.Height = 100;
myRectangle.Width = 100;
myRectangle.Stroke = System.Windows.Media.Brushes.LightSteelBlue;
here are the 2 solutions for your problem
1) with a canvas
var myRectangle = new System.Windows.Shapes.Rectangle();
var mainCanvas = new Canvas();
mainGrid.Children.Add(mainCanvas);
mainCanvas.Children.Add(myRectangle);
Canvas.SetLeft(myRectangle, 50);
Canvas.SetTop(myRectangle, 50);
myRectangle.Height = 100;
myRectangle.Width = 100;
myRectangle.Stroke = System.Windows.Media.Brushes.LightSteelBlue;
2) only with your grid
var myRectangle = new System.Windows.Shapes.Rectangle();
mainGrid.Children.Add(myRectangle);
myRectangle.Margin = new Thickness(50, 50, 0, 0);
myRectangle.HorizontalAlignment = HorizontalAlignment.Left;
myRectangle.VerticalAlignment = VerticalAlignment.Top;
myRectangle.Height = 100;
myRectangle.Width = 100;
myRectangle.Stroke = System.Windows.Media.Brushes.LightSteelBlue;

Categories