【发布时间】:2013-07-10 18:02:40
【问题描述】:
我有一个允许用户打开选项菜单的 WPF GUI。选项菜单在新窗口中打开,并充满了复选框。当用户按下“确定”按钮时,窗口关闭。但是,它不记得打开备份时选中了哪些复选框。如何确保程序能够记住哪些框被选中,哪些没有被选中?
只是为了说明:我只需要记住在程序运行期间选中了哪些框。整个程序退出后程序不需要记住。
谢谢!
这是我在主窗口 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();
}
}
}
这是我在子 Window Options.XAML.CS 下的代码。这是基于第一个答案。我已经阅读了您发布的link,这是有道理的。我的设置文件中有条件,当用户选中我的复选框时我会更改这些条件。然后我有一个条件确定是否根据设置文件选中该框,但它似乎没有反映任何更改...
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;
}
【问题讨论】:
-
您是否考虑过将复选框绑定到 ViewModel 中的布尔属性?如果您的意思是主窗口关闭,则需要以某种方式保留这些状态;也许是一个 xml 文件。
-
为了清楚起见,即使在重新启动整个程序后,您是否仍试图保持这种持久性?如果是这种情况,我会将其保存到您的 APPDATA 文件夹中的 XML 文件中。
-
就在程序运行期间。如果用户再次打开选项窗口,我希望选中相同的复选框。
-
您还可以在应用程序运行期间使用设置保存数据。
-
我该怎么做呢?
标签: c# wpf childwindow