0

i am trying to bind my MasterViewModel where i have initiated two original viewModel to one view. But i am not getting any data so i must be doing the binding wrong. I have found several post

I have tried in Xaml

  <Label
                                        x:Name="SectionRequired"
                                        Grid.Row="2"
                                        HorizontalOptions="End"
                                        IsVisible="{Binding PostViewModel.IsRequired, Source={x:Reference PostViewModel}}"
                                        Text="{x:Static resources:AppResources.AlertRequired}"
                                        TextColor="Red" />

And also followed this solution but i was getting an expcetion that its used lika markup extenstion 'local1:PostViewModel' is used like a markup extension but does not derive from MarkupExtension.

https://stackoverflow.com/questions/50307356/multiple-bindingcontexts-on-same-contentpage-two-different-views

My Master

class MasterPostsViewModel : BaseViewModel
    {
        public PostViewModel postViewModel { get; set; }
        public CategoriesViewModel categoriesViewModel { get; set; }

        public MasterPostsViewModel()
        {
            postViewModel = new PostViewModel();
            categoriesViewModel = new CategoriesViewModel();    
        }

    }
}

Conte page

I have set the binding to one field here and that works, buit having to do that for the whole page is not what i want.

  MasterPostsViewModel ViewModel;

   protected override void OnAppearing()
        {
            base.OnAppearing();
          
            BindingContext = ViewModel = new MasterPostsViewModel();
            
            NameRequired.IsVisible = ViewModel.postViewModel.IsRequired;

        }

Can you help please

1 Answers1

0

instead of

IsVisible="{Binding PostViewModel.IsRequired, Source={x:Reference PostViewModel}}"

just use

IsVisible="{Binding postViewModel.IsRequired}"

your property name is postViewModel is lower case

also, get rid of this line - it will break the binding you have setup in the XAML

NameRequired.IsVisible = ViewModel.postViewModel.IsRequired;
Jason
  • 86,222
  • 15
  • 131
  • 146
  • Hi, i am still geting that the property was not found [0:] Binding: 'NameEntry' property not found on 'Balt.ViewModel.MasterPostsViewModel', target property: 'Xamarin.Forms.Entry.Text' – Tomash Vovran Jul 04 '20 at 13:15
  • I assume that "NameEntry" is a property on either `postViewModel` or `categoriesViewModel`, not a direct property of `MasterPostsViewModel`. – Jason Jul 04 '20 at 13:19
  • you didn't show "NameEntry" in any of the code you posted, so I can't tell you what specifically you are doing wrong. But probably your binding path is incorrect. – Jason Jul 04 '20 at 13:25
  • my mistake sorry, its not specifically only name entry its all properties. Do i have to initiate all of them again in my Master? – Tomash Vovran Jul 04 '20 at 13:34
  • I have no idea. I can only comment on the code you actually posted. Is `IsVisible` giving you an error after you fixed it as I demonstrated? – Jason Jul 04 '20 at 13:39
  • sorry i was still specifing bindingcontext in xaml – Tomash Vovran Jul 04 '20 at 13:45