繁体   English   中英

是否可以访问框架当前页面的DataContext? 怎么样?

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

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

如果为“否”,那么应该使用什么替代框架以访问其DataContext?

如果不清楚,请告诉我。

更新:澄清

我在MainWindow.xaml有一个Frame 我想访问Frame显示的当前页面的DataContext 假设我要显示一个string属性,该string属性的名称为当前页面的ViewModel (假设每个页面的ViewModel都具有title属性)

更新:这是我的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>

更新:模型,视图和ViewModel关系

  • MainWindow.xaml (视图)已绑定到MainViewModel.cs (视图模型)。 简而言之, MainWindow.xaml's DataContextMainViewModel.cs

  • MainWindow.xaml (视图)具有Frame

  • Frame具有Page (视图)。 Frame有很多页面,一次显示一个页面。
  • Page具有自己的ViewModel( DataContext

我想做的事:

  • 访问当前页面”(Frame的) DataContextDataContext的主窗口中(MainViewModel)。

我不确定它是否对您有用,因为我仍然不了解您的体系结构中视图模型与模型之间的关系,但是请尝试使用以下想法:

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)。 我提取并修改Frame内容的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;

    }

它应该工作。

暂无
暂无

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

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