是否可以访问框架当前页面的DataContext?如何?

Is it possible to access the DataContext of the current page of the frame? How?

在wpf中,是否可以访问frame当前页的DataContext?如果是,怎么办?

如果否,我应该使用什么来替代框架以便我可以访问它的 DataContext?

如果有什么不清楚的地方,请告诉我。

更新: 澄清

我在 MainWindow.xaml 中有一个 Frame。我想访问 Frame 中显示的当前页面的 DataContext。假设我想在当前页面的 ViewModel 中显示一个名为 titlestring 属性。 (假设每个页面'ViewModel 都有标题 属性)

更新:这是我的MainWindow.xaml

<Window x:Class="Libertalia.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
        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"
        .
        .
        .
        DataContext="{Binding Main, Source={StaticResource Locator}}"
        >
        <ScrollViewer VerticalScrollBarVisibility="Auto">
            <Grid>
                <Frame Panel.ZIndex="1" x:Name="MainFrame" JournalOwnership="OwnsJournal" NavigationUIVisibility="Hidden" Source="View/BlankPage.xaml" />
            </Grid>
        </ScrollViewer>
</Window>

页面代码(只是其中之一,只是一个示例):

<Page x:Class="Libertalia.View.LoginView"
      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:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
      xmlns:behaviors="clr-namespace:Libertalia.Behavior"
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      .
      .
      .
      DataContext="{Binding Page1, Source={StaticResource Locator}}"
    <DockPanel Margin="200" >

    </DockPanel>
</Page>

更新: 模型、视图和视图模型关系

我想做的事情:

我不确定它是否适合你,因为我仍然不明白你的架构中视图模型和模型之间的关系,但尝试使用这个想法:

1).我的 Window1 的 xaml 有以下内容:

<Grid>       
    <Frame Panel.ZIndex="1"
           x:Name="MainFrame"
           JournalOwnership="OwnsJournal"
           NavigationUIVisibility="Hidden"
           Source="UserControl1.xaml" />
</Grid>

2) UserControl1 具有 DataContext 的定义:

 public UserControl1()
    {
        InitializeComponent();
        DataContext = new MainViewModel();
    }

3).我提取和修改框架内容的 DataContext 的代码:

   Window1 window = new Window1();
        //window.Content = uc;


        var aa = window.Content as Grid;

        foreach (var e in aa.Children)
        {
            if (e is Frame)
            {
                Frame f = e as Frame;
                f.ContentRendered += F_ContentRendered;
            }
        }

//only inside of handler of ContentRendered event you can access to the content of your Frame:
  private void F_ContentRendered(object sender, EventArgs e)
    {
        var frame = sender as Frame;
        UserControl1 uc1 = frame.Content as UserControl1;
        MainViewModel mvm = uc1.DataContext as MainViewModel;

    }

应该可以。