我正在尝试在WPF中构建一个CheckedListBox并将其绑定到用户设置,以便它只构造一次,然后使用ListBox ItemsSource="{Binding Source={x:Static local:Settings.Default}, Path=Customers, Mode=TwoWay}
我可以看到复选框,但它们不会显示客户名称,也不会在设置中保留其值。有人能够发现原因吗?
我想我可能需要从设置转换为当前的Customers ObservableCollection但是无法使其正常工作?
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>
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();
}
}
}
Settings.Designer.cs
//------------------------------------------------------------------------------
// <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;
}
}
}
}
答案 0 :(得分:0)
好的 - 排序。
有两个主要问题
1 /我只是添加了CustomerName而不是CheckListItem
ObservableCollection<Customer>
应为ObservableCollection<CheckedListItem<Customer>>
2 /再次添加客户。
new Customer() { CustomerName = "Kelly Smith" }
应为new CheckedListItem<Customer>(new Customer() { CustomerName = "Kelly Smith" })
完整的工作代码是
<强> Settings.Designer.cs 强>
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;
}
}
}
}
<强> 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" 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<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();
}
}
}
编辑:看起来这确实有效,但由于Settings.Designer.cs
已被修改,因此设置不会在不同的运行时间之间保持不变,因为通用类型无法存储在设置中。而不是更改Settings.Designer.cs我最终创建了XML字符串,并将其作为字符串保存在设置中,然后构建ObservableCollection&gt;来自XML,反之亦然。