I have created a gridtemplatecolumn inside the viewcell, I have created a label with tap gesture in there i need a row and column index. when i tap the label.
I have attached my code.
var column = new GridTemplateColumn();
var dtt = new DataTemplate(() => {
return new DateTimeTextBox();
});
column.CellTemplate = dtt;
dataGrid.Columns.Add(column);
dataGrid.ItemSource= DataTable;
public class DateTimeTextBox : ViewCell
{
StackLayout mainLayout = new StackLayout();
mainLayout.Orientation = StackOrientation.Horizontal;
var label = new Label();
label .Text = "Tap Here";
var tap = new TapGestureRecognizer();
tap.Tapped += (s, e) =>
{
// handle the tap
// Need Row and Column Index
};
}
|
public class DateTimeTextBox : ViewCell
{
StackLayout stack;
Label label;
TapGestureRecognizer tap;
public DateTimeTextBox()
{
stack = new StackLayout() { Orientation = StackOrientation.Vertical };
tap = new TapGestureRecognizer();
tap.Tapped += Tap_Tapped;
label = new Label() { Text = "test" };
label.GestureRecognizers.Add(tap);
stack.Children.Add(label);
View = stack;
}
private void Tap_Tapped(object sender, EventArgs e)
{
var rowindex= ((sender as Label).Parent.Parent as GridCell).DataColumn.RowIndex;
var colindex=((sender as Label).Parent.Parent as GridCell).DataColumn.ColumnIndex;
Debug.WriteLine("RowIndex" + rowindex + "\t" + "ColumnIndex" + colindex + "\n");
}
} |
tq it worked.How do i assign value on GridTemplate> Label View. i am adding row on sfdatagrid dynamically with the values.
|
public DataGridPage()
{
InitializeComponent();
dataGrid.AutoGenerateColumns = false;
var column = new GridTemplateColumn();
column.MappingName = "FirstName";
var dtt = new DataTemplate(() =>
{
return new DateTimeTextBox();
});
column.CellTemplate = dtt;
dataGrid.Columns.Add(column);
…
}
public class DateTimeTextBox : ViewCell
{
StackLayout stack;
Label label;
TapGestureRecognizer tap;
public DateTimeTextBox()
{
stack = new StackLayout() { Orientation = StackOrientation.Vertical };
tap = new TapGestureRecognizer();
tap.Tapped += Tap_Tapped;
label = new Label();
label.SetBinding(Label.TextProperty, new Binding($"[{0}]"));
label.GestureRecognizers.Add(tap);
stack.Children.Add(label);
View = label;
}
} |
tq it worked If I have several columns how do I get the column index on Viewcell?
|
for (int i=0; i < PeopleDT.Columns.Count ; i++)
{
var column = new GridTemplateColumn();
column.MappingName = PeopleDT.Columns[i].ColumnName;
column.CellTemplate = GetTemplate(i);
dataGrid.Columns.Add(column);
}
dataGrid.ItemsSource = PeopleDT;
this.Content = dataGrid;
}
private DataTemplate GetTemplate(int index)
{
DataTemplate cellTemplate = new DataTemplate(() =>
{
return new DateTimeTextBox(index);
});
return cellTemplate;
}
public class DateTimeTextBox : ViewCell
{
StackLayout stack;
Label label;
TapGestureRecognizer tap;
public DateTimeTextBox(int no)
{
stack = new StackLayout() { Orientation = StackOrientation.Vertical };
tap = new TapGestureRecognizer();
tap.Tapped += Tap_Tapped;
label = new Label();
label.SetBinding(Label.TextProperty, new Binding($"[{no}]"));
label.GestureRecognizers.Add(tap);
stack.Children.Add(label);
View = label;
} |