如何在xamarin.forms中绑定Label的Horizo​​ntalOptions属性

问题描述:

如何在Xamarin.Forms中绑定LabelHorizontalOptions属性

How to bind the HorizontalOptions attribute of Label in Xamarin.Forms`

<Label TextColor="#01B6FF"  Text="{Binding RecepientFullName}" FontSize="Small" HorizontalOptions="{Binding TextAlign} />`

    <ContentPage.Resources>
    <ResourceDictionary >
        <local:ChatTextAlignmentConverter x:Key="ChatTextAlignmentConverter">
        </local:ChatTextAlignmentConverter>
    </ResourceDictionary>
</ContentPage.Resources>


<Frame  Margin="10,0,10,0" Padding="10,5,10,5"  HorizontalOptions="{Binding TextAlign, Converter={StaticResource ChatTextAlignmentConverter}}" BackgroundColor="{Binding BackgroundColor}"/>


 public class ChatTextAlignmentConverter: IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value != null)
        {
            string valueAsString = value.ToString();
            switch (valueAsString)
            {
                case ("EndAndExpand"):
                    {
                        return LayoutOptions.EndAndExpand;
                    }
                case ("StartAndExpand"):
                    {
                        return LayoutOptions.StartAndExpand;
                    }
                default:
                    {
                        return LayoutOptions.StartAndExpand;
                    }
            }
        }
        else
        {
            return LayoutOptions.StartAndExpand;
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return null;
    }
}