C# 将CheckedListBox绑定到设置
我正试图在WPF中构建一个CheckedListBox,并将其绑定到用户设置,以便只构建一次,然后使用C# 将CheckedListBox绑定到设置,c#,wpf,c#-4.0,C#,Wpf,C# 4.0,我正试图在WPF中构建一个CheckedListBox,并将其绑定到用户设置,以便只构建一次,然后使用ListBox项资源=“{Binding Source={x:Static local:settings.Default},Path=Customers,Mode=TwoWay}在XAML中自动更新设置 我可以看到复选框,但它们不显示客户名称,也不在设置中保留其值。有人能够发现原因吗 我想我可能需要将设置转换为当前客户可观察集合,但无法使其工作 Customers=Properties.Sett
ListBox项资源=“{Binding Source={x:Static local:settings.Default},Path=Customers,Mode=TwoWay}在XAML中自动更新设置
我可以看到复选框,但它们不显示客户名称,也不在设置中保留其值。有人能够发现原因吗
我想我可能需要将设置转换为当前客户可观察集合,但无法使其工作
Customers=Properties.Settings.Default.Customers.Cast
TestSettings.xaml
<Window x:Class="WpfApplication1.TestSettings"
xmlns:local="clr-namespace:WpfApplication1.Properties"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="TestSettings" Height="300" Width="300">
<Grid>
<ListBox ScrollViewer.VerticalScrollBarVisibility="Auto" ItemsSource="{Binding Source={x:Static local:Settings.Default}, Path=Customers, Mode=TwoWay}" Margin="12,22,12,79">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding IsChecked}" Content="{Binding Path=Item.CustomerName}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Window>
<Window x:Class="WpfApplication1.TestSettings"
xmlns:local="clr-namespace:WpfApplication1.Properties"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="TestSettings" Height="300" Width="300" Closing="Window_Closing">
<Grid>
<ListBox ScrollViewer.VerticalScrollBarVisibility="Auto" ItemsSource="{Binding Source={x:Static local:Settings.Default}, Path=Customers, Mode=TwoWay}" Margin="12,22,12,79">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding IsChecked}" Content="{Binding Path=Item.CustomerName}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Window>
TestSettings.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.Shapes;
using System.ComponentModel;
using System.Collections.ObjectModel;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for TestSettings.xaml
/// </summary>
public partial class TestSettings : Window
{
public ObservableCollection<CheckedListItem<Customer>> Customers { get; set; }
public class Customer
{
public string CustomerName { get; set; }
}
public TestSettings()
{
InitializeComponent();
if (Properties.Settings.Default.Customers == null)
{
Properties.Settings.Default.Customers = new ObservableCollection<Customer>
{
new Customer() { CustomerName = "Kelly Smith" },
new Customer() { CustomerName = "Joe Brown" },
new Customer() { CustomerName = "Herb Dean" }
//Customers.Add(new CheckedListItem<Customer>(new Customer(){CustomerName="Kelly Smith"}));
//Customers.Add(new CheckedListItem<Customer>(new Customer(){CustomerName="Joe Brown"}));
//Customers.Add(new CheckedListItem<Customer>(new Customer(){CustomerName="Herb Dean"}));
};
Properties.Settings.Default.Save();
}
else
{
// Customers = (ObservableCollection<Customer>)Properties.Settings.Default.Customers;
}
}
public class CheckedListItem<T> : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private bool isChecked;
private T item;
public CheckedListItem()
{ }
public CheckedListItem(T item, bool isChecked = false)
{
this.item = item;
this.isChecked = isChecked;
}
public T Item
{
get { return item; }
set
{
item = value;
if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Item"));
}
}
public bool IsChecked
{
get { return isChecked; }
set
{
isChecked = value;
if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("IsChecked"));
}
}
}
private void Window_Closing(object sender, CancelEventArgs e)
{
Properties.Settings.Default.Save();
}
}
}
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.269
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System.Collections.ObjectModel;
namespace WpfApplication1.Properties
{
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "10.0.0.0")]
internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase
{
private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
public static Settings Default
{
get
{
return defaultInstance;
}
}
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
public ObservableCollection<WpfApplication1.TestSettings.Customer> Customers
{
get
{
return ((ObservableCollection<WpfApplication1.TestSettings.Customer>)(this["Customers"]));
}
set
{
this["Customers"] = value;
}
}
}
}
using System.Collections.ObjectModel;
namespace WpfApplication1.Properties {
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "10.0.0.0")]
internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {
private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
public static Settings Default {
get {
return defaultInstance;
}
}
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
public ObservableCollection<WpfApplication1.TestSettings.CheckedListItem<WpfApplication1.TestSettings.Customer>> Customers
{
get
{
return ((ObservableCollection<WpfApplication1.TestSettings.CheckedListItem<WpfApplication1.TestSettings.Customer>>)(this["Customers"]));
}
set
{
this["Customers"] = value;
}
}
}
}
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.Shapes;
using System.ComponentModel;
using System.Collections.ObjectModel;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for TestSettings.xaml
/// </summary>
public partial class TestSettings : Window
{
public ObservableCollection<CheckedListItem<Customer>> Customers
{
get;
set;
}
public class Customer
{
public string CustomerName { get; set; }
}
public TestSettings()
{
InitializeComponent();
if (Properties.Settings.Default.Customers == null)
{
Properties.Settings.Default.Customers = new ObservableCollection<CheckedListItem<Customer>>
{
new CheckedListItem<Customer>(new Customer() { CustomerName = "Kelly Smith" }),
new CheckedListItem<Customer>(new Customer() { CustomerName = "Joe Brown" }),
new CheckedListItem<Customer>(new Customer() { CustomerName = "Herb Dean" }),
new CheckedListItem<Customer>(new Customer() { CustomerName = "fg4" }),
new CheckedListItem<Customer>(new Customer() { CustomerName = "fg5" }),
new CheckedListItem<Customer>(new Customer() { CustomerName = "fg6" }),
new CheckedListItem<Customer>(new Customer() { CustomerName = "fg7" }),
new CheckedListItem<Customer>(new Customer() { CustomerName = "fg8" }),
new CheckedListItem<Customer>(new Customer() { CustomerName = "fg9" }),
new CheckedListItem<Customer>(new Customer() { CustomerName = "fg10" }),
new CheckedListItem<Customer>(new Customer() { CustomerName = "fg11" }),
new CheckedListItem<Customer>(new Customer() { CustomerName = "fg12" }),
new CheckedListItem<Customer>(new Customer() { CustomerName = "fg13" }),
new CheckedListItem<Customer>(new Customer() { CustomerName = "fg14" })
};
Properties.Settings.Default.Save();
}
else
{
//var a = Properties.Settings.Default.Customers;
//Customers = ObservableCollection<CheckedListItem<>>;
}
}
public class CheckedListItem<T> : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private bool isChecked;
private T item;
public CheckedListItem()
{ }
public CheckedListItem(T item, bool isChecked = false)
{
this.item = item;
this.isChecked = isChecked;
}
public T Item
{
get { return item; }
set
{
item = value;
if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Item"));
}
}
public bool IsChecked
{
get { return isChecked; }
set
{
isChecked = value;
if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("IsChecked"));
}
}
}
private void Window_Closing(object sender, CancelEventArgs e)
{
Properties.Settings.Default.Save();
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Shapes;
使用系统组件模型;
使用System.Collections.ObjectModel;
命名空间WpfApplication1
{
///
///TestSettings.xaml的交互逻辑
///
公共部分类TestSettings:窗口
{
公共ObservableCollection客户{get;set;}
公共类客户
{
公共字符串CustomerName{get;set;}
}
公共测试设置()
{
初始化组件();
if(Properties.Settings.Default.Customers==null)
{
Properties.Settings.Default.Customers=新的ObservableCollection
{
新客户(){CustomerName=“Kelly Smith”},
新客户(){CustomerName=“Joe Brown”},
新客户(){CustomerName=“Herb Dean”}
//Add(newcheckedlistitem(newcustomer(){CustomerName=“Kelly Smith”}));
//Add(newcheckedlistitem(newcustomer(){CustomerName=“joebrown”}));
//Add(newcheckedlistitem(newcustomer(){CustomerName=“Herb Dean”}));
};
Properties.Settings.Default.Save();
}
其他的
{
//Customers=(ObservableCollection)Properties.Settings.Default.Customers;
}
}
公共类CheckedListItem:INotifyPropertyChanged
{
公共事件属性更改事件处理程序属性更改;
私人住宅被检查;
私人物品;
公共CheckedListItem()
{ }
公共CheckedListItem(T项,bool isChecked=false)
{
this.item=项目;
this.isChecked=isChecked;
}
公共交通项目
{
获取{return item;}
设置
{
项目=价值;
如果(PropertyChanged!=null)PropertyChanged(这是新的PropertyChangedEventArgs(“项目”);
}
}
公共场所被检查
{
获取{return isChecked;}
设置
{
isChecked=值;
如果(PropertyChanged!=null)PropertyChanged(这是新的PropertyChangedEventArgs(“IsChecked”);
}
}
}
私有无效窗口\u关闭(对象发送方,取消事件参数)
{
Properties.Settings.Default.Save();
}
}
}
Settings.Designer.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.Shapes;
using System.ComponentModel;
using System.Collections.ObjectModel;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for TestSettings.xaml
/// </summary>
public partial class TestSettings : Window
{
public ObservableCollection<CheckedListItem<Customer>> Customers { get; set; }
public class Customer
{
public string CustomerName { get; set; }
}
public TestSettings()
{
InitializeComponent();
if (Properties.Settings.Default.Customers == null)
{
Properties.Settings.Default.Customers = new ObservableCollection<Customer>
{
new Customer() { CustomerName = "Kelly Smith" },
new Customer() { CustomerName = "Joe Brown" },
new Customer() { CustomerName = "Herb Dean" }
//Customers.Add(new CheckedListItem<Customer>(new Customer(){CustomerName="Kelly Smith"}));
//Customers.Add(new CheckedListItem<Customer>(new Customer(){CustomerName="Joe Brown"}));
//Customers.Add(new CheckedListItem<Customer>(new Customer(){CustomerName="Herb Dean"}));
};
Properties.Settings.Default.Save();
}
else
{
// Customers = (ObservableCollection<Customer>)Properties.Settings.Default.Customers;
}
}
public class CheckedListItem<T> : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private bool isChecked;
private T item;
public CheckedListItem()
{ }
public CheckedListItem(T item, bool isChecked = false)
{
this.item = item;
this.isChecked = isChecked;
}
public T Item
{
get { return item; }
set
{
item = value;
if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Item"));
}
}
public bool IsChecked
{
get { return isChecked; }
set
{
isChecked = value;
if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("IsChecked"));
}
}
}
private void Window_Closing(object sender, CancelEventArgs e)
{
Properties.Settings.Default.Save();
}
}
}
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.269
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System.Collections.ObjectModel;
namespace WpfApplication1.Properties
{
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "10.0.0.0")]
internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase
{
private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
public static Settings Default
{
get
{
return defaultInstance;
}
}
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
public ObservableCollection<WpfApplication1.TestSettings.Customer> Customers
{
get
{
return ((ObservableCollection<WpfApplication1.TestSettings.Customer>)(this["Customers"]));
}
set
{
this["Customers"] = value;
}
}
}
}
using System.Collections.ObjectModel;
namespace WpfApplication1.Properties {
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "10.0.0.0")]
internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {
private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
public static Settings Default {
get {
return defaultInstance;
}
}
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
public ObservableCollection<WpfApplication1.TestSettings.CheckedListItem<WpfApplication1.TestSettings.Customer>> Customers
{
get
{
return ((ObservableCollection<WpfApplication1.TestSettings.CheckedListItem<WpfApplication1.TestSettings.Customer>>)(this["Customers"]));
}
set
{
this["Customers"] = value;
}
}
}
}
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.Shapes;
using System.ComponentModel;
using System.Collections.ObjectModel;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for TestSettings.xaml
/// </summary>
public partial class TestSettings : Window
{
public ObservableCollection<CheckedListItem<Customer>> Customers
{
get;
set;
}
public class Customer
{
public string CustomerName { get; set; }
}
public TestSettings()
{
InitializeComponent();
if (Properties.Settings.Default.Customers == null)
{
Properties.Settings.Default.Customers = new ObservableCollection<CheckedListItem<Customer>>
{
new CheckedListItem<Customer>(new Customer() { CustomerName = "Kelly Smith" }),
new CheckedListItem<Customer>(new Customer() { CustomerName = "Joe Brown" }),
new CheckedListItem<Customer>(new Customer() { CustomerName = "Herb Dean" }),
new CheckedListItem<Customer>(new Customer() { CustomerName = "fg4" }),
new CheckedListItem<Customer>(new Customer() { CustomerName = "fg5" }),
new CheckedListItem<Customer>(new Customer() { CustomerName = "fg6" }),
new CheckedListItem<Customer>(new Customer() { CustomerName = "fg7" }),
new CheckedListItem<Customer>(new Customer() { CustomerName = "fg8" }),
new CheckedListItem<Customer>(new Customer() { CustomerName = "fg9" }),
new CheckedListItem<Customer>(new Customer() { CustomerName = "fg10" }),
new CheckedListItem<Customer>(new Customer() { CustomerName = "fg11" }),
new CheckedListItem<Customer>(new Customer() { CustomerName = "fg12" }),
new CheckedListItem<Customer>(new Customer() { CustomerName = "fg13" }),
new CheckedListItem<Customer>(new Customer() { CustomerName = "fg14" })
};
Properties.Settings.Default.Save();
}
else
{
//var a = Properties.Settings.Default.Customers;
//Customers = ObservableCollection<CheckedListItem<>>;
}
}
public class CheckedListItem<T> : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private bool isChecked;
private T item;
public CheckedListItem()
{ }
public CheckedListItem(T item, bool isChecked = false)
{
this.item = item;
this.isChecked = isChecked;
}
public T Item
{
get { return item; }
set
{
item = value;
if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Item"));
}
}
public bool IsChecked
{
get { return isChecked; }
set
{
isChecked = value;
if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("IsChecked"));
}
}
}
private void Window_Closing(object sender, CancelEventArgs e)
{
Properties.Settings.Default.Save();
}
}
}
//------------------------------------------------------------------------------
//
//这段代码是由一个工具生成的。
//运行时版本:4.0.30319.269
//
//对此文件的更改可能会导致不正确的行为,如果
//重新生成代码。
//
//------------------------------------------------------------------------------
使用System.Collections.ObjectModel;
命名空间WpfApplication1.Properties
{
[全局::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
[全局::System.CodeDom.Compiler.GeneratedCodeAttribute(“Microsoft.VisualStudio.Editor.SettingsDesigner.SettingsSingleFileGenerator”,“10.0.0.0”)]
内部密封部分类设置:全局::System.Configuration.ApplicationSettingsBase
{
私有静态设置defaultInstance=((设置)(全局::System.Configuration.ApplicationSettingsBase.Synchronized(新设置()));
公共静态设置默认值
{
得到
{
返回默认实例;
}
}
[全局::System.Configuration.UserScopedSettingAttribute()]
[全局::System.Diagnostics.DebuggerNonUserCodeAttribute()]
公开收集客户
{
得到
{
退货((可观收货)(本[“客户]));
}
设置
{
这[“客户”]=价值;
}
}
}
}
正常-已排序
主要有两个问题
1/我只是添加了CustomerName,而不是CheckListItem,所以
observedcollection
应为observedcollection
2/再次添加客户
new Customer(){CustomerName=“Kelly-Smith”}
应该是new CheckedListItem(new Customer(){CustomerName=“Kelly-Smith”})
所以完整的工作代码是
Settings.Designer.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.Shapes;
using System.ComponentModel;
using System.Collections.ObjectModel;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for TestSettings.xaml
/// </summary>
public partial class TestSettings : Window
{
public ObservableCollection<CheckedListItem<Customer>> Customers { get; set; }
public class Customer
{
public string CustomerName { get; set; }
}
public TestSettings()
{
InitializeComponent();
if (Properties.Settings.Default.Customers == null)
{
Properties.Settings.Default.Customers = new ObservableCollection<Customer>
{
new Customer() { CustomerName = "Kelly Smith" },
new Customer() { CustomerName = "Joe Brown" },
new Customer() { CustomerName = "Herb Dean" }
//Customers.Add(new CheckedListItem<Customer>(new Customer(){CustomerName="Kelly Smith"}));
//Customers.Add(new CheckedListItem<Customer>(new Customer(){CustomerName="Joe Brown"}));
//Customers.Add(new CheckedListItem<Customer>(new Customer(){CustomerName="Herb Dean"}));
};
Properties.Settings.Default.Save();
}
else
{
// Customers = (ObservableCollection<Customer>)Properties.Settings.Default.Customers;
}
}
public class CheckedListItem<T> : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private bool isChecked;
private T item;
public CheckedListItem()
{ }
public CheckedListItem(T item, bool isChecked = false)
{
this.item = item;
this.isChecked = isChecked;
}
public T Item
{
get { return item; }
set
{
item = value;
if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Item"));
}
}
public bool IsChecked
{
get { return isChecked; }
set
{
isChecked = value;
if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("IsChecked"));
}
}
}
private void Window_Closing(object sender, CancelEventArgs e)
{
Properties.Settings.Default.Save();
}
}
}
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.269
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System.Collections.ObjectModel;
namespace WpfApplication1.Properties
{
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "10.0.0.0")]
internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase
{
private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
public static Settings Default
{
get
{
return defaultInstance;
}
}
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
public ObservableCollection<WpfApplication1.TestSettings.Customer> Customers
{
get
{
return ((ObservableCollection<WpfApplication1.TestSettings.Customer>)(this["Customers"]));
}
set
{
this["Customers"] = value;
}
}
}
}
using System.Collections.ObjectModel;
namespace WpfApplication1.Properties {
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "10.0.0.0")]
internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {
private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
public static Settings Default {
get {
return defaultInstance;
}
}
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
public ObservableCollection<WpfApplication1.TestSettings.CheckedListItem<WpfApplication1.TestSettings.Customer>> Customers
{
get
{
return ((ObservableCollection<WpfApplication1.TestSettings.CheckedListItem<WpfApplication1.TestSettings.Customer>>)(this["Customers"]));
}
set
{
this["Customers"] = value;
}
}
}
}
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.Shapes;
using System.ComponentModel;
using System.Collections.ObjectModel;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for TestSettings.xaml
/// </summary>
public partial class TestSettings : Window
{
public ObservableCollection<CheckedListItem<Customer>> Customers
{
get;
set;
}
public class Customer
{
public string CustomerName { get; set; }
}
public TestSettings()
{
InitializeComponent();
if (Properties.Settings.Default.Customers == null)
{
Properties.Settings.Default.Customers = new ObservableCollection<CheckedListItem<Customer>>
{
new CheckedListItem<Customer>(new Customer() { CustomerName = "Kelly Smith" }),
new CheckedListItem<Customer>(new Customer() { CustomerName = "Joe Brown" }),
new CheckedListItem<Customer>(new Customer() { CustomerName = "Herb Dean" }),
new CheckedListItem<Customer>(new Customer() { CustomerName = "fg4" }),
new CheckedListItem<Customer>(new Customer() { CustomerName = "fg5" }),
new CheckedListItem<Customer>(new Customer() { CustomerName = "fg6" }),
new CheckedListItem<Customer>(new Customer() { CustomerName = "fg7" }),
new CheckedListItem<Customer>(new Customer() { CustomerName = "fg8" }),
new CheckedListItem<Customer>(new Customer() { CustomerName = "fg9" }),
new CheckedListItem<Customer>(new Customer() { CustomerName = "fg10" }),
new CheckedListItem<Customer>(new Customer() { CustomerName = "fg11" }),
new CheckedListItem<Customer>(new Customer() { CustomerName = "fg12" }),
new CheckedListItem<Customer>(new Customer() { CustomerName = "fg13" }),
new CheckedListItem<Customer>(new Customer() { CustomerName = "fg14" })
};
Properties.Settings.Default.Save();
}
else
{
//var a = Properties.Settings.Default.Customers;
//Customers = ObservableCollection<CheckedListItem<>>;
}
}
public class CheckedListItem<T> : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private bool isChecked;
private T item;
public CheckedListItem()
{ }
public CheckedListItem(T item, bool isChecked = false)
{
this.item = item;
this.isChecked = isChecked;
}
public T Item
{
get { return item; }
set
{
item = value;
if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Item"));
}
}
public bool IsChecked
{
get { return isChecked; }
set
{
isChecked = value;
if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("IsChecked"));
}
}
}
private void Window_Closing(object sender, CancelEventArgs e)
{
Properties.Settings.Default.Save();
}
}
}
使用System.Collections.ObjectModel;
命名空间WpfApplication1.Properties{
[全局::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute(“Microsoft.VisualS