简体   繁体   English

从不同的 ViewModel 调用 WPF mvvm 视图不会更新

[英]WPF mvvm View won't update when called from different ViewModel

I've been trying to update a property inside a usercontrol with it's own VM.我一直在尝试用它自己的虚拟机更新用户控件内的属性。

<Label Content="{Binding Path =LabelText,UpdateSourceTrigger=PropertyChanged }" Grid.Column="0"/>

The Label itself works when I set it's text in the VM's constructor, and also updates if I use a command within the VM.当我在 VM 的构造函数中设置它的文本时,Label 本身可以工作,并且如果我在 VM 中使用命令也会更新。

But changing it from outside won't do anything:但是从外部改变它不会做任何事情:

        private LabelTextBoxVM _testThing;
        public LabelTextBoxVM TestThing
        {
            get => _testThing;
            set
            {
                _testThing = value;
                NotifyPropertyChanged("TestThing");
            }
        }

           private void Update()
        {
            TestThing.LabelText = "Eureka";
            TestThing.TextBoxText = "Wooosh";
            NotifyPropertyChanged("TestThing");
        }

From all I've read, calling NotifyPropertyChanged for TestThing should update the content of both Label and Textbox, but nothing happens.从我读过的所有内容来看,为 TestThing 调用 NotifyPropertyChanged 应该会更新 Label 和 Textbox 的内容,但没有任何反应。

@Clemens @克莱门斯

I found the answer, provided by you actually, in another question:我在另一个问题中找到了您实际上提供的答案:

wpf-mvvm-usercontrol-binding wpf-mvvm-usercontrol-绑定

I have to set my DataContext in the parent view, binding to a property in the parent, and delete the Datacontext inside the childVM我必须在父视图中设置我的 DataContext,绑定到父视图中的属性,并删除 childVM 中的 Datacontext

So:所以:

<myViewes:UserControlName DataContext="{Binding VMProperty}/>"

And this part cannot be in my UserControls:这部分不能在我的用户控件中:

<UserControl.DataContext>
        <viewModels:UserControlVM/>
</UserControl.DataContext>

I am guessing this was what you meant by "inheriting" the VM我猜这就是“继承”虚拟机的意思

Thank you!谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM