如何在Xamarin.Forms中绑定标签(Label)的HorizontalOptions属性

3
如何在Xamarin.Forms中绑定Label的HorizontalOptions属性
<Label TextColor="#01B6FF"  Text="{Binding RecepientFullName}" FontSize="Small" HorizontalOptions="{Binding TextAlign} />`

看起来很不错...至少从你发布的内容来看。不过 BindingContext 类在哪里呢? - woelliJ
哦...是的,我做到了...我找到了解决方案,我必须使用值转换器...顺便说一句,谢谢。 - priya_d
@nitu,你能把你的解决方案发表为答案吗?这可能会帮助遇到同样问题的人 =) - Diego Rafael Souza
是的,你可以参考。 - priya_d
1个回答

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接