条目显示默认文本,而不显示占位符Xamarin

时间:2018-10-22 11:21:13

标签: c# xamarin mvvm xamarin.forms

运行应用程序时,在第一页上,Entry会显示默认文本0而不是占位符。

我有AddContactPage.xaml

<StackLayout>
    <Entry Placeholder="Enter Class" Text="{Binding Class}"></Entry>
    <Entry Placeholder="Enter Id" Text="{Binding StudentId}"></Entry>
</StackLayout>

属性绑定到条目

public int StudentId { get; set; }
public int Class { get; set; }

如何解决此问题。参见o / p-

enter image description here

3 个答案:

答案 0 :(得分:1)

一个可能的解决方案是用于绑定的字符串属性:

private int _studentId;

public int StudentId 
{ 
    get { return _studentId; }
    set
    {
        SetProperty(ref _studentId, value);
        RaisePropertyChanged("StudentIdString"); // If you're using Prism. You can use any other way to raise the PropertyChanged event 
    }
}

public string StudentIdString
{
    get { return StudentId.ToString(); }
}

就是这样!现在,您可以将StudentIdString绑定到Entry了。对Class执行相同操作,就可以了。


另一个解决问题的方法是Woj建议的转换器:

public class IntToStringConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        int i = (int)value;
        return i.ToString();
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return int.Parse((string)value);
    }
}

然后像这样在您的xaml中使用它:

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

<Entry Placeholder="Enter Id" Text="{Binding StudentId, Converter={StaticResource intToString}}"></Entry>

答案 1 :(得分:0)

由于Entry控件文本已绑定到模型,因此不会显示关联的null属性的占位符。在这种情况下,您可能需要删除文本绑定。

答案 2 :(得分:0)

实际上,您可以使用StringFormat。尝试这个 StringFormat ='{0:#。## ;;}'

相关问题