From b91df127eaaeec4972e0035fb4602a7666cc4a64 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Sat, 19 Jan 2019 01:09:58 +0100 Subject: [PATCH 1/2] Remove :valid and :invalid pseudoclasses. `:valid` and `:invalid` pseudoclasses are being added to all `ContentControls` simply in order to show/hide the `ContentPresenter` in `CheckBox`. Instead of this, add some `ObjectConverters` and use them to set the visibility of the `ContentPresenter.` Also renamed the converters in `StringConverters` to add an `Is` prefix as the `ObjectConverters` didn't look right as just `Null` and `NotNull`. --- .../Data/Converters/ObjectConverters.cs | 24 +++++++++++++++++++ .../Data/Converters/StringConverters.cs | 4 ++-- src/Avalonia.Controls/ContentControl.cs | 2 -- src/Avalonia.Themes.Default/CheckBox.xaml | 9 ++++--- src/Avalonia.Themes.Default/TextBox.xaml | 4 ++-- 5 files changed, 32 insertions(+), 11 deletions(-) create mode 100644 src/Avalonia.Base/Data/Converters/ObjectConverters.cs diff --git a/src/Avalonia.Base/Data/Converters/ObjectConverters.cs b/src/Avalonia.Base/Data/Converters/ObjectConverters.cs new file mode 100644 index 00000000000..3cadfd5b47d --- /dev/null +++ b/src/Avalonia.Base/Data/Converters/ObjectConverters.cs @@ -0,0 +1,24 @@ +// Copyright (c) The Avalonia Project. All rights reserved. +// Licensed under the MIT license. See licence.md file in the project root for full license information. + + +namespace Avalonia.Data.Converters +{ + /// + /// Provides a set of useful s for working with objects. + /// + public static class ObjectConverters + { + /// + /// A value converter that returns true if the input object is a null reference. + /// + public static readonly IValueConverter IsNull = + new FuncValueConverter(x => x is null); + + /// + /// A value converter that returns true if the input object is not null. + /// + public static readonly IValueConverter IsNotNull = + new FuncValueConverter(x => !(x is null)); + } +} diff --git a/src/Avalonia.Base/Data/Converters/StringConverters.cs b/src/Avalonia.Base/Data/Converters/StringConverters.cs index 0101cf5d295..42eb8e1d81f 100644 --- a/src/Avalonia.Base/Data/Converters/StringConverters.cs +++ b/src/Avalonia.Base/Data/Converters/StringConverters.cs @@ -12,13 +12,13 @@ public static class StringConverters /// /// A value converter that returns true if the input string is null or an empty string. /// - public static readonly IValueConverter NullOrEmpty = + public static readonly IValueConverter IsNullOrEmpty = new FuncValueConverter(string.IsNullOrEmpty); /// /// A value converter that returns true if the input string is not null or empty. /// - public static readonly IValueConverter NotNullOrEmpty = + public static readonly IValueConverter IsNotNullOrEmpty = new FuncValueConverter(x => !string.IsNullOrEmpty(x)); } } diff --git a/src/Avalonia.Controls/ContentControl.cs b/src/Avalonia.Controls/ContentControl.cs index 20ec5811085..6da6da54a5e 100644 --- a/src/Avalonia.Controls/ContentControl.cs +++ b/src/Avalonia.Controls/ContentControl.cs @@ -45,8 +45,6 @@ public class ContentControl : TemplatedControl, IContentControl, IContentPresent static ContentControl() { ContentControlMixin.Attach(ContentProperty, x => x.LogicalChildren); - PseudoClass(ContentProperty, x => x != null, ":valid"); - PseudoClass(ContentProperty, x => x == null, ":invalid"); } /// diff --git a/src/Avalonia.Themes.Default/CheckBox.xaml b/src/Avalonia.Themes.Default/CheckBox.xaml index e2301b9b09f..f87ae2674fa 100644 --- a/src/Avalonia.Themes.Default/CheckBox.xaml +++ b/src/Avalonia.Themes.Default/CheckBox.xaml @@ -1,9 +1,10 @@ - + - diff --git a/src/Avalonia.Themes.Default/TextBox.xaml b/src/Avalonia.Themes.Default/TextBox.xaml index 90100c2f2b1..6741bdc7d98 100644 --- a/src/Avalonia.Themes.Default/TextBox.xaml +++ b/src/Avalonia.Themes.Default/TextBox.xaml @@ -23,7 +23,7 @@ Path="UseFloatingWatermark"/> + Converter="{x:Static StringConverters.IsNotNullOrEmpty}"/> @@ -36,7 +36,7 @@ + IsVisible="{TemplateBinding Text, Converter={x:Static StringConverters.IsNullOrEmpty}}"/> Date: Sat, 19 Jan 2019 02:45:59 +0100 Subject: [PATCH 2/2] Fix tests. --- .../Data/BindingTests_Converters.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/Avalonia.Markup.UnitTests/Data/BindingTests_Converters.cs b/tests/Avalonia.Markup.UnitTests/Data/BindingTests_Converters.cs index 123aadfda53..1ede4ee0b0d 100644 --- a/tests/Avalonia.Markup.UnitTests/Data/BindingTests_Converters.cs +++ b/tests/Avalonia.Markup.UnitTests/Data/BindingTests_Converters.cs @@ -22,14 +22,14 @@ public void Converter_Should_Be_Used() var target = new Binding(nameof(Class1.Foo)) { - Converter = StringConverters.NullOrEmpty, + Converter = StringConverters.IsNullOrEmpty, }; var expressionObserver = (BindingExpression)target.Initiate( textBlock, TextBlock.TextProperty).Observable; - Assert.Same(StringConverters.NullOrEmpty, expressionObserver.Converter); + Assert.Same(StringConverters.IsNullOrEmpty, expressionObserver.Converter); } public class When_Binding_To_String @@ -129,7 +129,7 @@ public void StringFormat_Should_Be_Applied_After_Converter() var target = new Binding(nameof(Class1.Foo)) { - Converter = StringConverters.NotNullOrEmpty, + Converter = StringConverters.IsNotNullOrEmpty, StringFormat = "Hello {0}", };