我不知道该怎么尽可能管理_namePrefixes此控件做。我知道我可以使它成为非静态的,但在我的项目的内容方面,在这个控件的所有用途上保持一致是很有意义的。此外,我选择的ObservableCollection因以下情形的:如何防止在静态ObservableCollection线程安全中添加重复值?
我有2个客户机,一个用于标准用途,其他用于管理选项(管理员),如名称前缀列表。如果客户端正在运行并且管理员进行了更改,则客户端应该自行更新并在已加载后反映这些更改。哦,因为这是一个WPF项目,我想将它绑定到ListBox。如果这些都不会让我使用ObserableCollection,那么没什么大不了的......我会使用类似List的东西,但我认为这不会改变原来的问题。
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace MyProject
{
public class NameField : TextBox
{
private static ObservableCollection<NamePrefix> _namePrefixes;
private static ObservableCollection<NameSuffix> _nameSuffixes;
static NameField()
{
_namePrefixes = new ObservableCollection<NamePrefix>();
_nameSuffixes = new ObservableCollection<NameSuffix>();
}
public static void AddNamePrefix(Int32 id, String prefix)
{
//TODO: WHAT DO I DO HERE!?
}
}
/// <summary>
/// A Key/Value structure containing a Name Prefix ID and String value.
/// </summary>
public struct NamePrefix
{
#region Constructor
public NamePrefix(Int32 id, String prefix)
: this()
{
ID = id;
Prefix = prefix;
}
#endregion
#region Properties (ID, Prefix)
public Int32 ID { get; set; }
public String Prefix { get; set; }
#endregion
}
/// <summary>
/// A Key/Value structure containing a Name Suffix ID and String value.
/// </summary>
public struct NameSuffix
{
#region Constructor
public NameSuffix(Int32 id, String suffix)
: this()
{
ID = id;
Suffix = suffix;
}
#endregion
#region Properties (ID, Prefix)
public Int32 ID { get; set; }
public String Suffix { get; set; }
#endregion
}
}
请不要无意义的内容(如空白区域陈述或者,说实话,'#region'在所有语句)在发布代码。 – 2010-06-25 18:48:11
是什么问题? – 2010-06-25 19:32:20