C# WPF&;SimpleMVVM:将新viewmodel绑定到视图

C# WPF&;SimpleMVVM:将新viewmodel绑定到视图,c#,wpf,data-binding,viewmodellocator,simple-mvvm,C#,Wpf,Data Binding,Viewmodellocator,Simple Mvvm,请注意,此问题专门针对SimpleMVVM及其ViewModelLocator的使用。 我有这样的视图设置: <UserControl x:Class="CallTracker.WPF.Views.CallUserControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/w

请注意,此问题专门针对SimpleMVVM及其ViewModelLocator的使用。

我有这样的视图设置:

<UserControl x:Class="CallTracker.WPF.Views.CallUserControl"
             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" 

             DataContext="{Binding Source={StaticResource Locator}, Path=CallViewModel}">

    <StackPanel>
        <TextBlock Text="{Binding GreetingText}" />
    </StackPanel>
</UserControl>
在执行过程中,我试图创建一个新的CallUserControl来使用,但是使用Snoops,我可以看到CallUserControl的datacontext没有绑定到viewmodel(Snoops中的datacontext表示null)

通过代码,我可以看到CallViewModel正在创建并分配给定位器中的对象,但是UserControl的datacontext没有绑定到这个新创建的对象

我正在创建新的CallViewModel,如下所示:

private void NewCall()
{
    MessageBox.Show("NewCallCommand executed.");

    // Only start a new call if one doesn't currently exist
    if((App.Current.Resources["Locator"] as ViewModelLocator).CallViewModel == null)
    {
        CurrentCallViewModel = new CallViewModel();
    }
}

....

    /// <summary>
    /// Current MainContentViewModel 
    /// </summary>
    public CallViewModel CurrentCallViewModel
    {
        get
        {
            //Visibility _visible = (new ViewModelLocator()).CallViewModel == null ? Visibility.Collapsed : Visibility.Visible;
            return (App.Current.Resources["Locator"] as ViewModelLocator).CallViewModel;
        }
        set
        {
            if (this._CurrentCallViewModel != value)
            {
                this._CurrentCallViewModel = value;
                (App.Current.Resources["Locator"] as ViewModelLocator).CallViewModel = value;
                NotifyPropertyChanged(m => m.CurrentCallViewModel);
            }
        }
    }
    private CallViewModel _CurrentCallViewModel = null;
private void NewCall()
{
Show(“NewCallCommand已执行”);
//仅当当前不存在新呼叫时才开始新呼叫
if((App.Current.Resources[“Locator”]作为ViewModelLocator.CallViewModel==null)
{
CurrentCallViewModel=新的CallViewModel();
}
}
....
/// 
///当前MainContentViewModel
/// 
公共调用视图模型CurrentCallViewModel
{
收到
{
//可见性_visible=(新的ViewModelLocator()).CallViewModel==null?可见性。折叠:可见性。可见;
返回(App.Current.Resources[“Locator”]作为ViewModelLocator);
}
设置
{
如果(此._CurrentCallViewModel!=值)
{
这。_CurrentCallViewModel=值;
(App.Current.Resources[“Locator”]作为ViewModelLocator);
NotifyPropertyChanged(m=>m.CurrentCallViewModel);
}
}
}
私有CallViewModel _CurrentCallViewModel=null;

您知道为什么这个新的ViewModel不会绑定到新模型吗?是否存在类似于NotifyPropertyChanged的机制,我必须从定位器调用该机制才能绑定对象?

我在这里暗中拍摄了一张完整的照片……您已经设置了
CurrentCallViewModel=new CallViewModel()
,但您是否也需要将
(App.Current.Resources[“Locator”]设置为ViewModelLocator)。CallViewModel=CurrentCallViewModel
?我想您需要调用CallViewModel属性的setter中更改的属性。我应该知道我正在调用(App.Current.Resources[“Locator”]作为ViewModelLocator).CallViewModel=值:在CurrentCallViewModel的属性设置器中。我已经用属性设置器更新了OP。
private void NewCall()
{
    MessageBox.Show("NewCallCommand executed.");

    // Only start a new call if one doesn't currently exist
    if((App.Current.Resources["Locator"] as ViewModelLocator).CallViewModel == null)
    {
        CurrentCallViewModel = new CallViewModel();
    }
}

....

    /// <summary>
    /// Current MainContentViewModel 
    /// </summary>
    public CallViewModel CurrentCallViewModel
    {
        get
        {
            //Visibility _visible = (new ViewModelLocator()).CallViewModel == null ? Visibility.Collapsed : Visibility.Visible;
            return (App.Current.Resources["Locator"] as ViewModelLocator).CallViewModel;
        }
        set
        {
            if (this._CurrentCallViewModel != value)
            {
                this._CurrentCallViewModel = value;
                (App.Current.Resources["Locator"] as ViewModelLocator).CallViewModel = value;
                NotifyPropertyChanged(m => m.CurrentCallViewModel);
            }
        }
    }
    private CallViewModel _CurrentCallViewModel = null;