且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

WPF - MVVM - 用户控件绑定

更新时间:2022-05-11 17:58:55

表达式

<local:TestUC DataContext="{StaticResource TestUC_VM}"/>

将 TestUC_VM 资源的值分配给 UserControl 的 DataContext.这是与主视图模型的 _uc_VM 成员不同的对象,您稍后将更新该成员.

assigns the value of the TestUC_VM resource to the UserControl's DataContext. This is a different object than the _uc_VM member of the main view model, which you are later updating.

把成员变成公共财产

public TestUC_VM UcVm { get; } = new TestUC_VM();

<local:TestUC DataContext="{Binding UcVm}"/>

像这样更新视图模型:

UcVm.TestString = ...