开发者

WPF Bind to ViewModel of another element

开发者 https://www.devze.com 2023-02-23 12:26 出处:网络
Just an example. The CustomControl has a ViewModel with a property called \"Test\" How could I bind the textbox to that specific property?

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>
<开发者_JAVA技巧;Controls:CustomControl x:Name="myControl" />


Siblings ViewModel would be in its DataContext try

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


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"

0

精彩评论

暂无评论...
验证码 换一张
取 消