From 50d1b5917da6b1c05af495ef7ba5886d3f4c4b39 Mon Sep 17 00:00:00 2001 From: Tom van der Kleij Date: Thu, 11 Sep 2014 21:52:53 +0200 Subject: [PATCH] Added support for binding to properties of Component (WinForms) --- .../Winforms/DefaultPropertyBindingTests.cs | 24 +++++++++++++++++++ .../WinformsCreatesObservableForProperty.cs | 3 ++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/ReactiveUI.Tests/Winforms/DefaultPropertyBindingTests.cs b/ReactiveUI.Tests/Winforms/DefaultPropertyBindingTests.cs index 51c1856c1f..976d787fa1 100644 --- a/ReactiveUI.Tests/Winforms/DefaultPropertyBindingTests.cs +++ b/ReactiveUI.Tests/Winforms/DefaultPropertyBindingTests.cs @@ -32,6 +32,30 @@ public void WinformsCreatesObservableForPropertyWorksForTextboxes() Assert.Equal(1, output.Count); } + [Fact] + public void WinformsCreatesObservableForPropertyWorksForComponents() + { + var input = new ToolStripButton(); // ToolStripButton is a Component, not a Control + var fixture = new WinformsCreatesObservableForProperty(); + + Assert.NotEqual(0, fixture.GetAffinityForObject(typeof(ToolStripButton), "Checked")); + + Expression> expression = x => x.Checked; + var output = fixture.GetNotificationForProperty(input, expression.Body).CreateCollection(); + Assert.Equal(0, output.Count); + + input.Checked = true; + Assert.Equal(1, output.Count); + Assert.Equal(input, output[0].Sender); + Assert.Equal("Checked", output[0].GetPropertyName()); + + output.Dispose(); + + // Since we disposed the derived list, we should no longer receive updates + input.Checked = false; + Assert.Equal(1, output.Count); + } + [Fact] public void WinformsCreatesObservableForPropertyWorksForThirdPartyControls() { diff --git a/ReactiveUI.Winforms/Winforms/WinformsCreatesObservableForProperty.cs b/ReactiveUI.Winforms/Winforms/WinformsCreatesObservableForProperty.cs index 952a156aa2..e6264b85b3 100644 --- a/ReactiveUI.Winforms/Winforms/WinformsCreatesObservableForProperty.cs +++ b/ReactiveUI.Winforms/Winforms/WinformsCreatesObservableForProperty.cs @@ -1,4 +1,5 @@ using System; +using System.ComponentModel; using System.Linq; using System.Linq.Expressions; using System.Reactive.Disposables; @@ -18,7 +19,7 @@ public class WinformsCreatesObservableForProperty : ICreatesObservableForPropert public int GetAffinityForObject(Type type, string propertyName, bool beforeChanged = false) { - bool supportsTypeBinding = typeof(Control).IsAssignableFrom(type); + bool supportsTypeBinding = typeof(Component).IsAssignableFrom(type); if (!supportsTypeBinding) return 0; lock (eventInfoCache) {