WPF, a multi-key value binding problem, a key binds an object on an interface

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.