Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added IsNullOrEmptyConverters Samples #148

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,33 @@
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:mct="clr-namespace:CommunityToolkit.Maui.Converters;assembly=CommunityToolkit.Maui"
xmlns:pages="clr-namespace:CommunityToolkit.Maui.Sample.Pages"
xmlns:vm="clr-namespace:CommunityToolkit.Maui.Sample.ViewModels.Converters"
x:Class="CommunityToolkit.Maui.Sample.Pages.Converters.IsNotNullOrEmptyConverterPage">
<pages:BasePage.BindingContext>
<vm:IsNotNullOrEmptyConverterViewModel />
</pages:BasePage.BindingContext>
<pages:BasePage.Resources>
<ResourceDictionary>
<mct:IsNotNullOrEmptyConverter x:Key="IsNotNullOrEmptyConverter" />
</ResourceDictionary>
</pages:BasePage.Resources>
<pages:BasePage.Content>
<StackLayout>
<Label Text="IsNotNullOrEmptyConverter"
VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand" />
<StackLayout
Padding="10,10"
HorizontalOptions="Fill"
Spacing="16"
VerticalOptions="Fill">
<Label Text="The IsNullOrEmptyConverter is a converter that allows users to convert an incoming binding to a bool value. This value represents if the incoming binding value is null or empty." TextColor="{StaticResource NormalLabelTextColor}" />
<Button VerticalOptions="CenterAndExpand" Text="Clear Selection" Command="{Binding ClearSelectionCommand}"/>
<Label VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand" Text="This label should be false if selected item is null" TextColor="{StaticResource NormalLabelTextColor}" />
<Label VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand" Text="{Binding Path=SelectedItem, Converter={StaticResource IsNotNullOrEmptyConverter }}" />
<CollectionView VerticalOptions="StartAndExpand" HorizontalOptions="Center" SelectionMode="Single" ItemsSource="{Binding DummyItemSource}" SelectedItem="{Binding SelectedItem}">
<CollectionView.ItemTemplate>
<DataTemplate>
<Label Text="{Binding .}" Margin="10" TextColor="Black" />
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</StackLayout>
</pages:BasePage.Content>
</pages:BasePage>
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,33 @@
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:mct="clr-namespace:CommunityToolkit.Maui.Converters;assembly=CommunityToolkit.Maui"
xmlns:pages="clr-namespace:CommunityToolkit.Maui.Sample.Pages"
xmlns:vm="clr-namespace:CommunityToolkit.Maui.Sample.ViewModels.Converters"
x:Class="CommunityToolkit.Maui.Sample.Pages.Converters.IsNullOrEmptyConverterPage">
<pages:BasePage.BindingContext>
<vm:IsNullOrEmptyConverterViewModel />
</pages:BasePage.BindingContext>
<pages:BasePage.Resources>
<ResourceDictionary>
<mct:IsNullOrEmptyConverter x:Key="IsNullOrEmptyConverter" />
</ResourceDictionary>
</pages:BasePage.Resources>
<pages:BasePage.Content>
<StackLayout>
<Label Text="IsNullOrEmptyConverter"
VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand" />
<StackLayout
Padding="10,10"
HorizontalOptions="Fill"
Spacing="16"
VerticalOptions="Fill">
<Label Text="The IsNullOrEmptyConverter is a converter that allows users to convert an incoming binding to a bool value. This value represents if the incoming binding value is null or empty." TextColor="{StaticResource NormalLabelTextColor}" />
<Button VerticalOptions="CenterAndExpand" Text="Clear Selection" Command="{Binding ClearSelectionCommand}"/>
<Label VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand" Text="This label should be true if selected item is null" TextColor="{StaticResource NormalLabelTextColor}" />
<Label VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand" Text="{Binding Path=SelectedItem, Converter={StaticResource IsNullOrEmptyConverter}}" />
<CollectionView VerticalOptions="StartAndExpand" HorizontalOptions="Center" SelectionMode="Single" ItemsSource="{Binding DummyItemSource}" SelectedItem="{Binding SelectedItem}">
<CollectionView.ItemTemplate>
<DataTemplate>
<Label Text="{Binding .}" Margin="10" TextColor="Black" />
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</StackLayout>
</pages:BasePage.Content>
</pages:BasePage>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;
namespace CommunityToolkit.Maui.Sample.ViewModels.Converters;

public class IsNotNullOrEmptyConverterViewModel : IsNullOrEmptyConverterViewModel
{
public IsNotNullOrEmptyConverterViewModel()
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System.Collections.ObjectModel;
using Microsoft.Maui.Controls;
using System.Windows.Input;
using System.Collections.Generic;

namespace CommunityToolkit.Maui.Sample.ViewModels.Converters;

public class IsNullOrEmptyConverterViewModel : BaseViewModel
{
string? selectedItem;

public IsNullOrEmptyConverterViewModel() => ClearSelectionCommand = new Command(() => SelectedItem = null);

public IReadOnlyList<string> DummyItemSource { get; } = new[]
{
"Item 0",
"Item 1",
"Item 2",
"Item 3",
"Item 4",
"Item 5",
};

public ICommand ClearSelectionCommand { get; }

public string? SelectedItem
{
get => selectedItem;
set => SetProperty(ref selectedItem, value);
}
}