Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有 3 个用户控件:父母和两个孩子。子级从父级继承 DataContext,在它们内部我有一个标签(与父视图模型的属性标签字符串绑定)和一个按钮。我希望当我单击按钮时,父视图模型属性更改为字符串“Clicked”,我该怎么做?谢谢
如果您想从后面的代码中更改字符串,那么您将这样做。
为按钮的单击事件定义事件处理程序。在里面你将拥有
ParentViewModel vm = this.DataContext as ParentViewModel; if(vm!=null) { vm.SomeTextProperty = "Clicked"; }
但我建议您创建一个 ICommand 并将其绑定到按钮的 Command 属性,然后您可以使用 ViewModel 本身的属性,而无需触及后面的代码