0

Hi I had defined Property in My BookViewVM

 private Visibility _isLoaderPageView = Visibility.Hidden; public Visibility IsLoaderPageView { get { return _isLoaderPageView; } set { _isLoaderPageView = value; RaisePropertyChanged("IsLoaderPageView"); } } 

I am calling this bookViewVM on Page.xaml page but on Page.xaml some other PageViewVM had already bind But I have to call the BookViewVM Property:

<views:LoadingAnimation x:Name="ldrControl" Margin="100,100,100,150" Visibility="{Binding IsLoaderPageView}" /> 

I am able to access this BookViewVM Property on My Pageview.xaml.cs code Like this: ETBApp.MainVM.BookVM.IsLoaderPageView

But I have to call this from Property binding on page. please tell me how to bind it.

Both ViewModel like BookViewVm and PageViewVm on same namesapce.

1
  • In MVVM, each part of the View should have a single ViewModel. If your LoadingAnimation requires that property, then that property should be in its ViewModel (set through its DataContext property). Can't you move it from BookViewVM to PageViewVM instead? Or expose the property or the whole BookViewVM through a property in PageViewVM? Commented Jul 3, 2015 at 9:05

2 Answers 2

1

The problem is that you have multiple DataContexts and by default Binding works with the DataContext of current element if it is specified, and datacontext of parent if not(and so on).

so there is 2 simple solutions:

  • specify DataContext of element directly(only if that element does not have one)
  • use ElementName (if you have access in xaml to specific element and it must be named properly through Name or x:Name), or RelativeSource(if you bind to some ancestor) inside Binding with Path
Sign up to request clarification or add additional context in comments.

1 Comment

This answer would be improved if it included code snippets for the solutions.
0

Just set properly DataContext. For example in grid

<Grid.DataContext> <viewmodel:YourViewModel /> </Grid.DataContext> 

And now you can bind it

<ItemsControl ItemsSource="{Binding Model.ButtonsRaw1}"> 

There we bind ItemsSource from our model.

Or you have an alternative way to do it from Resource. SOmething like this

<UserControl.Resources> <Style TargetType="local:YourType"> <Setter Property="Text" Value="{Binding Path=DataContext.Model.Text, Mode=TwoWay,ElementName=YourGridName}"/> </Style> </UserControl.Resources> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.