WPF Data Binding - Example of “Custom Type Descriptor”

前端 未结 2 854

I see several people saying that WPF can use \"Custom Type Descriptors\" for \"Change notification\".

The ways I know how to do Change Notification are:



        
2条回答
  •  感动是毒
    2020-12-13 17:18

    Here's a pretty simple example for you.

    Window1.xaml:

    
        
            
                
                
            
            
                
                
                
            
    
            Name:
            
    
            Age:
            
    
            
                
                    
                        
                        
                    
                
            
        
    
    

    Window1.xaml.cs:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Linq;
    using System.Windows;
    
    namespace CTDExample
    {
        public partial class Window1 : Window
        {
            public Window1()
            {
                InitializeComponent();
    
                var ctd = new CTD();
                ctd.AddProperty("Name");
                ctd.AddProperty("Age");
                DataContext = ctd;
            }
        }
    
        public class CTD : CustomTypeDescriptor
        {
            private static readonly ICollection _propertyDescriptors = new List();
    
            public void AddProperty(string name)
            {
                _propertyDescriptors.Add(new MyPropertyDescriptor(name));
            }
    
            public override PropertyDescriptorCollection GetProperties()
            {
                return new PropertyDescriptorCollection(_propertyDescriptors.ToArray());
            }
    
            public override PropertyDescriptorCollection GetProperties(Attribute[] attributes)
            {
                return GetProperties();
            }
    
            public override EventDescriptorCollection GetEvents()
            {
                return null;
            }
    
            public override EventDescriptorCollection GetEvents(Attribute[] attributes)
            {
                return null;
            }
        }
    
        public class MyPropertyDescriptor : PropertyDescriptor
        {
            private readonly IDictionary _values;
    
            public MyPropertyDescriptor(string name)
                : base(name, null)
            {
                _values = new Dictionary();
            }
    
            public override bool CanResetValue(object component)
            {
                throw new NotImplementedException();
            }
    
            public override Type ComponentType
            {
                get { throw new NotImplementedException(); }
            }
    
            public override object GetValue(object component)
            {
                object value = null;
                _values.TryGetValue(component, out value);
                return value;
            }
    
            public override bool IsReadOnly
            {
                get { return false; }
            }
    
            public override Type PropertyType
            {
                get { return typeof(object); }
            }
    
            public override void ResetValue(object component)
            {
                throw new NotImplementedException();
            }
    
            public override void SetValue(object component, object value)
            {
                var oldValue = GetValue(component);
    
                if (oldValue != value)
                {
                    _values[component] = value;
                    OnValueChanged(component, new PropertyChangedEventArgs(base.Name));
                }
            }
    
            public override bool ShouldSerializeValue(object component)
            {
                throw new NotImplementedException();
            }
    
            public override void AddValueChanged(object component, EventHandler handler)
            {
                // set a breakpoint here to see WPF attaching a value changed handler
                base.AddValueChanged(component, handler);
            }
        }
    }
    

提交回复
热议问题