gpt4 book ai didi

c# - 将 CheckedListBox 绑定(bind)到设置

转载 作者:太空宇宙 更新时间:2023-11-03 16:25:40 25 4
gpt4 key购买 nike

我正在尝试在 WPF 中构建一个 CheckedListBox 并将其绑定(bind)到用户设置,以便仅构建一次,然后使用 ListBox ItemsSource="{Binding Source={x:静态本地:Settings.Default},Path=Customers,Mode=TwoWay

我可以看到复选框,但它们没有显示客户名称,也没有在设置中保留它们的值。有谁能找出原因吗?

我想我可能需要从设置中转换为当前的 Customers ObservableCollection,但无法正常工作?

客户 = 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;
}
}

}
}

最佳答案

确定 - 已排序。

主要有两个问题

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 字符串,该字符串作为字符串保存在设置中,然后从 XML 构建 ObservableCollection>,反之亦然。

关于c# - 将 CheckedListBox 绑定(bind)到设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12656510/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com