【问题标题】:How to use Converter to bind TextAlignment to string property如何使用 Converter 将 TextAlignment 绑定到字符串属性
【发布时间】:2018-09-29 21:09:43
【问题描述】:

我只是想让转换器将 TextAlignment 选项枚举绑定到字符串属性.. 经过长时间的搜索,我发现转换器是解决方案,但我仍然无法针对我的情况做特定的解决方案.. 有什么帮助吗? 我用这个代码

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 是我要绑定的字符串属性

我知道我应该在 convert 方法中编写代码,但我不知道我错过了什么?如果我的方式正确吗?

【问题讨论】:

  • 这可能会有所帮助...stackoverflow.com/questions/30095689/…
  • 很好,但是当我绑定属性时没有任何改变:/
  • 在您的ExtendedButton 类中,可绑定属性HorizontalContentAlignment 的类型是什么?
  • @apineda 枚举,它是 Xlabs 组件

标签: c# xamarin.forms


【解决方案1】:

您不需要这样做,您可以创建枚举属性并直接绑定到此属性为此使用 TextAlignment 你的代码会像

Private Enum alignment{get;set;}

在构造函数中

alignment = Textalignment.End

在xml中 对齐属性中的水平选项绑定

【讨论】:

  • 非常感谢它帮助了我
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-01-29
  • 2010-10-16
  • 1970-01-01
  • 1970-01-01
  • 2019-12-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多