Goober S. Johnsson Goober S. Johnsson - 2 months ago 5
C# Question

Showing a new page it unchecks my checkboxes. How do I prevent it?

So I have this application where you can select from different pages, Like a page navigation system..
And I have a checkbox on both of them.
And if I check a checkbox on

SecondPage();
and then go to
thirdPage();
the checkbox on
SecondPage();
will be unchecked, how do I prevent this?

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}

bool isCHecked = true;

private void button_Click(object sender, RoutedEventArgs e)
{
main.Content = new SecondPage();
}

private void button1_Click(object sender, RoutedEventArgs e)
{
main.Content = new thirdPage();
}
}

Answer

You are always creating a new instance of the pages so it will be a fresh copy, and the state that was previously there would be lost, one of that solution is to create fields in your main window like:

namespace CheckboxesAndPages
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private thirdPage _thirdPage;
        private SecondPage _SecondPage;
        public MainWindow()
        {
            InitializeComponent();

        }

        bool isCHecked = true;

        private void button_Click(object sender, RoutedEventArgs e)
        {
            _SecondPage = _SecondPage != null ? _SecondPage : new SecondPage(); 
            main.Content = _SecondPage;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            _thirdPage = _thirdPage !=null ? _thirdPage : new thirdPage();
            main.Content = _thirdPage;
        }
    }

or you can create Properties as well:

namespace CheckboxesAndPages
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            private thirdPage _thirdPage;
            private SecondPage _SecondPage;
            private ThirdPage ThirdPage 
            {
               get
               { 
                   _thirdPage = _thirdPage !=null ? new thirdPage();
                   return _thirdPage;
               }
               set
               {
                   _thirdPage = value;
               }
           }

           private SecondPage SecondPage
            {
               get
               { 
                   _SecondPage= _SecondPage!=null ? new SecondPage();
                   return _SecondPage;
               }
               set
               {
                   _SecondPage= value;
               }
           }
            private SecondPage _SecondPage;
            public MainWindow()
            {
                InitializeComponent();

            }

            bool isCHecked = true;

            private void button_Click(object sender, RoutedEventArgs e)
            { 
                main.Content = SecondPage;
            }

            private void button1_Click(object sender, RoutedEventArgs e)
            {
                main.Content = ThirdPage;
            }
        }

or you can write null condition like:

_SecondPage = _SecondPage ?? new SecondPage(); 
Comments