自定义ViewCell可绑定属性不起作用

时间:2018-07-12 10:21:02

标签: uitableview xaml xamarin.forms

我为TableView创建了ViewCell(自定义EntryCell),但该值未绑定到我的ViewModel。 如果我创建默认的EntryCell,则一切正常,但我的自定义ViewCell无法绑定。

enter image description here

我只是不明白怎么了,我真的希望得到您的帮助

EntryCell.xaml:

<?xml version="1.0" encoding="utf-8" ?>
<ViewCell xmlns="http://xamarin.com/schemas/2014/forms"
          xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
          x:Class="AproReportsClient.UI.Controls.EntryCell"
          x:Name="This"
          BindingContext="{x:Reference This}">

    <Entry Text="{Binding Text}"
           Placeholder="{Binding Placeholder}"         
           TextColor="{Binding TextColor}"
           FontSize="{Binding FontSize}" Margin="12, 8, 8, 8"
           HorizontalOptions="Fill" VerticalOptions="FillAndExpand"/>

</ViewCell>

EntryCell.xaml.cs:

namespace AproReportsClient.UI.Controls
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class EntryCell : ViewCell
    {
        public static readonly BindableProperty PlaceholderProperty =
            BindableProperty.Create(propertyName: nameof(Placeholder),
                                    returnType:   typeof(string),
                                    declaringType:typeof(EntryCell),
                                    defaultValue: string.Empty);

        public string Placeholder
        {
            get => (string)GetValue(PlaceholderProperty);
            set => SetValue(PlaceholderProperty, value);
        }


        public static readonly BindableProperty TextProperty =
            BindableProperty.Create(propertyName: nameof(Text),
                                    returnType:   typeof(string),
                                    declaringType:typeof(EntryCell),
                                    defaultValue: string.Empty);

        public string Text
        {
            get => (string)GetValue(TextProperty);
            set => SetValue(TextProperty, value);
        }


        public static readonly BindableProperty TextColorProperty =
            BindableProperty.Create(propertyName: nameof(TextColor),
                                    returnType:   typeof(Color),
                                    declaringType:typeof(EntryCell),
                                    defaultValue: Color.Black);

        public Color TextColor
        {
            get => (Color)GetValue(TextColorProperty);
            set => SetValue(TextColorProperty, value);
        }


        public static readonly BindableProperty FontSizeProperty =
            BindableProperty.Create(propertyName: nameof(FontSize),
                                    returnType:   typeof(double),
                                    declaringType:typeof(EntryCell),
                                    defaultValue: 10d);

        public double FontSize
        {
            get => (double)GetValue(FontSizeProperty);
            set => SetValue(FontSizeProperty, value);
        }


        public EntryCell ()
        {
            InitializeComponent ();
        }
    }
}

页面:

//....

<TableView Grid.Row="1" 
                       HasUnevenRows="True">
                <TableRoot>
                    <TableSection>
                        <EntryCell Text="{Binding Title}"/>

                        <controls:EntryCell Placeholder="{extensions:Translate Title}"
                                            Text="{Binding Title}"
                                            TextColor="{StaticResource PrimaryColor}"
                                            FontSize="{StaticResource HeaderFontSize}"/>
                    </TableSection>

                    <TableSection Title="{extensions:Translate AdditionalFields}">
                        <controls:EmptyCell EmptyHeight="150"/>
                    </TableSection>
                </TableRoot>
            </TableView>

//....

我还检查了TextChanged事件处理程序中的text属性为null。没有设置,但是为什么呢?

2 个答案:

答案 0 :(得分:0)

尝试在Cell构造函数中设置BindingContext。

 public EntryCell ()
 {
      InitializeComponent ();
      BindingContext = this;
 }

答案 1 :(得分:0)

看来,ViewCell中的属性绑定存在问题。有人可以创建扩展XF EntryCell的新CustomEntryCell。在此类中,应覆盖OnAppearing。在OnAppearing方法中,EntryCell的属性可以通过BindedContext归档。 BindedContext是包含标题的对象。