WPF Bind to ViewModel of another item

Just an example. CustomControl has a ViewModel with the property "Test"

How to associate a text box with this particular property? Can you access the majors ViewModel?

<TextBox Text="{Binding ElementName=myControl, Path=ViewModel.Test}"></TextBox> <Controls:CustomControl x:Name="myControl" /> 
+4
source share
2 answers

Siblings ViewModel will try in its DataContext

 <TextBox Text="{Binding ElementName=myControl, Path=DataContext.Test}"></TextBox> <Controls:CustomControl x:Name="myControl" /> 
+3
source

Perhaps this is what you need -

 <TextBox Text="{Binding Source={x:Static local:VieModel}, Path=Test}"></TextBox> <Controls:CustomControl x:Name="myControl" /> 

Remember to add the markup extension to include the namespace where the ViewModel class exists - xmlns:local="clr-namespace:ViewModel NameSpace"

0
source

Source: https://habr.com/ru/post/1347602/


All Articles