public int count = 1;
Dictionary<DatePicker, int> StartDates = new Dictionary<DatePicker, int>();
Dictionary<DatePicker, int> EndDates = new Dictionary<DatePicker, int>();
private void AddDateBtn_Clicked(object sender, EventArgs e)
{
Grid grid1 = new Grid
{
ColumnDefinitions =
{
new ColumnDefinition{ Width = new GridLength(0.4, GridUnitType.Star)},
new ColumnDefinition{ Width = new GridLength(0.6, GridUnitType.Star)}
}
};
Label label1 = new Label
{
Text = "Start Date",
HorizontalOptions = LayoutOptions.StartAndExpand,
VerticalOptions = LayoutOptions.CenterAndExpand,
FontSize = Device.GetNamedSize(NamedSize.Caption, typeof(Label)),
TextColor = Color.Black
};
grid1.Children.Add(label1, 0, 0);
DatePicker startdatePicker1 = new DatePicker
{
HorizontalOptions = LayoutOptions.StartAndExpand,
VerticalOptions = LayoutOptions.CenterAndExpand,
FontSize = Device.GetNamedSize(NamedSize.Caption, typeof(Label)),
TextColor = Color.Black
};
StartDates.Add(startdatePicker1, count);
grid1.Children.Add(startdatePicker1, 1, 0);
BoxView boxView = new BoxView
{
HorizontalOptions = LayoutOptions.FillAndExpand,
HeightRequest = 1,
BackgroundColor = Color.LightGray
};
NewDurationStack.Children.Add(grid1);
NewDurationStack.Children.Add(boxView);
Grid grid2 = new Grid
{
ColumnDefinitions =
{
new ColumnDefinition{ Width = new GridLength(0.4, GridUnitType.Star)},
new ColumnDefinition{ Width = new GridLength(0.6, GridUnitType.Star)}
}
};
Label label2 = new Label
{
Text = "End Date",
HorizontalOptions = LayoutOptions.StartAndExpand,
VerticalOptions = LayoutOptions.CenterAndExpand,
FontSize = Device.GetNamedSize(NamedSize.Caption, typeof(Label)),
TextColor = Color.Black
};
grid2.Children.Add(label2, 0, 0);
DatePicker enddatePicker1 = new DatePicker
{
HorizontalOptions = LayoutOptions.StartAndExpand,
VerticalOptions = LayoutOptions.CenterAndExpand,
FontSize = Device.GetNamedSize(NamedSize.Caption, typeof(Label)),
TextColor = Color.Black
};
grid2.Children.Add(enddatePicker1, 1, 0);
EndDates.Add(enddatePicker1, count);
BoxView boxView1 = new BoxView
{
HorizontalOptions = LayoutOptions.FillAndExpand,
HeightRequest = 1,
BackgroundColor = Color.LightGray
};
NewDurationStack.Children.Add(grid2);
NewDurationStack.Children.Add(boxView1);
}
I have this Button click event which creates two DatePickers every time the user press that button. Users can create as many as they want. Also, this page includes a Request form users can have multiple requests. So on the same page user can create mutiple requests forms and every request can have multiple date ranges. so how can I get the values of all the dates at the end? I try adding the controls to Dictionry but that works for one request form with multiple dates ranges not for multiple requests that have multiple date ranges.
CodePudding user response:
The method you use is a bit primitive. You can easily solve this using listview. check this out
first you add new element in ObservableCollection<DatTime>
when button event is triggered. The DatePicker
you specify only once in the listview
will automatically replicate itself.
CodePudding user response:
You could get the date when you select the DatePicker with DateSelected event,and then you could use KeyValue to store them.
For example in your codes above:
Dictionary<int, DateTime> startDateTimes = new Dictionary<int, DateTime>();
Dictionary<int, DateTime> endDateTimes = new Dictionary<int, DateTime>();
DatePicker startdatePicker1 = new DatePicker
{
HorizontalOptions = LayoutOptions.StartAndExpand,
VerticalOptions = LayoutOptions.CenterAndExpand,
FontSize = Device.GetNamedSize(NamedSize.Caption, typeof(Label)),
TextColor = Color.Black
};
startdatePicker1.DateSelected = StartdatePicker1_DateSelected;
StartDates.Add(startdatePicker1, count);
...
DatePicker enddatePicker1 = new DatePicker
{
HorizontalOptions = LayoutOptions.StartAndExpand,
VerticalOptions = LayoutOptions.CenterAndExpand,
FontSize = Device.GetNamedSize(NamedSize.Caption, typeof(Label)),
TextColor = Color.Black
};
enddatePicker1.DateSelected = EnddatePicker1_DateSelected;
EndDates.Add(enddatePicker1, count);
private void StartdatePicker1_DateSelected(object sender, DateChangedEventArgs e)
{
DatePicker datePicker = sender as DatePicker;
startDateTimes.Add(1, datePicker.Date); //1 corresponds to your count
}
private void EnddatePicker1_DateSelected(object sender, DateChangedEventArgs e)
{
DatePicker datePicker = sender as DatePicker;
endDateTimes.Add(1, datePicker.Date); //1 corresponds to your count
}