【发布时间】:2016-08-29 09:48:02
【问题描述】:
为什么 this.datacontext (Window) 显示来自 viewModel 的数据而 FrameContent.datacontext (page) 不显示?
目前,我将数据从页面视图加载到窗口。 我不想直接将其加载到窗口的dataContext中,而是将其加载到显示数据的框架的dataContext中。
在我的代码下面:
ViewConfiguration.xaml:
<Frame x:Name="FrameContent" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" NavigationUIVisibility="Hidden" BorderThickness="0"/>
ViewConfiguration.xaml.cs:
namespace Modules.Configuration
{
public partial class ViewConfiguration : Window
{
public ViewConfiguration()
{
InitializeComponent();
ViewModelConfiguration ViewModelConfiguration = new ViewModelConfiguration();
}
private void PageEditor1_Click(object sender, RoutedEventArgs e)
{
this.DataContext = new ViewModelEditor1();
FrameContent.Source = new Uri("/Modules/Editor1/View/ViewEditor1.xaml", UriKind.Relative);
}
private void PageEditor2_Click(object sender, RoutedEventArgs e)
{
this.DataContext = new ViewModelEditor2();
FrameContent.Source = new Uri("/Modules/Editor2/View/ViewEditor2.xaml", UriKind.Relative);
}
}
}
我怀疑这样的事情会起作用,但没有。
private void PageEditor1_Click(object sender, RoutedEventArgs e)
{
// this.DataContext = new ViewModelEditor1(); // loading in datacontext of window
this.FrameContent.DataContext = new ViewModelEditor1(); // loading in datacontext of frame
FrameContent.Source = new Uri("/Modules/Editor1/View/ViewEditor1.xaml", UriKind.Relative);
}
【问题讨论】:
标签: c# wpf mvvm window datacontext