Content Page with Bindable Property not setting the value

LucasLFLucasLF BRMember ✭✭
edited February 2016 in Xamarin.Forms

Hey guys! I have a tabbed page GroupDetailView that I create an instance of the MatchListView with the Bindable Property IdGroup receives the Binding Property of GroupDetailViewModel IdGroup, but the Bindable Property is not setting the value. What's wrong? Thanks.

.CS
<br /> public partial class GroupDetailView : TabbedPage<br /> {<br /> public GroupDetailView (int id)<br /> {<br /> this.BindingContext = new GroupDetailViewModel (id);<br /> InitializeComponent ();<br /> }<br /> }<br />

VIEWMODEL
<br /> private int idGroup;<br /> public int IdGroup {<br /> get { return idGroup; }<br /> set {<br /> idGroup = value;<br /> this.Notify ("IdGroup");<br /> }<br /> }</p> <p>public GroupDetailViewModel (int id)<br /> {<br /> this.IdGroup = id;<br /> this.GetGroup (id);<br /> }<br />

XAML
<br /> <TabbedPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="Revesgueio.Views.GroupDetailView" xmlns:local="clr-namespace:Revesgueio.Views;assembly=Revesgueio" Title="Grupo" BackgroundColor="{StaticResource PrimaryColorLight}"><br /> &lt;TabbedPage.Children><br /> <ContentPage Title="Grupo" BackgroundColor="{StaticResource PrimaryColorLight}"><br /> &lt;ContentPage.ToolbarItems><br /> <ToolbarItem Name="Editar" Order="Primary" Priority="0" Command="{ Binding EditCommand }"><br /> </ToolbarItem><br /> &lt;/ContentPage.ToolbarItems><br /> &lt;ContentPage.Content><br /> ...<br /> &lt;/ContentPage.Content><br /> </ContentPage></p> <pre><code> <local:MatchListView IdGroup="{ Binding IdGroup }" /> </TabbedPage.Children>


MATCHLISTVIEW

<br /> public partial class MatchListView : ContentPage<br /> {<br /> public static readonly BindableProperty IdGroupProperty =<br /> BindableProperty.Create&lt;MatchListView, int> (c => c.IdGroup, default(int), BindingMode.OneWay, null);</p> <pre><code>public int IdGroup { get { return (int)GetValue(IdGroupProperty); } set { SetValue(IdGroupProperty, value); } } public MatchListView () { InitializeComponent (); this.BindingContext = new MatchListViewModel (); }...

Answers

  • JulienRosenJulienRosen CAMember ✭✭✭✭

    it is really hard to read your question. try to format it better.

  • LucasLFLucasLF BRMember ✭✭

    @JulienRosen it's better?

    .CS

    public partial class GroupDetailView : TabbedPage { public GroupDetailView (int id) { this.BindingContext = new GroupDetailViewModel (id); InitializeComponent (); } }

    VIEWMODEL

    private int idGroup; public int IdGroup { get { return idGroup; } set { idGroup = value; this.Notify ("IdGroup"); } } public GroupDetailViewModel (int id) { this.IdGroup = id; this.GetGroup (id); }

    XAML

    TabbedPage... TabbedPage.Children... local:MatchListView IdGroup="{ Binding IdGroup }" TabbedPage.Children TabbedPage

    MATCHLISTVIEW

    public partial class MatchListView : ContentPage { public static readonly BindableProperty IdGroupProperty = BindableProperty.Create<MatchListView, int> (c => c.IdGroup, default(int), BindingMode.OneWay, null); public int IdGroup { get { return (int)GetValue(IdGroupProperty); } set { SetValue(IdGroupProperty, value); } } public MatchListView () { InitializeComponent (); this.BindingContext = new MatchListViewModel (); }

  • LucasLFLucasLF BRMember ✭✭

    up!

  • AnthonyRamirezAnthonyRamirez USUniversity ✭✭✭
    edited February 2016

    Hi @LucasLF

    I noticed in the first example the BP is defined using

    BindableProperty.Create<MatchListView, int> (c => c.IdGroup, default(int), BindingMode.OneWay, null);

    but in the second example its defined as

    public static readonly BindableProperty IdGroupProperty =
    BindableProperty.Create (c => c.IdGroup, default(int), BindingMode.OneWay, null);

    It may be worth checking that it is defined properly.

    The other thing is that the constructor of MatchListView is set the BindingContext. I believe this is causing the local:MatchListView IdGroup="{ Binding IdGroup }" to look for IdGroup on MatchListViewModel and not GroupDetailViewModel .

    To confirm this, check the output window while running the debugger. It should give you an error saying that it cannot find IdGroup on MatchListViewModel.

    Good Luck!

  • LucasLFLucasLF BRMember ✭✭

    @AnthonyRamirez said:
    Hi @LucasLF

    I noticed in the first example the BP is defined using

    BindableProperty.Create<MatchListView, int> (c => c.IdGroup, default(int), BindingMode.OneWay, null);

    but in the second example its defined as

    public static readonly BindableProperty IdGroupProperty =
    BindableProperty.Create (c => c.IdGroup, default(int), BindingMode.OneWay, null);

    It may be worth checking that it is defined properly.

    The other thing is that the constructor of MatchListView is set the BindingContext. I believe this is causing the local:MatchListView IdGroup="{ Binding IdGroup }" to look for IdGroup on MatchListViewModel and not GroupDetailViewModel .

    To confirm this, check the output window while running the debugger. It should give you an error saying that it cannot find IdGroup on MatchListViewModel.

    Good Luck!

    Hey @AnthonyRamirez thanks for answer. You're right, it's showing an error 'Binding: 'IdGroup' property not found on 'Revesgueio.ViewModels.MatchListViewModel', target property: 'Revesgueio.Views.MatchListView.IdGroup'. It's right because the GroupDetailViewModel should give the IdGroup, how I get the IdGroup in the right Model?

  • AnthonyRamirezAnthonyRamirez USUniversity ✭✭✭

    Cool! You can use Source to reference a x:Named control

    <local:MatchListView IdGroup="{Binding Source={x:Reference TheNameOfYourGroupDetailView}, Path=BindingContext.IdGroup }"

    OR

    You can use Source to reference the ViewModel via a StaticResource if you are using something like a ViewModelLocator

    <local:MatchListView IdGroup="{Binding Source={x:StaticResource Locator}, Path=GroupDetailViewModel.IdGroup }"

  • LucasLFLucasLF BRMember ✭✭

    @AnthonyRamirez my bindable property is not working as well hehehe I made this sample test and not worked as expected the IdGroup property remains 0!!!

    <local:MatchListView IdGroup="2" />
    
  • LucasLFLucasLF BRMember ✭✭

    @AnthonyRamirez The Source I put the x:Name in my Tabbed Page xaml like:

    <TabbedPage x:Name="GroupDetailView">
    

    and after

    <local:MatchListView IdGroup="{ Binding Source={x:Reference GroupDetailView}, Path=BindingContext.Group.Id }" />
    

    But the initial problem is my bindable property, I thought that was working...

Sign In or Register to comment.