视图(用户控件)的绑定属性在嵌套在 MainWindow 中后停止工作

View(User Control)'s binded properties stop working after being nested in a MainWindow

我目前是 WPF 的新手,我创建了一个示例项目以在 MVVM 设置之后使用 MVVMCross 框架。到目前为止,我使用 MainWindow.xaml 作为我的子视图 (StudentDetailsView.xaml) 的父视图。起初我只有我的子视图,所有绑定都运行良好,但在我将子视图作为嵌套视图添加到我的父视图后,所有绑定都停止工作。例如,FirstName 和 LastName 属性 停止使用他们的双向模式绑定来更新 FullName 属性.

MainWindow.xaml

<views:MvxWindow 
        xmlns:views="clr-namespace:MvvmCross.Platforms.Wpf.Views;assembly=MvvmCross.Platforms.Wpf"
        x:Class="MvxStarter.Wpf.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:MvxStarter.Wpf.Views"
        xmlns:vm="clr-namespace:MvxStarter.Core.ViewModels;assembly=MvxStarter.Core"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    
    <Grid x:Name="OuterGrid">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="25"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>

        <Grid.RowDefinitions>
            <RowDefinition Height="5"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="20"/>
        </Grid.RowDefinitions>

        <Grid Grid.Row="1" Grid.Column="1">
            <StackPanel>
                <Menu>
                    <MenuItem Header="_File">
                        <MenuItem Header="FileOption1"/>
                    </MenuItem>
                    <MenuItem Header="_Options">
                        <MenuItem Header="Option1"/>
                    </MenuItem>
                </Menu>
                <Grid>
                    <!--Dynamic view switch out here-->
                    <local:StudentDetailsView/>
                </Grid>
            </StackPanel>
        </Grid>

    </Grid>
</views:MvxWindow>

MainWindow.xaml.cs

using MvvmCross.Platforms.Wpf.Views;
using System.Windows.Controls;

namespace MvxStarter.Wpf
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : MvxWindow
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }
}

StudentDetailsView.xaml

<views:MvxWpfView 
             xmlns:views="clr-namespace:MvvmCross.Platforms.Wpf.Views;assembly=MvvmCross.Platforms.Wpf"
             xmlns:mvx="clr-namespace:MvvmCross.Platforms.Wpf.Binding;assembly=MvvmCross.Platforms.Wpf"
             x:Class="MvxStarter.Wpf.Views.StudentDetailsView"
             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:MvxStarter.Wpf.Views"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800" FontSize="20">

    <Grid>
        <StackPanel Margin="5">
            <TextBlock Text="{Binding FullName}" FontSize="28" Margin="0,0,0,15"/>
            <TextBlock Text="First Name"/>
            <TextBox Text="{Binding FirstName, Mode=TwoWay}" Margin="0,0,0,15"/>
            <TextBlock Text="Last Name"/>
            <TextBox Text="{Binding LastName, Mode=TwoWay}" Margin="0,0,0,15"/>
        </StackPanel>
    </Grid>
    
</views:MvxWpfView>

StudentDetailsViewModel.cs

using MvvmCross.Logging;
using MvvmCross.Navigation;
using MvvmCross.ViewModels;


namespace MvxStarter.Core.ViewModels
{
    public class StudentDetailsViewModel : MvxViewModel
    {

        public StudentDetailsViewModel()
        {

        }

        private string _firstName;

        public string FirstName
        {
            get { return _firstName; }
            set
            {
                SetProperty(ref _firstName, value);
                RaisePropertyChanged(() => FullName);
            }
        }

        private string _lastName;

        public string LastName
        {
            get { return _lastName; }
            set
            {
                SetProperty(ref _lastName, value);
                RaisePropertyChanged(() => FullName);
            }
        }

        public string FullName => $"{FirstName} {LastName}";

    }
}

我在父视图中设置子视图后是否没有正确绑定我的属性?

最终我想做的是,每当用户在菜单栏中选择要查看的不同视图时,在网格面板中切换出我的子视图。以红色绘制的区域是我想要的地方,因此根据用户的选择切换不同的视图。

好的,在网上稍作搜索并回顾我的步骤后。我发现我的子视图的数据上下文没有正确找到视图模型,所以我不得不在 .xaml 中显式调用它,就像这样

<views:MvxWpfView.DataContext>
    <vm:StudentDetailsViewModel/>
</views:MvxWpfView.DataContext>

不确定这是否是最干净的解决方案...