【发布时间】:2018-12-20 12:50:38
【问题描述】:
我为 TableView 创建了 ViewCell(自定义 EntryCell),但该值没有绑定到我的 ViewModel。 如果我创建默认的 EntryCell,那么一切正常,但我的自定义 ViewCell 没有绑定。
我就是不明白怎么回事,真的希望得到你的帮助
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 。没有设置,但是为什么呢?
【问题讨论】:
标签: uitableview xaml xamarin.forms