none
ContentDialog and checkbox, detect if ischecked

    Question

  • i have write this code and When user makes tap on "ok" i would like to read the contents of checkboxes but cb or cb1 is nothing when closing dialog.

     var dialog = new ContentDialog()
                {
                    Title = "Title",
                    MaxWidth = this.ActualWidth
                };
    
                var panel = new StackPanel();
    
                panel.Children.Add(new TextBlock
                {
                    Text = "text",
                    TextWrapping = TextWrapping.Wrap,
                });
    
                var cb = new CheckBox
                {
                    Content = "check1"
                };
                var cb1 = new CheckBox
                {
                    Content = "check2",
                    IsChecked = false
                };
    
                cb1.SetBinding(CheckBox.IsCheckedProperty, new Binding
                {
                    Source = dialog,
                    Path = new PropertyPath("IsPrimaryButtonEnabled"),
                    Mode = BindingMode.TwoWay,
                });
    
                panel.Children.Add(cb);
                panel.Children.Add(cb1);
                dialog.Content = panel;
    
                dialog.PrimaryButtonText = "OK";
                dialog.IsPrimaryButtonEnabled = false;
    
                ContentDialogResult result = await dialog.ShowAsync();
    how to detect cb and cb1 ischecked?
    Friday, April 21, 2017 2:34 PM

All replies

  • Hello Giuseppedev78,

    Though I feel it would be possible to read IsChecked properties immediately after dialog.ShowAsync() call, but you are trying to take a some different approach?

                ContentDialogResult result = await dialog.ShowAsync();
    
                bool check1_ischecked = cb.IsChecked.GetValueOrDefault(false);
                bool check2_ischecked = cb1.IsChecked.GetValueOrDefault(false);
    
                Debug.WriteLine("check1: " + check1_ischecked.ToString());
                Debug.WriteLine("check2: " + check2_ischecked.ToString());

    Saturday, April 22, 2017 4:45 PM
  • Hi Giuseppedev78,

    Welcome to the Developing Universal Windows apps forum! Please utilize tagging when posting to this forum, thanks!

    If you make the cb and cb1 as fields of the class, you can access the cb and cb1 ischecked property directly when the "OK" button taped.

                //register event
                dialog.PrimaryButtonClick += Button_Click1;
    
                ContentDialogResult result = await dialog.ShowAsync();
            }
    
            CheckBox cb, cb1;
    
            private void Button_Click1(ContentDialog sender, ContentDialogButtonClickEventArgs args)
            {
                Debug.WriteLine("check1: " + cb.IsChecked.ToString());
                Debug.WriteLine("check2: " + cb1.IsChecked.ToString());
            }

    Best regards,

    Breeze


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, April 25, 2017 1:36 AM
    Moderator