Ejemplo n.º 1
0
        private void OnDataContextPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (_subObservers.ContainsKey(e.PropertyName))
            {
                var propInfo    = _dataContext != null && _propInfoCache.ContainsKey(e.PropertyName) ? _propInfoCache[e.PropertyName] : null;
                var dataContext = _dataContext != null && propInfo.CanRead ? propInfo.GetValue(_dataContext, null) : null;
                _subObservers[e.PropertyName].DataContext = dataContext;
            }
            if (!_nameRefs.ContainsKey(e.PropertyName))
            {
                // do not raise events for non-registered path (sub observer will do it)
                return;
            }
            var handler = PropertyChanged;

            if (handler != null && _nameRefs.ContainsKey(e.PropertyName))
            {
                var propInfo = _dataContext != null && _propInfoCache.ContainsKey(e.PropertyName) ? _propInfoCache[e.PropertyName] : null;
                var args     = new ObserverPropertyChangedEventArgs(
                    e.PropertyName,
                    DataContext,
                    propInfo);
                handler(this, args);
            }
        }
Ejemplo n.º 2
0
 private void Observer_OnPropertyChanged(object sender, ObserverPropertyChangedEventArgs e)
 {
     _targetBindings
     .SelectMany(pair => pair.Value.Select(arg => new { Target = pair.Key, Binding = arg, }))
     .Where(arg => arg.Binding.SourcePath == e.Path)
     .ToList()
     .ForEach(arg => UpdateTarget(arg.Target, arg.Binding));
 }
Ejemplo n.º 3
0
        private void SubObserver_OnPropertyChanged(object sender, ObserverPropertyChangedEventArgs e)
        {
            var handler = PropertyChanged;

            if (handler != null)
            {
                var subObserver = sender as BindingObserver;
                var args        = new ObserverPropertyChangedEventArgs(
                    subObserver.Name + "." + e.Path,
                    e.DataContext,
                    e.PropertyInfo);
                handler(this, args);
            }
        }
Ejemplo n.º 4
0
        public void Register(string path)
        {
            var name     = path;
            var subName  = string.Empty;
            var dotIndex = path.IndexOf('.');

            if (dotIndex >= 0)
            {
                name = path.Substring(0, dotIndex);
                var subPos = dotIndex + 1;
                subName = path.Substring(subPos, path.Length - subPos);
            }
            if (subName.Length > 0)
            {
                if (!_subObservers.ContainsKey(name))
                {
                    OnCacheAdd(name);
                    var subObserver = new BindingObserver();
                    subObserver.Name = name;

                    var propInfo    = _dataContext != null && _propInfoCache.ContainsKey(name) ? _propInfoCache[name] : null;
                    var dataContext = _dataContext != null && propInfo.CanRead ? propInfo.GetValue(_dataContext, null) : null;
                    subObserver.DataContext = _dataContext != null && propInfo.CanRead ? propInfo.GetValue(_dataContext, null) : null;

                    subObserver.PropertyChanged += SubObserver_OnPropertyChanged;
                    _subObservers[name]          = subObserver;
                }
                _subObservers[name].Register(subName);
            }
            else
            {
                if (!_nameRefs.ContainsKey(name))
                {
                    _nameRefs[name] = 0;
                }
                _nameRefs[name]++;
                OnCacheAdd(name);

                // init
                var handler = PropertyChanged;
                if (handler != null)
                {
                    var args = new ObserverPropertyChangedEventArgs(name, DataContext, GetProperty(name));
                    handler(this, args);
                }
            }
        }