是否可以访问框架当前页面的DataContext?怎么样? [英] Is it possible to access the DataContext of the current page of the frame? How?
问题描述
在wpf中,是否可以访问框架当前页面的DataContext?如果是,怎么办?
In wpf, is it possible to access the DataContext of the current page of the frame? If YES, how?
如果不是,我应该用什么代替框架以便访问其DataContext?
If NO, what should I use as replacement for frame so that I can access its DataContext?
如果不清楚,请告诉我。
If something is not clear, please tell me.
更新:澄清
我在 MainWindow.xaml
中有一个 Frame
。我想访问框架
中显示的当前页面的 DataContext
。假设我要显示的 ViewModel
的 string
属性名为 title 当前页面。 (假设每个页面的 ViewModel
具有 title 属性)
I have a Frame
in MainWindow.xaml
. I want to access the DataContext
of the current page displayed in the Frame
. Let's just say I want to display a string
property named title of the ViewModel
of the current page. (Let's assume that each page' ViewModel
has title property)
更新:这是我的MainWindow.xaml
Update: Here is my 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>
页面代码(只是其中一个,只是一个示例):
Code of the page (just one of it, just a sample):
<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关系
UPDATE: Model, View and ViewModel Relationship
-
MainWindow.xaml
(视图)绑定到MainViewModel.cs
(ViewModel)。简而言之,MainWindow.xaml的
DataContext
是MainViewModel.cs
MainWindow.xaml
(View) is binded toMainViewModel.cs
(ViewModel). In short,MainWindow.xaml's
DataContext
isMainViewModel.cs
MainWindow.xaml
(视图)具有 Frame
我想做什么:
- 访问当前页面(从框架中)
DataContext
> MainWindow(MainViewModel)的DataContext 。
- Access current page' (of the Frame)
DataContext
from theDataContext
of the MainWindow (MainViewModel).
推荐答案
I我不确定它是否对您有用,因为我仍然不了解您的体系结构中视图模型与模型之间的关系,但请尝试使用以下想法:
I'm not sure that it will work for you, because I still don't understand the relationship between view models and models in your architecure, but try to use this idea:
1)。我的Window1的xaml具有以下内容:
1). My Window1's xaml has following content :
<Grid>
<Frame Panel.ZIndex="1"
x:Name="MainFrame"
JournalOwnership="OwnsJournal"
NavigationUIVisibility="Hidden"
Source="UserControl1.xaml" />
</Grid>
2)UserControl1具有DataContext的定义:
2) UserControl1 has definition of DataContext:
public UserControl1()
{
InitializeComponent();
DataContext = new MainViewModel();
}
3)。我提取并修改框架内容的DataContext的代码:
3). The code where I extract and modfy the DataContext of the Content of my Frame:
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;
}
应该可以。
这篇关于是否可以访问框架当前页面的DataContext?怎么样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!