public void NotifyOfPropertyChangedWithStringRaises() { var pc = new PropertyChanged(); string changedProperty = null; pc.PropertyChanged += (o, e) => changedProperty = e.PropertyName; pc.RaiseIntPropertyChangedWithString(); Assert.AreEqual("IntProperty", changedProperty); }
public void NotifyOfPropertyChangedWithCallerMemberName() { var pc = new PropertyChanged(); string changedProperty = null; pc.PropertyChanged += (o, e) => changedProperty = e.PropertyName; pc.StringProperty = "hello"; Assert.AreEqual("StringProperty", changedProperty); }
public void RefreshRaisesPropertyChangedWithEmptyString() { var pc = new PropertyChanged(); string changedProperty = null; pc.PropertyChanged += (o, e) => changedProperty = e.PropertyName; pc.Refresh(); Assert.AreEqual(String.Empty, changedProperty); }
private void Init(Model model) { this.DataContext = model; var applyCommand = new DelegateCommand( () => Success(new Result.Apply(model)), () => { if (repeatedPassword != model.password) { return false; } return true; } ); OnRepeatedPasswordChanged += () => applyCommand.RaiseCanExecuteChanged(); ApplyCommand = applyCommand; var cancelCommand = new DelegateCommand( () => Success(new Result.Cancel()), () => true ); CancelCommand = cancelCommand; repeatedPassword = model.password; InitializeComponent(); userNameCaption.CreateBinding(Label.ContentProperty, this.Strings, s => s.nameCaption); userNameValue.CreateBinding(TextBox.TextProperty, model, m => m.name); passwordCaption.CreateBinding(Label.ContentProperty, this.Strings, s => s.passwordCaption); repeatPasswordCaption.CreateBinding(Label.ContentProperty, this.Strings, s => s.repeatPasswordCaption); passwordValue.CreateBinding( TextBox.TextProperty, model, m => m.password, (m, v) => { m.password = String.IsNullOrEmpty(v) ? null : v; applyCommand.RaiseCanExecuteChanged(); } ); //repeatPasswordCaption.CreateBinding(Label.ContentProperty, this.Strings, s => s.repeatPassword); repeatPasswordValue.Text = repeatedPassword; repeatPasswordValue.TextChanged += (s, a) => { repeatedPassword = String.IsNullOrEmpty(repeatPasswordValue.Text) ? null : repeatPasswordValue.Text; }; roleCaption.CreateBinding(Label.ContentProperty, this.Strings, s => s.roleCaption); roleValue.ItemsSource = EnumHelper.GetValues<UserLevel>().Where(v => v != UserLevel.anonymous && v != UserLevel.extended); roleValue.CreateBinding(ComboBox.SelectedValueProperty, model, m => m.level, (m, v) => m.level = v); applyButton.Command = ApplyCommand; applyButton.CreateBinding(Button.ContentProperty, SaveCancel, s => s.apply); cancelButton.Command = CancelCommand; cancelButton.CreateBinding(Button.ContentProperty, SaveCancel, s => s.cancel); }
public void UsesDispatcher() { var pc = new PropertyChanged(); string changedProperty = null; pc.PropertyChanged += (o, e) => changedProperty = e.PropertyName; Action action = null; pc.PropertyChangedDispatcher = a => action = a; pc.RaiseIntPropertyChangedWithExpression(); Assert.IsNull(changedProperty); Assert.IsNotNull(action); action(); Assert.AreEqual("IntProperty", changedProperty); }
/****************************************************************************************************/ /* Constructor */ /****************************************************************************************************/ public Player(string username) { PropertyChanged = (type, player) => { }; Username = username; var city = new ObservableCollection<District>(); city.CollectionChanged += CityChanged; City = city; var characters = new ObservableCollection<Character>(); characters.CollectionChanged += CharactersChanged; Characters = characters; var hand = new ObservableCollection<District>(); hand.CollectionChanged += HandChanged; Hand = hand; HiddenCharacters = new SortedSet<Character>(); }
public void PropertyName() { var instance = CreateWeavedInstance(typeof (TestNames)); var eventInfo = instance.GetType().GetEvent("PropertyChanged"); var property1 = instance.GetType().GetProperty("Name1"); var propertyName = ""; var handler = new PropertyChanged( (o, name, value, nValue) => { propertyName = name; return false; }); eventInfo.AddEventHandler(instance, handler); property1.SetValue(instance,1); Assert.AreEqual(property1.Name,propertyName); }
/// <summary> /// /// </summary> /// <param name="instance"></param> /// <param name="property"></param> /// <param name="eventInfo"></param> /// <param name="value0">Initial value</param> /// <param name="value1">Changed value</param> private static void TestProperty(object instance,PropertyInfo property, EventInfo eventInfo, object value0, object value1) { var eventTriggerCount = 0; var keepAliveHandler = true; object oldValue = null; object newValue = null; var handler = new PropertyChanged( (o, name, value, nValue) => { eventTriggerCount++; oldValue = value; newValue = nValue; return keepAliveHandler; }); property.SetValue(instance,value0); Assert.AreEqual(value0,property.GetValue(instance)); Assert.AreEqual(0,eventTriggerCount); eventInfo.AddEventHandler(instance, handler); property.SetValue(instance,value1); Assert.AreEqual(value1,property.GetValue(instance)); Assert.AreEqual(1,eventTriggerCount); Assert.AreEqual(value0, oldValue); Assert.AreEqual(value1, newValue); property.SetValue(instance,value1); //same value should not be triggered Assert.AreEqual(value1,property.GetValue(instance)); Assert.AreEqual(1,eventTriggerCount); eventInfo.RemoveEventHandler(instance, handler); property.SetValue(instance,value0); //handler should not be triggered Assert.AreEqual(value0,property.GetValue(instance)); Assert.AreEqual(1,eventTriggerCount); eventInfo.AddEventHandler(instance, handler); keepAliveHandler = false; //event should be removed after next trigger property.SetValue(instance,value1); //test event self remove from return false Assert.AreEqual(value1,property.GetValue(instance)); Assert.AreEqual(2,eventTriggerCount); Assert.AreEqual(value0, oldValue); Assert.AreEqual(value1, newValue); property.SetValue(instance,value0); //should not trigger handler Assert.AreEqual(value0,property.GetValue(instance)); Assert.AreEqual(2,eventTriggerCount); }
public void UsesStaticDispatcherByDefault() { Action action = null; var oldDispatcher = Execute.DefaultPropertyChangedDispatcher; Execute.DefaultPropertyChangedDispatcher = a => action = a; var pc = new PropertyChanged(); string changedProperty = null; pc.PropertyChanged += (o, e) => changedProperty = e.PropertyName; pc.RaiseIntPropertyChangedWithExpression(); Assert.IsNull(changedProperty); Assert.IsNotNull(action); action(); Assert.AreEqual("IntProperty", changedProperty); Execute.DefaultPropertyChangedDispatcher = oldDispatcher; }
public void SetAndNotifyWorks() { var pc = new PropertyChanged(); string changedProperty = null; pc.PropertyChanged += (o, e) => changedProperty = e.PropertyName; pc.DoubleProperty = 5; Assert.AreEqual("DoubleProperty", changedProperty); Assert.AreEqual(5, pc.DoubleProperty); }
public WidgetSizeChangedEventArgs(int height, int width, PropertyChanged propertyChanged) { Height = height; Width = width; ChangedProperty = propertyChanged; }
private void Init(Model model) { this.DataContext = model; var applyCommand = new DelegateCommand( () => Success(new Result.Apply(userName, password, userLevel)), () => { if (repeatedPassword != password) { return false; } if (String.IsNullOrWhiteSpace(userName)) { return false; } if (model.existingUsers != null && model.existingUsers.Contains(userName)) { return false; } return true; } ); OnRepeatedPasswordChanged += () => applyCommand.RaiseCanExecuteChanged(); OnPasswordChanged += () => applyCommand.RaiseCanExecuteChanged(); OnUserNameChanged += () => applyCommand.RaiseCanExecuteChanged(); ApplyCommand = applyCommand; var cancelCommand = new DelegateCommand( () => Success(new Result.Cancel()), () => true ); CancelCommand = cancelCommand; password = model.defaultPassword; repeatedPassword = model.defaultPassword; userName = model.defaultUserName; userLevel = model.defaultUserLevel; InitializeComponent(); userNameCaption.CreateBinding(Label.ContentProperty, this.Strings, s => s.nameCaption); userNameValue.Text = userName; userNameValue.TextChanged += (s, a) => userName = userNameValue.Text; passwordCaption.CreateBinding(Label.ContentProperty, this.Strings, s => s.passwordCaption); passwordValue.Text = password; passwordValue.TextChanged += (s, a) => password = String.IsNullOrEmpty(passwordValue.Text) ? null : passwordValue.Text; //repeatPasswordCaption.CreateBinding(Label.ContentProperty, this.Strings, s => s.repeatPassword); repeatPasswordValue.Text = repeatedPassword; repeatPasswordValue.TextChanged += (s, a) => repeatedPassword = String.IsNullOrEmpty(repeatPasswordValue.Text) ? null : repeatPasswordValue.Text; roleCaption.CreateBinding(Label.ContentProperty, this.Strings, s => s.roleCaption); roleValue.ItemsSource = EnumHelper.GetValues<UserLevel>().Where(v=>v!= UserLevel.anonymous && v!=UserLevel.extended); roleValue.SelectedValue = userLevel; roleValue.SelectionChanged += (s, a) => userLevel = (UserLevel)roleValue.SelectedValue; applyButton.Command = ApplyCommand; applyButton.CreateBinding(Button.ContentProperty, SaveCancel, s => s.apply); cancelButton.Command = CancelCommand; cancelButton.CreateBinding(Button.ContentProperty, SaveCancel, s => s.cancel); }