I don't know if the title is clear enough, so let me explain what I am trying to do:
I have a LoggingField
class which is composed of the following:
XAML:
<ContentView.Content>
<StackLayout>
<Label Style="{DynamicResource LabelTitle}" x:Name="LabelTitle"/>
<View x:Name="FieldContent"/>
</StackLayout>
</ContentView.Content>
C#:
[ContentProperty(nameof(Field))]
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class LoggingField : ContentView
{
/// <summary>
/// Creates a new Logging field.
/// </summary>
public LoggingField()
{
InitializeComponent();
}
//
// PUBLIC
//
// PROPERTIES
/// <summary>
/// The field's content.
/// </summary>
public View Field
{
set => FieldContent = value;
get => FieldContent;
}
/// <summary>
/// The field's title.
/// </summary>
public string Text
{
set => LabelTitle.Text = value;
get => LabelTitle.Text;
}
}
Page XAML:
<logcontent:LoggingField Text="Test Title:">
<Entry Placeholder="PlaceholderText"/>
</logcontent:LoggingField>
From it, you can see that I want the <Entry Placeholder="PlaceholderText"/>
to override the <View x:Name="FieldContent"/>
object, but I can't make it work. It would always overwrite the entire ContentView, displaying only the placeholder Entry
.
The only advancement I've made was adding [ContentProperty(nameof(Field))]
in the ContentView's C# class, which made its original content be displayed but then the Entry
object won't appear anymore.
So how can I make the Entry
object correctly override the View
object inside the ContentView? Thanks in advance.
PS: I've tried replacing <View x:Name="FieldContent"/>
with <ContentPresenter x:Name="FieldContent"/>
and using ContentPresenter.Content
instead, but to no avail.
You can create ControlTemplate
in code behind(viewmodel), and bind it in xaml.
ControlTemplate MyTemplate = new ControlTemplate(()=> {
return new Entry { Placeholder= "PlaceholderText" };
});
<local:LoggingField ControlTemplate="{Binding MyTemplate}"/>
However, this is total not recommended, the best way is to place
ControlTemplate
in xaml.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.