[Proposal] IsStringNullOrWhiteSpace Converter
See original GitHub issueIsStringNullOrWhiteSpace Converters
- Proposed
- Prototype
- Implementation
- iOS Support
- Android Support
- macOS Support
- Windows Support
- Unit Tests
- Sample
- Documentation
Summary
The IsStringNullOrWhiteSpaceConverter
is a converter that allows users to convert an incoming string
binding to a bool
value. This value represents if the incoming binding value is null or empty using string.IsNullOrWhiteSpace
.
The IsStringNotNullOrWhiteSpaceConverter
is a converter that allows users to convert an incoming string
binding to a bool
value. This value represents if the incoming binding value is null or empty using string.IsNullOrWhiteSpace
.
Detailed Design
IsStringNullOrWhiteSpaceConverter.shared.cs
public class IsStringNullOrWhiteSpaceConverter : ValueConverterExtension, ICommunityToolkitValueConverter
{
public object Convert(object? value, Type? targetType, object? parameter, CultureInfo? culture) => value is string stringValue ? string.IsNullOrWhiteSpace(stringValue) : throw new InvalidArgumentException("Binding must be of type string");
public object? ConvertBack(object? value, Type? targetType, object? parameter, CultureInfo? culture) => throw new NotImplementedException();
}
IsStringNotNullOrWhiteSpaceConverter.shared.cs
public class IsStringNotNullOrWhiteSpaceConverter : ValueConverterExtension, ICommunityToolkitValueConverter
{
public object Convert(object? value, Type? targetType, object? parameter, CultureInfo? culture) => value is string stringValue ? !string.IsNullOrWhiteSpace(stringValue) : throw new InvalidArgumentException("Binding must be of type string");
public object? ConvertBack(object? value, Type? targetType, object? parameter, CultureInfo? culture) => throw new NotImplementedException();
}
Usage Syntax
XAML Usage
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:xct="http://xamarin.com/schemas/2020/toolkit"
x:Class="MyLittleApp.MainPage">
<ContentPage.Resources>
<ResourceDictionary>
<xct: IsStringNullOrWhiteSpaceConverter x:Key="IsStringNullOrWhiteSpaceConverter" />
</ResourceDictionary>
</ContentPage.Resources>
<StackLayout>
<Label IsVisible="{Binding MyStringValue, Converter={StaticResource IsStringNullOrWhiteSpaceConverter}}" />
</StackLayout>
</ContentPage>
C# Usage
class MyPage : ContentPage
{
public MyPage()
{
Content = new StackLayout
{
Children =
{
new Label().Bind(Label.IsVisibleProperty, nameof(ViewModel.MyString), converter: new IsStringNullOrWhiteSpaceConverter()),
};
}
}
}
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (4 by maintainers)
Top Results From Across the Web
IsStringNullOrWhiteSpaceConve...
The IsStringNullOrWhiteSpaceConverter is a one way converter that returns a bool indicating whether the binding value is null or string.
Read more >[Bug] IsNullOrEmptyConverter checks with string. ...
I'm using the IsNullOrEmptyConverter in my project to show a UI element when an input string is null or empty, but when I...
Read more >How can I determine if a String is non-null and not only ...
myString?.trim() returns a string (or null or blank) instead of the question's which returns a boolean. I guess it depends how you're using...
Read more >Checking if a string is null
There is a IsNullOrWhiteSpace method when it's telling you if there is any character in the variable which is not space, tab, enter...
Read more >Convert Empty String Columns to NULL for Many Colu...
I have many fields that I need to convert to NULLs if they are empty strings. Is there an easier and more time-efficient...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I approve to move forward with this feature ✅
I approve this feature