简体   繁体   中英

how to bind HorizontalOptions property of label in xamarin.forms

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

<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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM