Added converters

pull/69/head
Titouan LOUVET 2 years ago
parent b098e94ba2
commit 00e84264f9

@ -1,10 +1,18 @@
<?xml version="1.0" encoding="utf-8" ?> <?xml version="1.0" encoding="utf-8" ?>
<ContentView xmlns="http://schemas.microsoft.com/dotnet/2021/maui" <ContentView xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:conv="clr-namespace:Banquale.Views.Converters"
x:Class="Banquale.Views.Balance.BalanceView"> x:Class="Banquale.Views.Balance.BalanceView">
<ContentView.Resources>
<conv:Bool2ColorConverters x:Key="bool2ColorConverters"/>
<conv:Int2StringConverters x:Key="int2StringConverters"/>
</ContentView.Resources>
<VerticalStackLayout> <VerticalStackLayout>
<ListView ItemsSource="{Binding TransactionsList}" <ListView ItemsSource="{Binding TransactionsList}"
SelectionMode="None"> SelectionMode="None">
@ -38,8 +46,9 @@
Margin="10, 0, 0, 0"/> Margin="10, 0, 0, 0"/>
<Label <Label
Text="{Binding Sum, StringFormat='{0} €'}" Text="{Binding Sum, Converter={StaticResource int2StringConverters}, ConverterParameter={Binding Type}, StringFormat='{0} €'}"
Grid.Column="2" Grid.Column="2"
TextColor="{Binding Type, Converter={StaticResource bool2ColorConverters}}"
VerticalOptions="Center" VerticalOptions="Center"
HorizontalOptions="End" HorizontalOptions="End"
Margin="0, 0, 20, 0"/> Margin="0, 0, 20, 0"/>

@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Banquale.Views.Converters
{
public class Bool2ColorConverters : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
bool NewValue = (bool) value;
if (NewValue== true)
return Colors.Red;
return Colors.Green;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}

@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Banquale.Views.Converters
{
public class Int2StringConverters : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
string res;
Debug.WriteLine(parameter);
//return value.ToString();
//if (parameter is not bool)
// return false;
bool NewParameter = (bool)parameter;
if (NewParameter)
res = "-" + (string)value;
else
res = "+" + (string)value;
return res;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}

@ -2,8 +2,12 @@
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui" <ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Banquale.Views.TransactionsPage" x:Class="Banquale.Views.TransactionsPage"
xmlns:conv="clr-namespace:Banquale.Views.Converters"
Title="TransactionsPage"> Title="TransactionsPage">
<ContentPage.Resources>
<conv:Bool2ColorConverters x:Key="bool2ColorConverters"/>
</ContentPage.Resources>
<!--<Grid RowDefinitions="2*, *, 0*, *, 0*, *, *, *, *, *, *"> <!--<Grid RowDefinitions="2*, *, 0*, *, 0*, *, *, *, *, *, *">
<Grid Grid.Row="1" ColumnDefinitions="2*, *, 2*"> <Grid Grid.Row="1" ColumnDefinitions="2*, *, 2*">
@ -40,6 +44,7 @@
<Label <Label
Text="{Binding Sum, StringFormat='{0} €'}" Text="{Binding Sum, StringFormat='{0} €'}"
HorizontalOptions="Center" HorizontalOptions="Center"
TextColor="{Binding Type, Converter={StaticResource bool2ColorConverters}}"
FontSize="Large" FontSize="Large"
Margin="0, 0, 0, 20"/> Margin="0, 0, 0, 20"/>

Loading…
Cancel
Save