1

I have a WPF project. In that I have 3 views and one viewmodel.

1.View 1.Xaml, View1viewmodel.cs 2.View 2.Xaml 3.View 3.Xaml

View 3 is referred in view 2 like below

<ScrollViewer>
    <local:FeatureView x:Name="View3" Margin="0,5,5,0" Visibility="{Binding 
        IsFeatureView,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}" />
</ScrollViewer> 

and View2 is referred in view 1 like below

<StackPanel>
    <local:ProjectConfigurations x:Name="View2" />
<StackPanel/>

Datacontext in defined in View1

<Window.DataContext>
    <local:View1ViewModel />
</Window.DataContext>

now i need to command bind a check box control of View 3. the property is defined in View1ViewModel.but am unable to achieve that, please any one guide me to achieve this

4
  • 2
    Sorry but can you change the format of your question a little bit, Your explanation is very confusing Commented Jun 15, 2015 at 12:34
  • command binding is not working in my project. I have 3 different view.xaml, and one commaon viewmodel.cs.view3 is local 2 view2 . view 2 is local 2 view 1. how to apply command binding to view 3. i need to use the viewmodel.cs of view1. Commented Jun 15, 2015 at 12:50
  • 1
    Can you check the Output tab in Visual Studio and see if any Binding error appears there? It could be helpful to know the exact error Commented Jun 15, 2015 at 12:51
  • Also, does the Visibility binding in FeatureView work correctly? Have you checked you're not changing the DataContext at some point in the visual tree? (some WPF tool like Snoop would be helpful to check that) Commented Jun 15, 2015 at 12:52

1 Answer 1

1

You just need to use a RelativeSource Binding to access the property from the view model.

In view 3:

<Checkbox Content="{Binding DataContext.PropertyInViewModel, 
    RelativeSource={RelativeSource AncestorType={x:Type YourPrefix:View1}}}"
Sign up to request clarification or add additional context in comments.

3 Comments

I have tried relative source binding it doesn't work for view 3. if i use the same in view 2 means its working. so am confused
If view3 is in view2 and view2 in is view1, then a RelativeSource Binding will work, so perhaps you should try again. Don't forget to include DataContext. in the Binding Path as I have above.
I have used this in view 3 Command="{Binding DataContext.FeatureChecked, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type local:MainWindow}}}" CommandParameter="{Binding ElementName=Features}"/> is this correct?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.