Share via

PartialView question

Stesvis 1,041 Reputation points
2021-01-09T01:09:12.497+00:00

I have a custom view wired to its own view model using Prism:
prism:ViewModelLocator.AutowireViewModel="True"
MyCustomView -> MyCustomViewViewModel

This custom view has a bindable property called Target:

public partial class MyCustomView: ContentView
    {
        public static readonly BindableProperty TargetProperty = BindableProperty.Create(
            propertyName: nameof(Target),
            returnType: typeof(string),
            declaringType: typeof(MyCustomView),
            defaultValue: "");

        public string Target
        {
            get { return (string)GetValue(TargetProperty); }
            set { SetValue(TargetProperty, value); }
        }

        //...
    }

So i can use it like this: <views:MyCustomView Target="something" />

The question is, how can I read the value of Target from CustomViewViewModel??

Developer technologies | .NET | Xamarin
0 comments No comments

1 answer

Sort by: Most helpful
  1. JessieZhang-MSFT 7,721 Reputation points Microsoft External Staff
    2021-01-11T09:16:35.34+00:00

    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.

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.