Browse Source

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`.
pull/2253/head
Steven Kirk 7 years ago
parent
commit
b91df127ea
  1. 24
      src/Avalonia.Base/Data/Converters/ObjectConverters.cs
  2. 4
      src/Avalonia.Base/Data/Converters/StringConverters.cs
  3. 2
      src/Avalonia.Controls/ContentControl.cs
  4. 9
      src/Avalonia.Themes.Default/CheckBox.xaml
  5. 4
      src/Avalonia.Themes.Default/TextBox.xaml

24
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
{
/// <summary>
/// Provides a set of useful <see cref="IValueConverter"/>s for working with objects.
/// </summary>
public static class ObjectConverters
{
/// <summary>
/// A value converter that returns true if the input object is a null reference.
/// </summary>
public static readonly IValueConverter IsNull =
new FuncValueConverter<object, bool>(x => x is null);
/// <summary>
/// A value converter that returns true if the input object is not null.
/// </summary>
public static readonly IValueConverter IsNotNull =
new FuncValueConverter<object, bool>(x => !(x is null));
}
}

4
src/Avalonia.Base/Data/Converters/StringConverters.cs

@ -12,13 +12,13 @@ namespace Avalonia.Data.Converters
/// <summary> /// <summary>
/// A value converter that returns true if the input string is null or an empty string. /// A value converter that returns true if the input string is null or an empty string.
/// </summary> /// </summary>
public static readonly IValueConverter NullOrEmpty = public static readonly IValueConverter IsNullOrEmpty =
new FuncValueConverter<string, bool>(string.IsNullOrEmpty); new FuncValueConverter<string, bool>(string.IsNullOrEmpty);
/// <summary> /// <summary>
/// A value converter that returns true if the input string is not null or empty. /// A value converter that returns true if the input string is not null or empty.
/// </summary> /// </summary>
public static readonly IValueConverter NotNullOrEmpty = public static readonly IValueConverter IsNotNullOrEmpty =
new FuncValueConverter<string, bool>(x => !string.IsNullOrEmpty(x)); new FuncValueConverter<string, bool>(x => !string.IsNullOrEmpty(x));
} }
} }

2
src/Avalonia.Controls/ContentControl.cs

@ -45,8 +45,6 @@ namespace Avalonia.Controls
static ContentControl() static ContentControl()
{ {
ContentControlMixin.Attach<ContentControl>(ContentProperty, x => x.LogicalChildren); ContentControlMixin.Attach<ContentControl>(ContentProperty, x => x.LogicalChildren);
PseudoClass<ContentControl, object>(ContentProperty, x => x != null, ":valid");
PseudoClass<ContentControl, object>(ContentProperty, x => x == null, ":invalid");
} }
/// <summary> /// <summary>

9
src/Avalonia.Themes.Default/CheckBox.xaml

@ -1,9 +1,10 @@
<Styles xmlns="https://github.com/avaloniaui"> <Styles xmlns="https://github.com/avaloniaui" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style Selector="CheckBox"> <Style Selector="CheckBox">
<Setter Property="Foreground" Value="{DynamicResource ThemeForegroundBrush}"/> <Setter Property="Foreground" Value="{DynamicResource ThemeForegroundBrush}"/>
<Setter Property="Background" Value="Transparent"/> <Setter Property="Background" Value="Transparent"/>
<Setter Property="BorderBrush" Value="{DynamicResource ThemeBorderMidBrush}"/> <Setter Property="BorderBrush" Value="{DynamicResource ThemeBorderMidBrush}"/>
<Setter Property="BorderThickness" Value="{DynamicResource ThemeBorderThickness}"/> <Setter Property="BorderThickness" Value="{DynamicResource ThemeBorderThickness}"/>
<Setter Property="Padding" Value="4,0,0,0"/>
<Setter Property="VerticalContentAlignment" Value="Center"/> <Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="HorizontalContentAlignment" Value="Left"/> <Setter Property="HorizontalContentAlignment" Value="Left"/>
<Setter Property="Template"> <Setter Property="Template">
@ -38,17 +39,15 @@
TextBlock.Foreground="{TemplateBinding Foreground}" TextBlock.Foreground="{TemplateBinding Foreground}"
ContentTemplate="{TemplateBinding ContentTemplate}" ContentTemplate="{TemplateBinding ContentTemplate}"
Content="{TemplateBinding Content}" Content="{TemplateBinding Content}"
Margin="4,0,0,0" Margin="{TemplateBinding Padding}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
IsVisible="{TemplateBinding Content, Converter={x:Static ObjectConverters.IsNotNull}}"
Grid.Column="1"/> Grid.Column="1"/>
</Grid> </Grid>
</ControlTemplate> </ControlTemplate>
</Setter> </Setter>
</Style> </Style>
<Style Selector="CheckBox:invalid /template/ ContentPresenter#PART_ContentPresenter">
<Setter Property="IsVisible" Value="False"/>
</Style>
<Style Selector="CheckBox:pointerover /template/ Border#border"> <Style Selector="CheckBox:pointerover /template/ Border#border">
<Setter Property="BorderBrush" Value="{DynamicResource ThemeBorderHighBrush}"/> <Setter Property="BorderBrush" Value="{DynamicResource ThemeBorderHighBrush}"/>
</Style> </Style>

4
src/Avalonia.Themes.Default/TextBox.xaml

@ -23,7 +23,7 @@
Path="UseFloatingWatermark"/> Path="UseFloatingWatermark"/>
<Binding RelativeSource="{RelativeSource TemplatedParent}" <Binding RelativeSource="{RelativeSource TemplatedParent}"
Path="Text" Path="Text"
Converter="{x:Static StringConverters.NotNullOrEmpty}"/> Converter="{x:Static StringConverters.IsNotNullOrEmpty}"/>
</MultiBinding> </MultiBinding>
</TextBlock.IsVisible> </TextBlock.IsVisible>
</TextBlock> </TextBlock>
@ -36,7 +36,7 @@
<TextBlock Name="watermark" <TextBlock Name="watermark"
Opacity="0.5" Opacity="0.5"
Text="{TemplateBinding Watermark}" Text="{TemplateBinding Watermark}"
IsVisible="{TemplateBinding Text, Converter={x:Static StringConverters.NullOrEmpty}}"/> IsVisible="{TemplateBinding Text, Converter={x:Static StringConverters.IsNullOrEmpty}}"/>
<TextPresenter Name="PART_TextPresenter" <TextPresenter Name="PART_TextPresenter"
Text="{TemplateBinding Text, Mode=TwoWay}" Text="{TemplateBinding Text, Mode=TwoWay}"
CaretIndex="{TemplateBinding CaretIndex}" CaretIndex="{TemplateBinding CaretIndex}"

Loading…
Cancel
Save