I'm trying to use CotentDialog to get some data and change it in my database in UWP XAML but I encountered some issues

39 Views Asked by At
private async void iconu_PointerPressed(object sender, PointerRoutedEventArgs eventArgs)
{
    if (BigGrid.SelectedIndex == -1)
    {
        ContentDialog dialog2 = new ContentDialog
        {
            Title = "Please select a Grid!",
            Content = "When selected it will have a border :)",
            CloseButtonText = "Cancel",
        };
        ContentDialogResult result2 = await dialog2.ShowAsync();
        return;
    }
    ContentDialog dialog = new ContentDialog
    {   

        Title = "Please enter your information",
        CloseButtonText = "Cancel",
        PrimaryButtonText = "OK",
        Content = new StackPanel
        {
            Children =
        {
            new TextBlock { Text = "Title:" },
            new TextBox { Name = "TitleTextBox" },
            new TextBlock { Text = "Description:" },
            new TextBox { Name = "DescriptionTextBox" },
            new TextBlock { Text = "Select Date:" },
            new DatePicker { Name = "DatePicker" },
            new TextBlock { Text = "Select Time:" },
            new TimePicker { Name = "TimePicker" }
        }
        }
    };

    ContentDialogResult result = await dialog.ShowAsync();
    if (result == ContentDialogResult.Primary)
        {
            TextBox titleTextBox = dialog.FindName("TitleTextBox") as TextBox;
            TextBox descriptionTextBox = dialog.FindName("DescriptionTextBox") as TextBox;
            DatePicker datePicker = dialog.FindName("DatePicker") as DatePicker;
            TimePicker timePicker = dialog.FindName("TimePicker") as TimePicker;

            if (titleTextBox != null && descriptionTextBox != null && datePicker != null && timePicker != null)
            {
                Debug.WriteLine("All controls are not null.");
                string title = titleTextBox.Text;
                string description = descriptionTextBox.Text;
                DateTime selectedDateTime = datePicker.Date.DateTime.Date + timePicker.Time;
                Debug.WriteLine($"1.TitleTextBox: {titleTextBox.Text}, DescriptionTextBox: {descriptionTextBox.Text}, DatePicker: {datePicker.Date}, TimePicker: {timePicker.Time}");

            int Id = ((Note)BigGrid.SelectedItem).Id;
                Notedatabase.UpdateData(Id, title, description, selectedDateTime);
                BigGrid.ItemsSource = Notedatabase.GetData();
            }
            else
            {
                Debug.WriteLine("Some controls are null.");
                Debug.WriteLine($"2.TitleTextBox: {titleTextBox}, DescriptionTextBox: {descriptionTextBox}, DatePicker: {datePicker}, TimePicker: {timePicker}");
                ContentDialog dialog1 = new ContentDialog
                {
                    Title = "Please input All values!",
                    Content = "Thank you :)", 
                    CloseButtonText = "Cancel",
                };
                await dialog1.ShowAsync();
            }
    }

I am getting data from a Locally saved Database, BigGrid is a GridView.ItemTemplate

I expected to get the values to save but it kept throwing the other dialog box "Please input All values"

Debug.WriteLine($"2.TitleTextBox: {titleTextBox}, DescriptionTextBox: {descriptionTextBox}, DatePicker: {datePicker}, TimePicker: {timePicker}");

This line shows that there is nothing inside

I think I did this wrongly? But I'm not too sure

TextBox titleTextBox = dialog.FindName("TitleTextBox") as TextBox;
            TextBox descriptionTextBox = dialog.FindName("DescriptionTextBox") as TextBox;
            DatePicker datePicker = dialog.FindName("DatePicker") as DatePicker;
            TimePicker timePicker = dialog.FindName("TimePicker") as TimePicker;

Any help would be greatly appreciated :D

1

There are 1 best solutions below

1
chirpe On
            StackPanel stackPanel = (StackPanel)dialog.Content;
            string title = ((TextBox)stackPanel.Children[1]).Text;
            string description = ((TextBox)stackPanel.Children[3]).Text;
            DateTime selectedDate = ((DatePicker)stackPanel.Children[5]).Date.DateTime;
            TimeSpan selectedTime = ((TimePicker)stackPanel.Children[7]).SelectedTime.Value;

I used this instead and it worked :)