C# 如何在xamarin.forms中绑定标签的HorizontalOptions属性
如何在Xamarin.Forms中绑定C# 如何在xamarin.forms中绑定标签的HorizontalOptions属性,c#,xamarin,xamarin.forms,C#,Xamarin,Xamarin.forms,如何在Xamarin.Forms中绑定标签的水平选项属性` <Label TextColor="#01B6FF" Text="{Binding RecepientFullName}" FontSize="Small" HorizontalOptions="{Binding TextAlign} />` 公共类ChatterXtAlignmentConverter:IValueConverter { 公共对象转换(对象值、类型targetType、对象参数、CultureInfo
标签的水平选项
属性`
<Label TextColor="#01B6FF" Text="{Binding RecepientFullName}" FontSize="Small" HorizontalOptions="{Binding TextAlign} />`
公共类ChatterXtAlignmentConverter:IValueConverter
{
公共对象转换(对象值、类型targetType、对象参数、CultureInfo区域性)
{
if(值!=null)
{
字符串值string=value.ToString();
开关(值字符串)
{
案例(“EndAndExpand”):
{
返回LayoutOptions.EndAndExpand;
}
案例(“开始和扩展”):
{
返回LayoutOptions.StartAndExpand;
}
违约:
{
返回LayoutOptions.StartAndExpand;
}
}
}
其他的
{
返回LayoutOptions.StartAndExpand;
}
}
公共对象转换回(对象值、类型targetType、对象参数、CultureInfo区域性)
{
返回null;
}
}
看起来很结实。。。至少从你发布的内容来看。bindingcontext类在哪里?哦……是的,我做到了……我得到了解决方案,我必须使用值转换器……顺便说一句thanks@nitu你能把你的答案贴出来吗?这可能会帮助有相同问题的人=)是的,你可以参考
<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;
}
}