コード例 #1
0
        /// <summary>
        /// Binds an action to a specific property.
        /// </summary>
        /// <param name="propertyName">The property to bind against</param>
        /// <param name="action">The action to be called</param>
        public void Bind(string propertyName, EventHandler <EventArgs> action)
        {
            if (!ValidatePublicPropertyNames(CallerType, propertyName))
            {
                throw new ArgumentException("Invalid property name");
            }

            Delegate list;

            if (!_links.TryGetValue(propertyName, out list))
            {
                _links[propertyName] =
                    WeakReferenceEventHandler.MakeWeakHandler(
                        action,
                        rem =>
                {
                    var handler = (EventHandler <EventArgs>)_links[propertyName];
                    handler    -= rem;
                });
            }
            else
            {
                _links[propertyName] = (EventHandler <EventArgs>)list +
                                       WeakReferenceEventHandler.MakeWeakHandler(
                    action,
                    rem =>
                {
                    var handler = (EventHandler <EventArgs>)_links[propertyName];
                    handler    -= rem;
                });
            }
        }
コード例 #2
0
        /// <summary>
        /// Creates an instance
        /// </summary>
        /// <param name="source">The source collection</param>
        /// <param name="filter">The filter delegate</param>
        public FilteredReadOnlyObservableCollection(ObservableCollection <Type> source, Func <Type, bool> filter)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }
            if (filter == null)
            {
                throw new ArgumentNullException("filter");
            }

            _ignoreSourceChangeEvents = false;
            Source   = source;
            _filter  = filter;
            _handler = WeakReferenceEventHandler.MakeWeakPropertyChangedHandler(Handler, (item, rem) => { item.PropertyChanged -= rem; });
            Filtered = new ObservableCollection <Type>(source.Where(filter));
            source.CollectionChanged   += source_CollectionChanged;
            Filtered.CollectionChanged += filtered_CollectionChanged;

            foreach (var item in Source)
            {
                if (item is INotifyPropertyChanged)
                {
                    ((INotifyPropertyChanged)item).PropertyChanged += _handler;
                }
            }
        }