Amr
Amr

Reputation: 57

How to use Converter to bind TextAlignment to string property

Simply i want to make converter to bind TextAlignment option enum to string property .. i found converter is the solution after long searching but i'm still can't do specific one for my case .. any help ? I use this code

EnumtoStringConverter.cs

    class EnumtoStringConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var returnValue = value as string;

        if (returnValue == "Start")
            return "Start";

        else if (returnValue == "Center")
            return "Center";

        else if (returnValue == "End")
            return "End";

        return returnValue;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {

        }
    }

Xaml

<StackLayout Orientation="Horizontal"  HeightRequest="40" HorizontalOptions="Fill" BackgroundColor="White">
                        <controls:ExtendedButton HorizontalContentAlignment="{Binding HoriRLLR}" Margin="20,0,0,0" Image="house.png"  HorizontalOptions="FillAndExpand" BackgroundColor="White" Text="{translator:Translate HomeSpacing}"></controls:ExtendedButton>

                    </StackLayout>

HoriRLLR is the string property which i want to bind

I know i should write code in convert method but i don't know What do I miss? and if i'm in right way or not ?

Upvotes: 0

Views: 136

Answers (1)

amr kamal
amr kamal

Reputation: 58

You don't need to do this you can make enum property and bind direct to this property Use TextAlignment for this your code will be like

Private Enum alignment{get;set;}

and in constructor

alignment = Textalignment.End

in xaml horizontaloption bind in aignment property

Upvotes: 1

Related Questions