C# 为什么绑定到标签的一个字符串的propertyChange在我从其他视图模型设置其值时不会上升

C# 为什么绑定到标签的一个字符串的propertyChange在我从其他视图模型设置其值时不会上升,c#,wpf,mvvm,C#,Wpf,Mvvm,我有一个WPF应用程序,在视图1中有两个视图和两个模型视图。有一个按钮和一个标签: </WrapPanel> <WrapPanel Orientation="Horizontal"> <Button Command="{Binding InitServerCommand}" Content="Connect to Server" Margin="10 10 0 0"/> </WrapPanel> <WrapPanel Orientat

我有一个WPF应用程序,在视图1中有两个视图和两个模型视图。有一个按钮和一个标签:

</WrapPanel>
<WrapPanel Orientation="Horizontal">
    <Button Command="{Binding InitServerCommand}" Content="Connect to Server" Margin="10 10 0 0"/>
</WrapPanel>
<WrapPanel Orientation="Horizontal">
    <Label Content="Info: " Style="{StaticResource myLabelStyle}"/>
<Label x:Name="lblInfoView" Content="{Binding Path=LblInfo, Mode=TwoWay}" Style="{StaticResource myLabelStyle}"/>
</WrapPanel>
此外,我还有一个BaseView模型,它扩展了InotifyPropertyCharged:

     public abstract class BaseViewModel : INotifyPropertyChanged
        {

            public event PropertyChangedEventHandler PropertyChanged;

            protected BaseViewModel()
            {

            }

            protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
            {
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
我有一个函数,它执行一些操作并刷新LblInfo的值,它会自动映射到视图1中的标签上

    public void send(string str)
            {
                try
                {
                    //Do somenthing
                    LblInfo = "Connect";
                }
                catch (Exception ex)
                {
                    LblInfo = "Error..... " + ex.StackTrace;
                }
            }
问题是:

如果在视图模型1中调用send(),程序将在调用时自动执行setter

OnPropertyChanged("LblInfo"); 
propertyChanged与null不同,它调用

Invoke(this, new PropertyChangedEventArgs(propertyName));
视图1成功地更新了标签内容

但是如果从另一个模型视图2调用该方法

public class ViewModel2: BaseViewModel
  {
          public ViewModel1 ConfigureParametersVM
    {
        get { return GetValue<ViewModel1>(); }
        set { SetValue(value); }
    }
        //Constructor
          public ViewModel2()
    {
        ConfigureParametersVM = new ViewModel1();
        //Execute the send method from View Model 1
        ConfigureParametersVM.send("update_mroi");
    }

}
公共类ViewModel2:BaseViewModel
{
公共视图模型1配置参数SVM
{
获取{return GetValue();}
set{SetValue(value);}
}
//建造师
公共视图模型2()
{
ConfigureParametersVM=新的ViewModel1();
//从视图模型1执行send方法
configureParametersSVM.send(“update_mroi”);
}
}
它运行send方法,setter更新_lblInfo的值,但是当它调用OnPropertyChanged()时,propertyChanged等于null,并且不更新View1中的标签

我做错了什么

补充资料


现在,我没有在ViewModel1或ViewModel2上提到DataContext,你能告诉我怎么做吗?View1(xaml)和(.xaml.cs)中的代码如下:.xaml

<UserControl x:Class="Namespace1.Views.View1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:Namespace1.Views"
             xmlns:GuiControlLibrary="clr-namespace:GuiControlLibrary;assembly=GuiControlLibrary"
             mc:Ignorable="d" 
             d:DesignHeight="768" d:DesignWidth="1024">
    <Grid>
             //Somethin else        
            <Grid>
                </StackPanel>
                <StackPanel Orientation="Vertical" Grid.Column="1">
                    <!--Code for TCP/IP Parameters-->
                     //More code not showed here
                    <WrapPanel Orientation="Horizontal">
            //This is the commad from Icommand that starts de method send()
                        <Button Command="{Binding InitServerCommand}" Content="Connect to Server" Margin="10 10 0 0"/>
                    </WrapPanel>
                    <WrapPanel Orientation="Horizontal">
            //Static label
                        <Label Content="Info: " Style="{StaticResource myLabelStyle}"/>
            //Binded label
                        <Label x:Name="lblInfoView" Content="{Binding Path=LblInfo, Mode=TwoWay}" Style="{StaticResource myLabelStyle}"/>
                    </WrapPanel>
                </StackPanel>
            </Grid>
    </Grid>
</UserControl>

//别的什么
//这里没有显示更多代码
//这是Icommand中启动de method send()的命令
//静态标签
//装订标签
而.xaml.cs是:

using System.Windows.Controls;
namespace Namespace1.Views
{
    /// <summary>
    /// Interaction logic for View1.xaml
    /// </summary>
    public partial class View1 : UserControl
    {
        public View1()
        {
            InitializeComponent();
        }
    }
}
使用System.Windows.Controls;
名称空间1.Views
{
/// 
///View1.xaml的交互逻辑
/// 
公共部分类View1:UserControl
{
公众意见1()
{
初始化组件();
}
}
}

根据您问题中的代码:

public ViewModel2()
{
    ConfigureParametersVM = new ViewModel1();
    //Execute the send method from View Model 1
    ConfigureParametersVM.send("update_mroi");
}
ConfigureParametersVM
(ViewModel1)未指定为视图的
DataContext
。分配后,视图将在ViewModel上注册
PropertyChanged
事件

public static string _lblInfo;
这是完全错误的。应该是:

private string _lblInfo;
这不会解决你的问题,但会给你一个很好的提示,为什么它不起作用。因为您从未调用视图模型实例的send方法。您创建了一个新的。这就像有第二个
int
并且想知道为什么在设置第二个
int
时第一个
int
没有改变。因为它们是不同的实例


您的ViewModels需要一种相互了解的方式,如果没有更多的代码,我无法告诉您哪种方式可能是最好的。但您需要连接现有的两个实例,而不是创建新实例。

请添加代码,说明如何将View1的DataContext绑定到ViewModel1。请查看您的View1 xaml文件。听起来好像您将ViewModel1作为一个静态资源。现在,我在ViewModel1或ViewModel2上没有提到DataContext,您能告诉我怎么做吗?我已经更新了问题,包括代码。非常感谢你的帮助。
private string _lblInfo;