显示新页面会取消选中我的复选框。我该如何预防呢?

时间:2016-10-01 15:35:38

标签: c# .net wpf

所以我有这个应用程序,您可以从不同的页面中选择,如页面导航系统.. 我有两个复选框。 如果我选中SecondPage();上的复选框,然后转到thirdPage();SecondPage();上的复选框将取消选中,我该如何阻止此操作?

/// <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();
    }
}

1 个答案:

答案 0 :(得分:0)

您总是在创建一个新的页面实例,因此它将是一个全新的副本,之前存在的状态将会丢失,其中一个解决方案是在主窗口中创建字段,如:

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;
        }
    }

或者你也可以创建属性:

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;
            }
        }

或者您可以编写null条件,如:

_SecondPage = _SecondPage ?? new SecondPage();