A Microsoft framework for building cross-platform mobile apps using .NET and C# with native performance and user interfaces.
Hello,
Welcome to our Microsoft Q&A platform!
For how to use custom BindableProperty in ContentView, you can check the official document here: https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/layouts/contentview.
You can refer to the sample code :
public partial class CardView : ContentView
{
public static readonly BindableProperty CardTitleProperty = BindableProperty.Create(nameof(CardTitle), typeof(string), typeof(CardView), string.Empty);
public static readonly BindableProperty CardDescriptionProperty = BindableProperty.Create(nameof(CardDescription), typeof(string), typeof(CardView), string.Empty);
public static readonly BindableProperty BorderColorProperty = BindableProperty.Create(nameof(BorderColor), typeof(Color), typeof(CardView), Color.DarkGray);
public static readonly BindableProperty CardColorProperty = BindableProperty.Create(nameof(CardColor), typeof(Color), typeof(CardView), Color.White);
public static readonly BindableProperty IconImageSourceProperty = BindableProperty.Create(nameof(IconImageSource), typeof(ImageSource), typeof(CardView), default(ImageSource));
public static readonly BindableProperty IconBackgroundColorProperty = BindableProperty.Create(nameof(IconBackgroundColor), typeof(Color), typeof(CardView), Color.LightGray);
public string CardTitle
{
get => (string)GetValue(CardView.CardTitleProperty);
set => SetValue(CardView.CardTitleProperty, value);
}
public string CardDescription
{
get => (string)GetValue(CardView.CardDescriptionProperty);
set => SetValue(CardView.CardDescriptionProperty, value);
}
public Color BorderColor
{
get => (Color)GetValue(CardView.BorderColorProperty);
set => SetValue(CardView.BorderColorProperty, value);
}
public Color CardColor
{
get => (Color)GetValue(CardView.CardColorProperty);
set => SetValue(CardView.CardColorProperty, value);
}
public ImageSource IconImageSource
{
get => (ImageSource)GetValue(CardView.IconImageSourceProperty);
set => SetValue(CardView.IconImageSourceProperty, value);
}
public Color IconBackgroundColor
{
get => (Color)GetValue(CardView.IconBackgroundColorProperty);
set => SetValue(CardView.IconBackgroundColorProperty, value);
}
public CardView()
{
InitializeComponent();
}
}
And use like this:
<controls:CardView BorderColor="DarkGray"
CardTitle="Colette Quint"
CardDescription="In pellentesque odio eget augue elementum lobortis. Sed augue massa, rhoncus eu nisi vitae, egestas."
IconBackgroundColor="SlateGray"
IconImageSource="user.png" />
And you can check the full sample here: https://github.com/xamarin/xamarin-forms-samples/tree/master/UserInterface/ContentViewDemos
Best Regards,
Jessie Zhang
---
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.