如何在WPF中的两个视图之间共享相同的ViewModel?

时间:2022-10-02 23:08:23

I have this view:

我有这个观点:

<Window x:Class="Ohmio.Client.PruebasView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:Ohmio.Client"   
    Title="Pruebas" Height="284" Width="626">
<Window.DataContext>
    <local:PruebasViewModel/>
</Window.DataContext>
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="40"/>
        <RowDefinition Height="40"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Label Content="Create View"/>
    <Button Grid.Row="1" Content="Create child View" Click="Button_Click_1"></Button>
    <ContentPresenter x:Name="ContentPrt" Grid.Row="2" Margin="5"></ContentPresenter>        
</Grid>

My idea is to load a second view(child view) in the content presenter. Just for testing i'm doing this from code-behind:

我的想法是在内容展示器中加载第二个视图(子视图)。仅仅是为了测试我是从代码隐藏中做到这一点:

this.ContentPrt.Content = new ModalViewModel();

So my question is: How can I make the new view(the one load on contentPresenter) share the same dataContext with PruebasView? (In this case, PruebasViewModel)

所以我的问题是:如何使新视图(contentPresenter上的一个加载)与PruebasView共享相同的dataContext? (在这种情况下,PruebasViewModel)

Thanks!

谢谢!

2 个解决方案

#1


1  

If I understood your question correctly, it's simple. Just pass PruebasViewModel to ContentPresenter as the data context.

如果我正确理解你的问题,那很简单。只需将PruebasViewModel传递给ContentPresenter作为数据上下文。

<ContentPresenter x:Name="ContentPrt" DataContext={Binding} Grid.Row="2" Margin="5"></ContentPresenter>

#2


0  

I suppose that ModalViewModel is your child view class. So when you add it to the content presenter, your View becomes a part of your Visual Tree, so it automatically shares the DataContext of the main window.

我想ModalViewModel是你的子视图类。因此,当您将其添加到内容展示器时,您的View将成为Visual Tree的一部分,因此它会自动共享主窗口的DataContext。

Pratically WPF makes of the job for you: the content presenter's content will "see" the PruebasViewModel by its own.

实际上,WPF为您完成了工作:内容演示者的内容将自己“看到”PruebasViewModel。

EDIT

编辑

I did not read that you are using Caliburn Micro, but the idea does not change.

我没有读到您使用的是Caliburn Micro,但这个想法没有改变。

#1


1  

If I understood your question correctly, it's simple. Just pass PruebasViewModel to ContentPresenter as the data context.

如果我正确理解你的问题,那很简单。只需将PruebasViewModel传递给ContentPresenter作为数据上下文。

<ContentPresenter x:Name="ContentPrt" DataContext={Binding} Grid.Row="2" Margin="5"></ContentPresenter>

#2


0  

I suppose that ModalViewModel is your child view class. So when you add it to the content presenter, your View becomes a part of your Visual Tree, so it automatically shares the DataContext of the main window.

我想ModalViewModel是你的子视图类。因此,当您将其添加到内容展示器时,您的View将成为Visual Tree的一部分,因此它会自动共享主窗口的DataContext。

Pratically WPF makes of the job for you: the content presenter's content will "see" the PruebasViewModel by its own.

实际上,WPF为您完成了工作:内容演示者的内容将自己“看到”PruebasViewModel。

EDIT

编辑

I did not read that you are using Caliburn Micro, but the idea does not change.

我没有读到您使用的是Caliburn Micro,但这个想法没有改变。