Matt Overall
Matt Overall

Reputation: 329

WPF Bind to ViewModel of another element

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

How could I bind the textbox to that specific property? Can you access the siblings ViewModel?

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

Upvotes: 5

Views: 10903

Answers (2)

Muhammad Hasan Khan
Muhammad Hasan Khan

Reputation: 35156

Siblings ViewModel would be in its DataContext try

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

Upvotes: 5

Rohit Vats
Rohit Vats

Reputation: 81348

May be this is what you required-

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

Don't forgot to add the markup extension to include the namespace where your class Viewmodel exists - xmlns:local="clr-namespace:ViewModel NameSpace"

Upvotes: 0

Related Questions