private void DoRebind() { // removing possible previous bindings for formatting validation results as colors (would not be needed if elements were always IValidatingObject instances) if (formatColorBinding != null) { tbIntPropList.DataBindings.Remove(intPropColorBinding); tbStringPropList.DataBindings.Remove(stringPropColorBinding); tbIntPropList.BackColor = SystemColors.Window; tbStringPropList.BackColor = SystemColors.Window; tbIntPropCurrent.BackColor = SystemColors.Window; tbStringPropCurrent.BackColor = SystemColors.Window; commandBindings.Remove(formatColorBinding); formatColorBinding = null; } // the actual rebind IList testList = viewModel.TestList; listBindingSource.SuspendBinding(); listBindingSource.DataSource = testList; listBindingSource.ResumeBinding(); errorProvider.UpdateBinding(); // bindings for TextBox.BackColor (could be in the constructor if elements were always IValidatingObject instances but WinForms is not tolerant for invalid property names) if (testList.Cast <ITestObject>().FirstOrDefault() is IValidatingObject) { intPropColorBinding = new Binding(nameof(TextBox.BackColor), listBindingSource, nameof(IValidatingObject.ValidationResults), true, DataSourceUpdateMode.Never); stringPropColorBinding = new Binding(nameof(TextBox.BackColor), listBindingSource, nameof(IValidatingObject.ValidationResults), true, DataSourceUpdateMode.Never); formatColorBinding = commandBindings.Add <ConvertEventArgs>(OnFormatColor) .AddSource(intPropColorBinding, nameof(Binding.Format)) .AddSource(stringPropColorBinding, nameof(Binding.Format)); tbIntPropList.DataBindings.Add(intPropColorBinding); tbStringPropList.DataBindings.Add(stringPropColorBinding); } }
private void UpdateCurrentView() { // We remove the earlier binding if any, which releases it subscription automatically. if (currentItemChangedBinding != null) { commandBindings.Remove(currentItemChangedBinding); } currentView = CollectionViewSource.GetDefaultView(viewModel.TestList); // currentView.CurrentChanged -> OnTestListCurrentChangedCommand currentItemChangedBinding = commandBindings.Add <EventArgs>(OnTestListCurrentChangedCommand) .AddSource(currentView, nameof(currentView.CurrentChanged)); }