Tag: 字典

当DataGrid发生更改时,为什么我的ViewModel中的属性没有更新?

我正在尝试创建一个UserControl,它允许我在网格中编辑Dictionary类型的Dictionary (只编辑条目到目前为止,不添加或删除)。 每当我将DataGrid绑定到Dictionary时,它将网格显示为只读,因此我决定创建一个值转换器,将其转换为ObservableCollection ,其中DictionaryEntry只是一个具有两个属性Key和Value 。 这适用于在网格中显示字典,但现在当我对网格进行更改时,我的字典没有被更新。 我不确定为什么。 我认为这是我设置绑定的方式或我的值转换器的问题。 如果有人能够发光,那就太棒了。 下面是我能做的最小的演示,它展示了我正在做的事情。 问题是当我更改网格中的值时,我的MainViewModel上的MyDictionary不会更新。 为什么? MainViewModel.cs public class MainViewModel : INotifyPropertyChanged { public MainViewModel() { _myDictionary = new Dictionary() { {“Key1”, “Value1”}, {“Key2”, “Value2”}, {“Key3”, “Value3”} }; } private Dictionary _myDictionary; public Dictionary MyDictionary { get { return _myDictionary; } set { if (_myDictionary == value) return; _myDictionary […]

使用Dictionary和HashSet的GetHashCode方法

我有一个关于Dictionary和HashSet如何在C#中工作的问题。 根据我的理解,GetHashCode用于哈希表以确定密钥唯一性。 在以下MSDN页面上,它指出: 哈希码是一个数值,用于插入和标识基于散列的集合中的对象,例如Dictionary类,Hashtable类或从DictionaryBase类派生的类型。 链接: MSDN Object.GetHashCode 如果是这种情况,为什么当包含与car1相同的哈希码时,ContainsKey和Contains会为car2返回false? 如果我的理解是正确的,如果MSDN说的是正确的,那么这两个都不应该返回真的吗? class Program { static void Main(string[] args) { // Create a Dictionary and HashSet Dictionary carDictionary = new Dictionary(); HashSet carSet = new HashSet(); // Create 3 Cars (2 generic and 1 Civic) Car car1 = new Car(); Car car2 = new Car(); Car car3 = […]

将Dictionary 成员序列化为XML元素和数据

我有一个可以序列化为XML的类“产品”。 我使用标准的System.Xml.Serialization.XmlSerializer来序列化,并使用XmlWriter“writer”对象将序列化结果写入StreamWriter对象。 序列化程序对象现在一次性序列化整个类: XmlSerializer serializer = new XmlSerializer(typeof(products)); serializer.Serialize(writer, products); 该类有一个名为’规范’的Dictionary 成员。 它是动态构建的,所以我事先不知道密钥。 以下是字典可能包含的数据示例(键:值): 颜色:蓝色 长度:110mm 宽度:55mm 我希望能够将该属性序列化为: … blue 110mm 55mm … 我知道这是糟糕的XML设计,但它必须符合第三方规范。 是否可以使用标准属性? 如果没有,我怎么能这样序列化字典? 如果您需要更多代码段,请告诉我们。 编辑 :由于需求的一些变化,我放开了Dictionary 。 相反,我创建了一个类“规范”: public class Specification { public string Name; public string Value; public bool IsOther; public Specification() : this(null, null, false) { } public Specification(string name, […]

如何从C#中的资源字典(XAML)中获取值

我正在开发一个WPF应用程序,用户可以在运行时更改语言(而不是当前的文化!)。 所以我有多个XAML类型的资源词典,我添加了文本来制作我的WPF-app多语言: First name Last name Validation with DataAnnotations German English The first name has to be inserted WPF窗口和控件受窗口资源的约束。 但我正在使用DataAnnotations进行validation。 我的第一个想法是在我的viewmodel中validation时将文本传递给键“insert_first_name”。 所以我试图通过使用它来获得它: System.Windows.Application.Current.Resources.FindName(“insert_first_name”) 但是当我使用FindName方法时,我得到null。 当我尝试 System.Windows.Application.Current.Resources.Contains(“insert_first_name”) 我得到“真实”,这意味着密钥存在。 我怎样才能获得钥匙的价值? protected void ValidateModel() { validationErrors.Clear(); ICollection validationResults = new List(); ValidationContext validationContext = new ValidationContext(personmodel, null, null); if (!Validator.TryValidateObject(personmodel, validationContext, validationResults, true)) { foreach (ValidationResult validationResult in […]

从对象字典C#中获取价值

我正在使用LiveConnect sdk来获取一些用户信息。 在做了必要的事情之后,这就是我得到的结果: { “id”: “123456789”, “name”: “ab”, “first_name”: “a”, “last_name”: “b”, “link”: “https://profile.live.com/”, “work”: [], “gender”: null, “emails”: { “preferred”: “a@live.com”, “account”: “a@live.com”, “personal”: null, “business”: null }, “addresses”: { “personal”: { “street”: null, “street_2”: null, “city”: null, “state”: null, “postal_code”: null, “region”: null }, “business”: { “street”: null, “street_2”: null, “city”: null, “state”: […]

项目已添加。 键入词典:

我有一个应用程序,当我尝试添加项目时挂起,当我检查跟踪文件我得到此条目 for (int i=0; i<objects.Count; i++) { DataModelObject dmo = (DataModelObject)objects.GetAt(i); sl.Add(dmo.Guid, dmo); } } 我不知道如何解决这个问题,任何机构都有任何建议

Newtonsoft.Json,Populate Dictionary失败

我通过Newtonsoft.json将字典序列化为json,并且代码如下: var serializeSettings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All, TypeNameAssemblyFormat = FormatterAssemblyStyle.Full, Formatting = Formatting.Indented }; var serializedObject = JsonConvert.SerializeObject(dic, serializeSettings); 这段代码生成一个像这样的json: { “$type”: “System.Collections.Generic.Dictionary`2[[System.Guid, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”, “9648af76-7986-4b34-8b2c-97b2345769ef”: “Test” } 我尝试通过以下代码将json反序列化为字典: var newDic = new Dictionay(); var deserializeSettings = new JsonSerializerSettings { TypeNameHandling = […]

如何在linq中展平字典<string,List >并将键保留在结果中

你如何在linq中实现以下目标? 我觉得应该有一个Linq替代方案。 var foods = new Dictionary<string, List>(); foods.Add(“Cake”, new List() { “Sponge”, “Gateux”, “Tart” }); foods.Add(“Pie”, new List() { “Mud”, “Apple” }); foods.Add(“Roll”, new List() { “Sausage” }); var result = new List<Tuple>(); foreach (var food in foods) { foreach (var detail in food.Value) { result.Add(new Tuple(food.Key, detail)); } } ie cake pie to […]

如何将(静态)字典绑定到标签?

我有一个静态字典 class X { static Dictionary MyDict {get { … }} } 此词典包含我想在网格控件中显示的数据: 1.)我不知道如何访问(在xaml中)字典 2.)我想将指定键的值绑定到Label的Content-Property。 这该怎么做?

.net字典使用多少个哈希桶?

我知道这是一个实现细节,但我很好奇:.NET字典中使用的散列桶的数量是否有限制? 我假设它会在2 * numberOfElements附近 ,但是有没有人确切知道(或者它是否记录在任何地方)?