/// <summary>
            /// Handles the property changed.
            /// </summary>
            /// <param name="sender">The sender.</param>
            /// <param name="args">The <see cref="System.ComponentModel.PropertyChangedEventArgs"/> instance containing the event data.</param>
            public void HandlePropertyChanged(object sender, PropertyChangedEventArgs args)
            {
                INotifyPropertyChanged target = sender as INotifyPropertyChanged;

                if (target == null)
                {
                    throw new NotSupportedException("WeakSource can only work on INotifyPropertyChanged");
                }

                string name = args.PropertyName;

                if (!Actions.ContainsKey(name))
                {
                    return;
                }

                if (IsAlive)
                {
                    NotifyPropertyChanged(target, name);
                }
                else
                {
                    target.PropertyChanged -= new PropertyChangedEventHandler(HandlePropertyChanged);
                    Targets.Remove(target.GetHashCode());
                }
            }
        public void RemoveBindings(INotifyPropertyChanged dataSource)
        {
            if (dataSource != null)
            {
                string        sKey = dataSource.GetHashCode().ToString();
                List <string> keys = new List <string>();
                foreach (KeyValuePair <string, List <DataBindingCtrlInfo> > kv in m_DTDataSource2ControlProperty)
                {
                    if (kv.Key.StartsWith(sKey))
                    {
                        keys.Add(kv.Key);
                    }
                }

                if (keys.Count > 0)
                {
                    foreach (string key in keys)
                    {
                        m_DTDataSource2ControlProperty.Remove(key);
                    }
                }

                if (m_RegisteredDataSource.Contains(dataSource))
                {
                    dataSource.PropertyChanged -= new PropertyChangedEventHandler(dataSource_PropertyChanged);
                    m_RegisteredDataSource.Remove(dataSource);
                }
            }
        }
            /// <summary>
            /// Registers the specified source.
            /// </summary>
            /// <param name="source">The source.</param>
            /// <param name="target">The target.</param>
            /// <param name="action">The action.</param>
            /// <param name="targetProp">The target prop.</param>
            /// <param name="converter">The converter.</param>
            /// <param name="parameter">The converter parameter.</param>
            /// <returns></returns>
            public static WeakSource Register(Object source, INotifyPropertyChanged target, Delegate action, string targetProp, IDataConverter converter = null, object parameter = null)
            {
                WeakSource wSource = _weakSources.ContainsKey(source.GetHashCode()) ? _weakSources[source.GetHashCode()] : null;

                if (wSource == null)
                {
                    wSource = new WeakSource(source);
                    _weakSources.Add(source.GetHashCode(), wSource);
                }

                IList <WeakAction> actions;

                if (wSource.Actions.ContainsKey(targetProp))
                {
                    actions = wSource.Actions[targetProp];
                }
                else
                {
                    actions = new List <WeakAction>();
                    wSource.Actions.Add(targetProp, actions);
                }
                actions.Add(new WeakAction(action, converter, parameter));

                IList <string> props;

                if (!wSource.Targets.ContainsKey(target.GetHashCode()))
                {
                    props = new List <string>();
                    props.Add(targetProp);
                    wSource.Targets.Add(target.GetHashCode(), props);
                    target.PropertyChanged += new PropertyChangedEventHandler(wSource.HandlePropertyChanged);
                }
                else
                {
                    props = wSource.Targets[target.GetHashCode()];
                    if (!props.Contains(targetProp))
                    {
                        props.Add(targetProp);
                    }
                }
                return(wSource);
            }
Beispiel #4
0
            /// <summary>
            /// Registers the specified source.
            /// </summary>
            /// <param name="source">The source.</param>
            /// <param name="target">The target.</param>
            /// <param name="action">The action.</param>
            /// <param name="targetProp">The target prop.</param>
            /// <param name="converter">The converter.</param>
            /// <param name="parameter">The converter parameter.</param>
            /// <returns></returns>
            public static WeakSource Register(Object source, INotifyPropertyChanged target, Delegate action, string targetProp, IDataConverter converter = null, object parameter = null)
            {
                WeakSource wSource = _weakSources.ContainsKey(source.GetHashCode()) ? _weakSources[source.GetHashCode()] : null;
                if (wSource == null)
                {
                    wSource = new WeakSource(source);
                    _weakSources.Add(source.GetHashCode(), wSource);
                }

                IList<WeakAction> actions;
                if (wSource.Actions.ContainsKey(targetProp))
                {
                    actions = wSource.Actions[targetProp];
                }
                else
                {
                    actions = new List<WeakAction>();
                    wSource.Actions.Add(targetProp, actions);
                }
                actions.Add(new WeakAction(action, converter, parameter));

                IList<string> props;
                if (!wSource.Targets.ContainsKey(target.GetHashCode()))
                {
                    props = new List<string>();
                    props.Add(targetProp);
                    wSource.Targets.Add(target.GetHashCode(), props);
                    target.PropertyChanged += new PropertyChangedEventHandler(wSource.HandlePropertyChanged);
                }
                else
                {
                    props = wSource.Targets[target.GetHashCode()];
                    if (!props.Contains(targetProp))
                    {
                        props.Add(targetProp);
                    }
                }
                return wSource;
            }
 private string GetKey(INotifyPropertyChanged dataSource, string dataMember)
 {
     return(string.Format("{0}#{1}", dataSource.GetHashCode(), dataMember));
 }