我有一个完全懒惰的单身人士和胜利表格,
我想将控件绑定到singeton实例中的属性
我有一个组合框,其形式有牵引方式绑定到单身人士的财产
组合框有一个数据源(列表),当表单加载并且组合框被选中的项目填充时,所选项目不会在单例属性中更新。
例如,如果加载时组合框的选择值为“1”,则单例属性将不会获得此值并保持为空,如果之后我将在组合框中选择不同的值,它将正确更新属性。
有没有办法来减轻它?
avilibaleLinesComboBox.DataBindings.Add("SelectedItem", Settings.Instance,
"CallBackExtn", true, DataSourceUpdateMode.OnPropertyChanged);
[Serializable()]
public class Settings
{
private static volatile Settings instance;
private Settings() { }
public static Settings Instance
{
get
{
if (instance == null)
{
lock (syncRoot)
{
if (instance == null)
instance = loadSettingsFromFile();
}
}
return instance;
}
}
[System.Xml.Serialization.XmlElementAttribute("ConnectionString")]
public string ConnectionString { get; set; }
}
答案 0 :(得分:0)
我认为您必须在班级上实施INotifyPropertyChanged界面。
像这样(注意:检查序列化/反序列化是否仍按预期工作):
[Serializable()]
public class Settings : INotifyPropertyChanged {
private static volatile Settings instance;
private Settings() { }
public static Settings Instance {
get {
if (instance == null) {
lock (syncRoot) {
if (instance == null)
instance = loadSettingsFromFile();
}
}
return instance;
}
}
private string _connectionString;
[System.Xml.Serialization.XmlElementAttribute("ConnectionString")]
public string ConnectionString {
get { return _connectionString; }
set {
if (value != _connectionString) {
_connectionString = value;
OnPropertyChanged("ConnectionString");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName) {
var propertyChanged = PropertyChanged;
if (propertyChanged != null) {
propertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}