0
votes

I have in my ParentView(DashboardConsultants) a gridview which shows a custom tooltip when the user's mousepointer is hovered over a cell. The tooltip show a View (AgreementDetails_View) which shows information of the Agreement binded to that cell. I will show the code I have now so you can better understand my question:

DataGrid Cell in ParentView:

<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:

 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).

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?).

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?

Ofcourse, more information/code/clarification can be happily provided, just ask :)

Thanks in advance!

3

3 Answers

1
votes

Not sure that I got question in righ way but my feelings like you need to use Commands instead of introducing tied coupling by passing back reference to parent itself

1
votes

Personally, I feel that WPF Views should be nothing more than a pretty reflection of the ViewModel. So the View should not actually be passing any data to the ViewModels - instead it should be reflecting the ViewModel's data.

In your case, I would attach a property to the object that is displayed in each DataGrid Row. 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

0
votes

It's pefectly legal and valid to pass in that ID eitehr via a constructor or through a property. 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 (i.e. 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:

object ActiveItem {get;set;}

I then bind that ActiveItem to the ActiveItem in my grid.

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