Я создал ViewCell (пользовательский EntryCell) для TableView, но значение не привязано к моей 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. Нет набора, но почему?





Попробуйте установить свой BindingContext в конструкторе Cell.
public EntryCell ()
{
InitializeComponent ();
BindingContext = this;
}
Похоже, есть проблема с привязкой свойств во ViewCell. Кто-то может создать новую CustomEntryCell, которая расширяет XF EntryCell. OnAppearing следует переопределить в этом классе. В методе OnAppearing свойства EntryCell могут быть сохранены с помощью BindedContext. BindedContext - это ваш объект, который содержит заголовок.
Спасибо, но это не помогло. Метод Get всегда выполняется в свойствах, а метод Set - нет.