public static void SubscribeToPropertyChange <T>(this T objectThatNotifies, Expression <Func <T, object> > property, PropertyChangedHandler <T> handler) where T : INotifyPropertyChanged { PropertyInfo info = objectThatNotifies.GetPropertyInfo(property); subscribe(objectThatNotifies, info, handler); }
/// <summary> /// Subscribe to changes in an object implementing INotifiyPropertyChanged. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="objectThatNotifies">The object you are interested in.</param> /// <param name="property">The property you are interested in.</param> /// <param name="handler">The delegate that will handle the event.</param> public static void SubscribeToChange <T>(this T objectThatNotifies, Expression <Func <object> > property, PropertyChangedHandler <T> handler) where T : INotifyPropertyChanged { // Add a new PropertyChangedEventHandler objectThatNotifies.PropertyChanged += (s, e) => { // Get name of Property var lambda = property as LambdaExpression; MemberExpression memberExpression; if (lambda.Body is UnaryExpression) { var unaryExpression = lambda.Body as UnaryExpression; memberExpression = unaryExpression.Operand as MemberExpression; } else { memberExpression = lambda.Body as MemberExpression; } var propertyInfo = memberExpression.Member as PropertyInfo; // Notify handler if PropertyName is the one we were interested in if (e.PropertyName.Equals(propertyInfo.Name)) { handler(objectThatNotifies); } }; }
protected void BuildCategoriesTree(ItemsList contents, TreeItem parentItem, ICollection <string> categories, ICollection <string> allSelectedCategories, IDictionary <string, ICollection <string> > categories2Children, IDictionary <string, MediaCategory> allCategories) { contents.Clear(); List <string> categoriesSorted = new List <string>(categories); categoriesSorted.Sort(); foreach (string mediaCategory in categoriesSorted) { TreeItem categoryItem = new TreeItem(Consts.KEY_NAME, mediaCategory); categoryItem.AdditionalProperties[Consts.KEY_MEDIA_CATEGORY] = allCategories[mediaCategory]; categoryItem.AdditionalProperties[Consts.KEY_PARENT_ITEM] = parentItem; if (allSelectedCategories.Contains(mediaCategory)) { categoryItem.Selected = true; } PropertyChangedHandler handler = (property, oldValue) => OnMediaCategoryItemSelectionChanged(categoryItem); categoryItem.SelectedProperty.Attach(handler); categoryItem.AdditionalProperties["HandlerStrongReference"] = handler; // SelectedProperty only manages weak references to its handlers -> avoid handler to be removed ICollection <string> childCategories; if (!categories2Children.TryGetValue(mediaCategory, out childCategories)) { childCategories = new List <string>(); } BuildCategoriesTree(categoryItem.SubItems, categoryItem, childCategories, allSelectedCategories, categories2Children, allCategories); contents.Add(categoryItem); } }
public Listener() { handler = new PropertyChangedHandler(toBeListenedObject) { { "PropertyA", DoA }, { "PropertyB", DoB } }; }
private static void AddHandler(INotifyPropertyChanged dataContext, TextBlock textBlock) { if (dataContext == null) return; var propertyChangedHandler = new PropertyChangedHandler(textBlock); dataContext.PropertyChanged += propertyChangedHandler.PropertyChanged; Handlers[dataContext] = propertyChangedHandler; }
public override void Fire(object oldValue) { PropertyChangedHandler dlgt = _eventDelegate; if (dlgt != null) { dlgt(this, oldValue); } }
protected void OnPropertyChanged(String propName, Object oldValue, Object newValue) { PropertyChangedHandler handler = PropertyChanged; if (handler != null) { handler(this, new PropertyChangedHandlerEventArgs(propName, oldValue, newValue)); } }
private static void subscribe <T>(T objectThatNotifies, PropertyInfo info, PropertyChangedHandler <T> Handler) where T : INotifyPropertyChanged { objectThatNotifies.PropertyChanged += (s, e) => { if (e.PropertyName.Equals(info.Name)) { Handler(objectThatNotifies); } }; }
internal void SetPropertyChanged(bool bPropertyChanged) { if (bPropertyChanged) { _OnPropertyChanged += OnEntityPropertyChanged; } else { _OnPropertyChanged -= OnEntityPropertyChanged; } }
internal void SetMappingPropertyChanged(bool bMappingPropertyChanged) { if (bMappingPropertyChanged) { _OnMappingPropertyChanged += OnEntityMappingPropertyChanged; } else { _OnMappingPropertyChanged -= OnEntityMappingPropertyChanged; } }
/// <summary> /// Creates am instance of the class /// </summary> /// <param name="defaultValue">Default for non existing key-value pairs</param> internal PropertyBagCollection(TValue defaultValue, PropertyChangedHandler propertyChangedHandler = null, KeyValuePair <string, TValue>[] initialValues = null) { DefaultValue = defaultValue; PropertyChangedHandler = propertyChangedHandler; if (null != initialValues) { foreach (KeyValuePair <string, TValue> item in initialValues) { this[item.Key] = item.Value; } } }
protected void EmitPropertyEvent(string name) { // Make a local assignment of the handler here to prevent // any race conditions if the PropertyChanged value changes // to null after the != null check. PropertyChangedHandler handler = PropertyChanged; if (handler != null) { handler(this, name); } }
public int MatchingBackingFieldAndPropertyReturnType(object initialValue, object value) { //Arrange. NpcClass npc = new NpcClass(initialValue); PropertyChangedHandler handler = new PropertyChangedHandler(npc); //Act. npc.Data = value; //Assert. return(handler.EventCount); }
public int SaveNonNullableInNullable(DateTime?initialValue, DateTime value) { //Arrange. NullableNpcClass npc = new NullableNpcClass(initialValue); PropertyChangedHandler handler = new PropertyChangedHandler(npc); //Act. npc.Data = value; //Assert. return(handler.EventCount); }
/// <summary> /// BaseEntity 构造方法 /// </summary> /// <param name="bPropertyChanged">是否记录值改变事件</param> /// <param name="bMappingPropertyChanged">是否记录MAPPING改变信息</param> internal BaseEntity(bool bPropertyChanged, bool bMappingPropertyChanged) { _ChangedMappingProperties = new Dictionary <string, object>(); _ChangedProperties = new Dictionary <string, object>(); if (bPropertyChanged) { _OnPropertyChanged += OnEntityPropertyChanged; } if (bMappingPropertyChanged) { _OnMappingPropertyChanged += OnEntityMappingPropertyChanged; } }
/// <summary> /// Subscribes to property notifications for the current binding /// </summary> /// <param name="action">The action to execute when new values are raised</param> /// <returns>A disposable to be called when the subscription is disposed.</returns> private IDisposable SubscribeToPropertyChanged(PropertyChangedHandler action) { var disposable = new CompositeDisposable(); foreach (var handler in _propertyChangedHandlers) { object previousValue = null; Action updateProperty = () => { var newValue = GetSourceValue(); action(previousValue, newValue, shouldRaiseValueChanged: true); previousValue = newValue; }; Action disposeAction = () => { action(previousValue, DependencyProperty.UnsetValue, shouldRaiseValueChanged: false); }; var handlerDisposable = handler(_dataContextWeakStorage, PropertyName, updateProperty); if (handlerDisposable != null) { previousValue = GetSourceValue(); // We need to keep the reference to the updatePropertyHandler // in this disposable. The reference is attached to the source's // object lifetime, to the target (bound) object. // // The registerations made by _propertyChangedHandlers are all // weak with regards to the delegates that are provided. disposable.Add(() => updateProperty = null); disposable.Add(handlerDisposable); disposable.Add(disposeAction); } } return(disposable); }
/// <summary> /// Subscribe to changes in an object implementing INotifiyPropertyChanged. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="ObjectThatNotifies">The object you are interested in.</param> /// <param name="Property">The property you are interested in.</param> /// <param name="Handler">The delegate that will handle the event.</param> public static PropertyChangedEventHandler SubscribeToChange <T> (this T ObjectThatNotifies, Expression <Func <object> > Property, PropertyChangedHandler <T> Handler) where T : INotifyPropertyChanged { string propertyName = GetPropertyName(Property); PropertyChangedEventHandler evh = (s, e) => { if (e.PropertyName.Equals(propertyName)) { Handler(ObjectThatNotifies); } }; ObjectThatNotifies.PropertyChanged += evh; return(evh); }
/// <summary> /// Detaches the specified event handler. /// </summary> /// <param name="handler">The handler.</param> public abstract void Detach(PropertyChangedHandler handler);
/// <summary> /// Attaches an event handler. The event handler will be bound with a weak reference. /// </summary> /// <param name="handler">The handler.</param> public override void Attach(PropertyChangedHandler handler) { _eventDelegate.Attach(handler); }
public static void AddChangeHandler <T>(this T element, DependencyProperty dependencyProperty, PropertyChangedHandler <T> handler) where T : FrameworkElement { var key = element; IDependencyPropertySubscription subscription = null; if (ExpressionSubscriptions.ContainsKey(key)) { subscription = ExpressionSubscriptions[key]; } else { subscription = new DependencyPropertySubscription <T>((T)element, dependencyProperty); ExpressionSubscriptions[key] = subscription; } ((INotifyPropertyChanged)subscription).SubscribeToChange(() => subscription.Value, o => handler((T)element)); }
public static void AddChangeHandler <T>(this T element, Expression <Func <object> > expression, PropertyChangedHandler <T> handler) where T : FrameworkElement { string propertyPath; var propertyInfo = expression.GetPropertyInfo(); var dependencyProperty = DependencyPropertyHelper.GetDependencyPropertyByPropertyName(element, propertyInfo.Name, out propertyPath); AddChangeHandler(element, dependencyProperty, handler); }
private void Settings_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) { switch (e.PropertyName) { case nameof(PrefixListText): PrefixList = PrefixListText.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries); break; case nameof(UserBlacklistText): UserBlacklist = UserBlacklistText.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries); break; case nameof(UserNicknamesText): UserNicknames = new Dictionary <string, string>( UserNicknamesText .Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries) .Select(x => x.Split(new[] { ':' }, StringSplitOptions.RemoveEmptyEntries)) .ToDictionary(x => x[0], y => y[1]), StringComparer.OrdinalIgnoreCase); break; // For every class we have to call this pattern case nameof(SubscriberNotification): SubscriberNotification.PropertyChanged += (s, e) => Safe(this); SubscriberNotification.PropertyChanged += (s, e) => PropertyChangedHandler.Invoke(this, new System.ComponentModel.PropertyChangedEventArgs(nameof(Settings))); break; case nameof(RaidNotification): RaidNotification.PropertyChanged += (s, e) => Safe(this); RaidNotification.PropertyChanged += (s, e) => PropertyChangedHandler.Invoke(this, new System.ComponentModel.PropertyChangedEventArgs(nameof(Settings))); break; case nameof(UserJoinedNotification): UserJoinedNotification.PropertyChanged += (s, e) => Safe(this); UserJoinedNotification.PropertyChanged += (s, e) => PropertyChangedHandler.Invoke(this, new System.ComponentModel.PropertyChangedEventArgs(nameof(Settings))); break; case nameof(UserLeftNotification): UserLeftNotification.PropertyChanged += (s, e) => Safe(this); UserLeftNotification.PropertyChanged += (s, e) => PropertyChangedHandler.Invoke(this, new System.ComponentModel.PropertyChangedEventArgs(nameof(Settings))); break; case nameof(BeingHostedNotification): BeingHostedNotification.PropertyChanged += (s, e) => Safe(this); BeingHostedNotification.PropertyChanged += (s, e) => PropertyChangedHandler.Invoke(this, new System.ComponentModel.PropertyChangedEventArgs(nameof(Settings))); break; case nameof(MessageNotification): MessageNotification.PropertyChanged += (s, e) => Safe(this); MessageNotification.PropertyChanged += (s, e) => PropertyChangedHandler.Invoke(this, new System.ComponentModel.PropertyChangedEventArgs(nameof(Settings))); break; case nameof(ClientConnectedNotification): ClientConnectedNotification.PropertyChanged += (s, e) => Safe(this); ClientConnectedNotification.PropertyChanged += (s, e) => PropertyChangedHandler.Invoke(this, new System.ComponentModel.PropertyChangedEventArgs(nameof(Settings))); break; case nameof(NewFollowerNotification): NewFollowerNotification.PropertyChanged += (s, e) => Safe(this); NewFollowerNotification.PropertyChanged += (s, e) => PropertyChangedHandler.Invoke(this, new System.ComponentModel.PropertyChangedEventArgs(nameof(Settings))); break; case nameof(BabelSettings): BabelSettings.PropertyChanged += (s, e) => Safe(this); BabelSettings.PropertyChanged += (s, e) => PropertyChangedHandler.Invoke(this, new System.ComponentModel.PropertyChangedEventArgs(nameof(Settings))); break; } Safe(this); }
public void OnPropertyChanged([CallerMemberName] string name = null) { PropertyChangedHandler?.Invoke(this, new PropertyChangedEventArgs(name)); }
/// <summary> /// Detaches the specified event handler. /// </summary> /// <param name="handler">The handler.</param> public override void Detach(PropertyChangedHandler handler) { _eventDelegate -= handler; }
protected void OnPropertyChanged(string propertyName) { PropertyChangedHandler?.Invoke(this, new PropertyChangedEventArgs(propertyName)); }
public static void SubscribeToChange <T>(this T objectThatNotifies, Expression <Func <object> > expression, PropertyChangedHandler <T> handler) where T : INotifyPropertyChanged { objectThatNotifies.PropertyChanged += (s, e) => { var lambda = expression as LambdaExpression; MemberExpression memberExpression; if (lambda.Body is UnaryExpression) { var unaryExpression = lambda.Body as UnaryExpression; memberExpression = unaryExpression.Operand as MemberExpression; } else { memberExpression = lambda.Body as MemberExpression; } var propertyInfo = memberExpression.Member as PropertyInfo; if (e.PropertyName.Equals(propertyInfo.Name)) { handler(objectThatNotifies); } }; }
public static void SubscribeToChange <T>(this T objectThatNotifies, Expression <Func <object> > expression, PropertyChangedHandler <T> handler) where T : INotifyPropertyChanged { var thread = Thread.CurrentThread; objectThatNotifies.PropertyChanged += (s, e) => { var memberExpression = expression.GetMemberExpression(); var propertyInfo = memberExpression.Member as PropertyInfo; if (e.PropertyName.Equals(propertyInfo.Name)) { if (Deployment.Current.Dispatcher.CheckAccess()) { handler(objectThatNotifies); } else { Deployment.Current.Dispatcher.BeginInvoke(handler, objectThatNotifies); } } }; }
public static void SubscribeToChange(this INotifyPropertyChanged objectThatNotifies, Expression <Func <object> > expression, PropertyChangedHandler <INotifyPropertyChanged> handler) { SubscribeToChange <INotifyPropertyChanged>(objectThatNotifies, expression, handler); }
public void OnPropertyChangedAsync(string name) { PropertyChangedHandler.BeginInvoke(this, new PropertyChangedEventArgs(name), null, null); }
public static void SubscribeToChange(this INotifyPropertyChanged objectThatNotifies, string propertyName, PropertyChangedHandler <INotifyPropertyChanged> handler) { objectThatNotifies.PropertyChanged += (s, e) => { if (e.PropertyName.Equals(propertyName)) { handler(objectThatNotifies); } }; }
/// <summary>Casts the given object to an 'INotifyPropertyChanged' and if it is observable un-wires the given event handler from the 'PropertyChanged' event.</summary> /// <param name="model">The model to un-wire.</param> /// <param name="handler">The handler to remove.</param> public static void UnlistenPropertyChanged(object model, PropertyChangedHandler handler) { INotifyPropertyChanged observable = model as INotifyPropertyChanged; if (observable != null) observable.PropertyChanged -= handler; }