简体   繁体   English

将信息从父视图传递到子视图模型

[英]Pass info from Parent View to Child ViewModel

I have in my ParentView(DashboardConsultants) a gridview which shows a custom tooltip when the user's mousepointer is hovered over a cell. 我在ParentView(DashboardConsultants)中有一个gridview,当用户的mousepointer悬停在一个单元格上时,它会显示一个自定义工具提示。 The tooltip show a View (AgreementDetails_View) which shows information of the Agreement binded to that cell. 工具提示显示一个View(AgreementDetails_View),它显示绑定到该单元格的协议的信息。 I will show the code I have now so you can better understand my question: 我将展示我现在的代码,以便您更好地理解我的问题:

DataGrid Cell in ParentView: ParentView中的DataGrid单元格:

<DataGridTextColumn Header="Okt" Width="*" x:Name="test">
    <DataGridTextColumn.ElementStyle>
       <Style TargetType="{x:Type TextBlock}">                                   
            <Setter Property="Tag" Value="{Binding Months[9].AgreementID}"/>
            <Setter Property="Background" Value="White" />
            <Setter Property="DataGridCell.ToolTip" >
                <Setter.Value>
                     <v:UC1001_AgreementDetails_View Background="#FFF" Opacity="0.88"  />
                </Setter.Value>
            </Setter>

My ChildView: 我的孩子观点:

 public UC1001_DashBoardConsultants_View(UC1001_DashboardConsultantViewModel viewModel)
        {
            InitializeComponent();
            this.DataContext = viewModel;
        }  

In the ViewModel, I have following method to get the right Agreement from the database: 在ViewModel中,我有以下方法从数据库中获取正确的协议:

 private void GetRefData()
        {
            UC1001_ActiveAgreementArguments args = new UC1001_ActiveAgreementArguments();
            args.AgreementID = 3;
            DefaultCacheProvider defaultCacheProvider = new DefaultCacheProvider();
            if (!defaultCacheProvider.IsSet("AgrDet:" + args.AgreementID))
            {
                ConsultantServiceClient client = new ConsultantServiceClient();

                AgreementDetailsContract = client.GetAgreementDetailsByAgreementID(args);
                defaultCacheProvider.Set("AgrDet:" + args.AgreementID, AgreementDetailsContract, 5);
            }
            else
            {
                AgreementDetailsContract = (UC1001_ActiveAgreementContract)defaultCacheProvider.Get("AgrDet:" + args.AgreementID);
            }
        }

As you can see for now, the method always calls the same Agreement, (I did that for testing purposes) but now I want the Agreement which ID is specified in the DataGrid Cell Tag (in this example it's the Months[9].AgreementID ). 正如您现在所看到的,该方法始终调用相同的协议(我为了测试目的而这样做),但现在我想要在DataGrid单元格标记中指定ID的协议(在此示例中,它是Months[9].AgreementID )。

I can give it to the ViewModel in my Child View's constructor, but I don't think that it's allowed due to the MVVM Pattern (or is it allowed?). 我可以在我的Child View的构造函数中将它提供给ViewModel,但是我不认为它是由于MVVM模式允许的(或者是允许的吗?)。

So my question is: How can I pass the AgreementID specified in my ParentView to the ChildView's ViewModel to get the right data for the ChildView? 所以我的问题是:如何将ParentView中指定的AgreementID传递给ChildView的ViewModel以获取ChildView的正确数据?

Ofcourse, more information/code/clarification can be happily provided, just ask :) 当然,可以愉快地提供更多的信息/代码/澄清,只要问:)

Thanks in advance! 提前致谢!

不确定我是否以粗略的方式提出问题,但我的感觉就像你需要使用命令而不是通过将引用传递给父本身来引入绑定耦合

Personally, I feel that WPF Views should be nothing more than a pretty reflection of the ViewModel . 就个人而言,我觉得WPF Views应该只是ViewModel一个很好的反映。 So the View should not actually be passing any data to the ViewModels - instead it should be reflecting the ViewModel's data. 所以View实际上不应该将任何数据传递给ViewModels - 而应该反映ViewModel的数据。

In your case, I would attach a property to the object that is displayed in each DataGrid Row. 在您的情况下,我会将属性附加到每个DataGrid行中显示的对象。 For example, if your DataGrid contained Agreement objects, I would ensure that each Agreement object had a property called AgreementDetails which can be viewed from the ToolTip 例如,如果您的DataGrid包含Agreement对象,我将确保每个Agreement对象都有一个名为AgreementDetails的属性,可以从ToolTip查看

It's pefectly legal and valid to pass in that ID eitehr via a constructor or through a property. 通过构造函数或通过属性传递ID eitehr是完全合法且有效的。 I'm not sure from your code, but if your parent is the one with access to your model, you can also pass teh model into your view model (ie via constructor, property, or method). 我不确定你的代码,但如果你的父母是可以访问你的模型的那个,你也可以将模型传递给你的视图模型(即通过构造函数,属性或方法)。

One thing I often do in this case is to add a property such as the following in the ViewModel of my Parent: 在这种情况下我经常做的一件事是在我父的ViewModel中添加如下属性:

object ActiveItem {get;set;}

I then bind that ActiveItem to the ActiveItem in my grid. 然后我将ActiveItem绑定到我的网格中的ActiveItem。

<DataGrid SelectedItem="{Binding ActiveItem}">
</DataGrid>

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

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