質問

I have a WPF GUI that allows the user to open up an options menu. The option menu opens in a new window and is filled with check boxes. When the user presses the "ok" button the window closes. However, it doesn't remember what check boxes were checked when it is opened back up. How do I make sure the program is able to remember what boxes were checked and which ones weren't?

Just to specify: I only need to remember which boxes are checked during the run of the program. The program does not need to remember after the entire program has been exited.

Thanks!

Here is my code under the main window Window1.XAML.CS:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

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

        //Exit
        private void Exit_Click(object sender, RoutedEventArgs e)
        {
            System.Environment.Exit(0);
        }

        //Options
        private void Options_Click(object sender, RoutedEventArgs e)
        {
            var newWindow = new Options();
            newWindow.Show();
        }
    }
}

Here is my code under the child Window Options.XAML.CS. This is based off of the first answer. I've read through the link you posted and it makes sense. I have conditions in my settings file that I change when the user checks my check boxes. I then have a condition that determines whether the box is checked based on the settings file, but it doesn't seem to reflect any change...

public partial class Options_Window : Window
    {
        public Options_Window()
        {
            InitializeComponent();

            //Checkbox1
            if (Properties.Settings.Default.OptionsBox1 == true)
                checkBox1.IsChecked = true;
            else
                checkBox1.IsChecked = false;
        }

        //Close Window
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        //Ask before downloading... - CHECKED
        private void checkBox1_Checked(object sender, RoutedEventArgs e)
        {
            Properties.Settings.Default.OptionsBox1 = true;
        }

        //Ask before downloading... - UNCHECKED
        private void checkBox1_Unchecked(object sender, RoutedEventArgs e)
        {
            Properties.Settings.Default.OptionsBox1 = false;
        }
役に立ちましたか?

解決

You can use Settings to share data between different Windows/Controls and even save application data when closing/starting an application.

The .NET Framework allows you to create and access values that are persisted between application execution sessions. These values are called settings. Settings can represent user preferences, or valuable information the application needs to use. For example, you might create a series of settings that store user preferences for the color scheme of an application. Or you might store the connection string that specifies a database that your application uses. Settings allow you to both persist information that is critical to the application outside of the code, and to create profiles that store the preferences of individual users.

You can save a Setting in any Window:

Properties.Settings.Default.mySetting = true;
Properties.Settings.Default.Save();

You can read/use a setting in any Window:

this.Property = Properties.Settings.Default.mySetting;

他のヒント

First we need a settings object, that stores our properties. Remember to properly implement INotifyPropertyChanged

class MySettings : INotifyPropertyChanged
{
    public bool IsSettingSet {get;set;}
}

then in our settings window, just use bindings to bind the view controls to your Settings object.

<Window 
    x:Class="SettingsWindow"
    ...>
    <CheckBox IsChecked="{Binding IsSettingSet}"/>
</Window>

and finally where you actually open the window, you need to assign your settings object to the DataContext of your settings window.

class MainWindow
{
    public MainWindow()
    {
        InitializeComponent();
    }
    void OpenSettingsWindow()
    {
        var dlg = new SettingsWindow();
        dlg.DataContext = mGlobalSettings;
        dlg.ShowDialog();
    }

    MySettings mGlobalSettings = new MySettings();
}

now everytime you open the window, your settings will be the same like they were last time. As long as you don't close the application.

If it's just during runtime, then it's pretty easy. You could use a static class:

public static class MyState
{
    public static bool IsChecked1 { get; set; }
}

Or a singleton instance of a class:

public class MyState   
{
   private static MyState _instance = new MyState();

   public static MyState Instance 
   {
      get { return _instance; }
   }

   private MyState() {}

   public bool IsChecked1 { get; set; }

}

When the window loads, get the state from the properties of MyClass. When the window is closing, set the properties of MyClass.

In Options.xaml.cs:

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    CheckBox1.IsChecked = MyState.IsChecked1;
}

private void Window_Closed(object sender, EventArgs e)
{
    MyState.IsChecked1 = CheckBox1.IsChecked;
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top