Custom Bindable Property Type Mismatch

BNBerardBNBerard USMember, University

I'm having an issue creating a BindableProperty on a custom view. I used a working piece of code as a template and am nearly positive it's a match but there is still the type mismatch error on the binding.

PortfolioHeaderView Property code:

public static readonly BindableProperty PortfolioHeaderProperty = BindableProperty.Create("HeaderModel", typeof(PortfolioHeader), typeof(PortfolioHeaderView), null);
public PortfolioHeader HeaderModel { get { return (PortfolioHeader)GetValue(PortfolioHeaderProperty); } set { SetValue(PortfolioHeaderProperty, value); } }

ViewModel Source:

public class PortfolioLandingViewModel { public PortfolioHeader HeaderInfo {get; set;} }

XAML in page containing the custom view:

<local:PortfolioHeaderView HeightRequest="300" HorizontalOptions="FillAndExpand" HeaderModel="{Binding HeaderInfo}"/>

Exception:

Position 18:83. Cannot assign property "HeaderModel": type mismatch between "Xamarin.Forms.Binding" and "CSSPMobile.PortfolioHeader"

Not sure why it's giving me the type mismatch, hoping another set of eyes may be able to spot something I'm missing.

Posts

  • ChaseFlorellChaseFlorell CAInsider, University mod
    edited August 2015

    There are a few examples already out there, so I won't re-write it. I've got an example of a Markdown View here.

    Maybe setup your BindableProperty like this.

    public static readonly BindableProperty HeaderProperty = BindableProperty.Create<PortfolioHeaderView, PortfolioHeader>(p => p.Header, default(PortfolioHeader));
    
    public PortfolioHeader Header
    { 
        get { return (PortfolioHeader) GetValue(HeaderProperty); }
        set { SetValue(HeaderProperty, value); }
    }
    

    And make sure your ViewModel is properly signaling the value update.

        private PortfolioHeader _headerInfo;
        public PortfolioHeader HeaderInfo
        {
            get { return _headerInfo; }
            set
            {
                _headerInfo= value;
                OnPropertyChanged();
            }
        }
    

    Then in your xaml, you bind like so.

    <local:PortfolioHeaderView HeightRequest="300" 
                               HorizontalOptions="FillAndExpand" 
                               Header="{Binding HeaderInfo}"/>
    

    please forgive any typo's, I'm not in front of my IDE :smile:

  • stvansolanostvansolano UMInsider, University ✭✭✭

    Hey @BNBerard

    I think the issue might be in the bindable property declaration to explicitly handle the type being declared. Try this declaration instead:

        public static readonly BindableProperty PortfolioHeaderProperty = BindableProperty.Create<PortfolioHeaderView, HeaderModel>(p => p.HeaderInfo, default(HeaderModel), BindingMode.OneWay, null, OnHeaderInfoChanged);
    
        private static void OnHeaderInfoChanged(BindableObject bindable, HeaderModel oldValue, HeaderModel newValue)
        {
            var source = bindable as PortfolioHeaderView;
            // Call source NotifyPropertyChanged or whatever you need with oldValue and newValue when changed
        }
    

    Here is a working BindableProperty example that you could check with.

  • BNBerardBNBerard USMember, University

    Thanks @ChaseFlorell and @stvansolano . I used the first example and it worked. I guess a little clarity about which is best and why exactly the Create method I used didn't work would help as well.

  • stvansolanostvansolano UMInsider, University ✭✭✭
    edited August 2015

    Yeah tricky stuff! Great to see it worked for you :wink:

  • ClmentNonn.8916ClmentNonn.8916 USMember
    edited February 2016

    Hi,
    I try to integrate a similar stuff, and got the same issue...
    I have to create a Breadcrumb, so I have the visibility level as a enumeration value (3 step maximum)
    I have this custom enumeration :

    public enum BreadcrumbStepVisible { None = 0, Step1, Step2, Step3 }
    

    I bound it into my ContentView :

    public static readonly BindableProperty StepVisibleProperty = BindableProperty.Create<Breadcrumb,
        BreadcrumbStepVisible>(t => t.StepVisibility, default(BreadcrumbStepVisible));
    
    public BreadcrumbStepVisible StepVisibility
    {
         get { return (BreadcrumbStepVisible)GetValue(StepVisibleProperty); }
         set
         {
              SetValue(StepVisibleProperty, value);
         }
     }
    

    Then I use it in my xaml :

     <t:Breadcrumb BackgroundColor="#3B6AA0" HeightRequest="31"  StepVisibility="Step2" />
    

    It works that way, but when I want to use MVVM and bind it into my viewmodel, it does't work...

     <t:Breadcrumb BackgroundColor="#3B6AA0" HeightRequest="31"  StepVisibility="{Binding BreadcrumbVisibility}" />
    
    
     Xamarin.Forms.Xaml.XamlParseException: Position 15:151. Cannot assign property "StepVisibility": type mismatch between "Xamarin.Forms.Binding" and "XXXX.Views.Controls.BreadcrumbStepVisible"
    

    Thanks for your help !!

    Edit :
    It works !

    The BindableProperty didn't have the good name relatively to the property :

    StepVisibleProperty
    StepVisibility
    

    I renamed the Binding to StepVisibilityProperty and it works for now..

  • stvansolanostvansolano UMInsider, University ✭✭✭

    Awesome @ClmentNonn.8916 ! This breadcrumb control looks interested. Do you have some more code from it for share?

  • AngelR0909AngelR0909 FRMember, University ✭✭

    Hi,

    Working workaround described is marked as obsolete on my Xamarin.Forms.2.1.0.6529.

    New way don't work as @BNBerard showed it.

    I go crazy with this kind of Xamarin Forms things ...

  • jerome.tonnjerome.tonn USMember ✭✭

    Hi,
    what is the alternative approach for BindableProperty.Create?
    As @AngelR0909 described this is marked as obsolete.

  • DavidCASBONNEDavidCASBONNE USMember ✭✭

    Hi @jerome.2638,

    I don't know if you have had your answer but for people like me who comes to un unanswer question, here is the answer :)

    public static readonly BindableProperty PortfolioHeaderProperty = 
          BindableProperty.Create("PortfolioHeader", typeof(PortfolioHeaderView), typeof(HeaderModel), null);
    

    For the other parameter you could look at https://developer.xamarin.com/api/member/Xamarin.Forms.BindableProperty.Create/p/System.String/System.Type/System.Type/System.Object/Xamarin.Forms.BindingMode/Xamarin.Forms.BindableProperty+ValidateValueDelegate/Xamarin.Forms.BindableProperty+BindingPropertyChangedDelegate/Xamarin.Forms.BindableProperty+BindingPropertyChangingDelegate/Xamarin.Forms.BindableProperty+CoerceValueDelegate/Xamarin.Forms.BindableProperty+CreateDefaultValueDelegate/

  • AngelR0909AngelR0909 FRMember, University ✭✭

    Hi, thanks @DavidCASBONNE .

    I'll try this next week.

  • jerome.tonnjerome.tonn USMember ✭✭

    Hi thanks @DavidCASBONNE
    Many thanks for your reply and no I did not have abn answer.
    So many thanks for that

  • DavidCASBONNEDavidCASBONNE USMember ✭✭

    @AngelR0909 @jerome.tonn You're welcome!
    I'm new to Xamarin and spend a lot of time here to learn and debug and I hate see somebody that as a similar problem having no answer! So I make my possible to complete answers when I can!

Sign In or Register to comment.