我正在尝试在C#中为WPF DataBinding创建一个Observable Dictionary Class.我在这里找到了Andy的一个很好的例子:双向数据绑定在WPF中的字典
据此,我试图将代码更改为以下内容:
class ObservableDictionary : ViewModelBase
{
public ObservableDictionary(Dictionary<TKey, TValue> dictionary)
{
_data = dictionary;
}
private Dictionary<TKey, TValue> _data;
public Dictionary<TKey, TValue> Data
{
get { return this._data; }
}
private KeyValuePair<TKey, TValue>? _selectedKey = null;
public KeyValuePair<TKey, TValue>? SelectedKey
{
get { return _selectedKey; }
set
{
_selectedKey = value;
RaisePropertyChanged("SelectedKey");
RaisePropertyChanged("SelectedValue");
}
}
public TValue SelectedValue
{
get
{
return _data[SelectedKey.Value.Key];
}
set
{
_data[SelectedKey.Value.Key] = value;
RaisePropertyChanged("SelectedValue");
}
}
}
Run Code Online (Sandbox Code Playgroud)
}
不幸的是我仍然不知道如何传递"一般"字典对象..任何想法? …
我正在制作一个可观察的课程.Add方法工作正常.但后来我试图调用Remove()方法我得到这个错误:
"添加的项目不会出现在给定的索引'0'"
但是我将NotifyCollectionChangedAction枚举设置为Remove,如下面的代码所示.
public class ObservableOrderResponseQueue : INotifyCollectionChanged, IEnumerable<OrderResponse>
{
public event NotifyCollectionChangedEventHandler CollectionChanged;
private List<OrderResponse> _list = new List<OrderResponse>();
public void Add(OrderResponse orderResponse)
{
this._list.Add(orderResponse);
if (CollectionChanged != null)
{
CollectionChanged(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, orderResponse, 0));
}
}
public void RemoveAt(int index)
{
OrderResponse order = this._list[index];
this._list.RemoveAt(index);
if (CollectionChanged != null)
{
CollectionChanged(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, order, index));
}
}
public void Remove(OrderResponse orderResponse)
{
var item = _list.Where(o => o.OrderDetail.TrayCode == orderResponse.OrderDetail.TrayCode).FirstOrDefault();
int index = _list.IndexOf(item);
this._list.RemoveAt(index);
if (CollectionChanged …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用ObservableDictionary的以下实现:ObservableDictionary(C#).
当我将字典绑定到DataGrid时使用以下代码:
ObserveableDictionary<string,string> dd=new ObserveableDictionary<string,string>();
....
dd["aa"]="bb";
....
dd["aa"]="cc";
Run Code Online (Sandbox Code Playgroud)
在dd["aa"]="cc";我得到以下异常
Index was out of range. Must be non-negative and less than the size of the
collection. Parameter name: index
Run Code Online (Sandbox Code Playgroud)
CollectionChanged(this, new NotifyCollectionChangedEventArgs(action, newItem, oldItem)在以下方法中引发此异常:
private void OnCollectionChanged(NotifyCollectionChangedAction action, KeyValuePair<TKey, TValue> newItem, KeyValuePair<TKey, TValue> oldItem)
{
OnPropertyChanged();
if (CollectionChanged != null) CollectionChanged(this, new NotifyCollectionChangedEventArgs(action, newItem, oldItem));
}
Run Code Online (Sandbox Code Playgroud)
这个index参数似乎对应于KeyValuePair<TKey, TValue> oldItem.
如何KeyValuePair<TKey, TValue>超出范围,我该怎么做才能使这项工作?
该任务的Parallels额外扩展发表于2010年,从那时起没有更新已被释放.
我在3年前在Nuget上将此代码作为DLL发布,并且已经有超过16,000次下载,这是对代码感兴趣的指标.
TPL Extras是否已被任何新技术取代?如果是这样,我想适当地注释Nuget描述.
c# parallel-processing concurrency multithreading task-parallel-library
我正在使用RestSharp与远程服务器通信.我收到一个JSON序列化字符串,我可以反序列化为ac#对象.我也能够将json数组反序列化为List.但是,我希望这些对象在WPF绑定中使用,所以我需要将它们放在ObservableCollection中以方便使用.但是,如果我尝试将属性从List更改为ObservableCollection(或IList,或ICollection或Collection),则会在反序列化时出现异常.
Unable to cast object of type 'RestSharp.JsonArray' to type 'System.Collections.Generic.IDictionary`2[System.String,System.Object]
Run Code Online (Sandbox Code Playgroud)
底层代码真的不是特别的,但无论如何它在这里:
private ObservableCollection<StationDto> stations;
[JsonProperty(PropertyName = "stations")]
public ObservableCollection<StationDto> Stations
{
get { return this.stations; }
set
{
this.stations = value;
RaisePropertyChanged(() => Stations);
}
}
Run Code Online (Sandbox Code Playgroud)
我知道接口不起作用,因为Json.net需要一个具体的类来序列化.
我已经做了相当多的谷歌搜索,但我还没有看到解决方案.是否有一种常用于json/rest服务的手工代理的模式?
如果将新的键\值对添加到静态字典中,我想在其他线程中执行某些操作.一种天真的方法是以频繁的间隔进行轮询,并检查ContainsKey(givenKey),但我想更快,而不是延迟轮询.
c# ×6
wpf ×3
dictionary ×2
.net-4.0 ×1
binding ×1
concurrency ×1
data-binding ×1
json.net ×1
restsharp ×1