locked
Custom Bindable Property Type Mismatch RRS feed

  • Question

  • User74943 posted

    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.

    Wednesday, August 5, 2015 3:35 PM

All replies

  • User28549 posted

    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:

    Wednesday, August 5, 2015 3:52 PM
  • User119704 posted

    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.

    Wednesday, August 5, 2015 3:57 PM
  • User74943 posted

    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.

    Wednesday, August 5, 2015 4:02 PM
  • User119704 posted

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

    Wednesday, August 5, 2015 4:08 PM
  • User193689 posted

    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..

    Wednesday, February 10, 2016 3:17 PM
  • User119704 posted

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

    Wednesday, February 10, 2016 3:28 PM
  • User180261 posted

    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 ...

    Thursday, March 24, 2016 10:10 AM
  • User140275 posted

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

    Thursday, March 31, 2016 9:15 AM
  • User105043 posted

    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/

    Thursday, April 14, 2016 12:10 AM
  • User180261 posted

    Hi, thanks @DavidCASBONNE .

    I'll try this next week.

    Thursday, April 14, 2016 7:34 AM
  • User140275 posted

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

    Thursday, April 14, 2016 9:11 AM
  • User105043 posted

    @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!

    Friday, April 15, 2016 2:34 AM