Problem Description:
When using dictionary<key,value> to store data, it is sometimes necessary to bind a key on the interface to display value, at which point there are two ways of thinking:
One is to write a custom extension class, similar to binding, which is named "Mybinding", which implements the binding of key within the binding class.
Another more concise, more general approach is to implement binding with an index. Properties can be bound to the interface, and the same index can be bound to the interface.
The implementation code is as follows:
1. Custom MarkupExtension,
Using system;using system.windows.data;using System.windows.markup;namespace Index Binding {public class mybinding: MarkupExtension {public int key {get; set;} public override Object ProvideValue (IServiceProvider serviceprovider) { var b = new Binding ("Value"); B.source = Viewmodelnomal.instance.li[key]; Return B.providevalue (serviceprovider);}}
2. Index binding
public class modeluseindexer:inotifypropertychanged { private readonly Dictionary<int, Int> _ Localdictionary = new Dictionary<int, int> (); [IndexerName ("Item")] public int This[int Index] { get { int result; _localdictionary.trygetvalue (index, out result); return result; } Set { if (_localdictionary.containskey (index)) _localdictionary[index] = value; else _localdictionary.add (index, value); if (propertychanged! = null) propertychanged (This, new PropertyChangedEventArgs ("item[]"));} } public event PropertyChangedEventHandler propertychanged; }
The effect is the same, but the index binding relies less on code and more in line with OOP ideas.
Source Address: Https://files.cnblogs.com/files/lizhijian/%E7%B4%A2%E5%BC%95%E7%BB%91%E5%AE%9A.rar
Thank you for reading and hope to help you.
WPF, a multi-key value binding problem, a key binds an object on an interface