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

发布于 2024-12-11 21:46:50 字数 2268 浏览 0 评论 0原文

我的 ParentView(DashboardConsultants) 中有一个 gridview,当用户的鼠标指针悬停在单元格上时,它会显示自定义工具提示。工具提示显示一个视图 (AgreementDetails_View),其中显示绑定到该单元格的协议的信息。我将展示我现在拥有的代码,以便您可以更好地理解我的问题:

ParentView 中的 DataGrid Cell:

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

我的 ChildView:

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

在 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);
            }
        }

正如您现在所看到的,该方法始终调用相同的协议(我这样做是为了测试目的),但现在我想要在 DataGrid 单元格标记中指定 ID 的协议(在本例中为 Months[9].AgreementID)。

我可以将它交给子视图构造函数中的 ViewModel,但由于 MVVM 模式,我认为这是不允许的(或者是否允许?)。

所以我的问题是:如何将ParentView中指定的AgreementID传递给ChildView的ViewModel以获得ChildView的正确数据?

当然,我们可以愉快地提供更多信息/代码/说明,只需询问即可:)

提前致谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

神仙妹妹 2024-12-18 21:46:50

不确定我是否以正确的方式提出问题,但我的感觉是您需要使用命令,而不是通过将引用传递回父级本身来引入绑定耦合

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

夏雨凉 2024-12-18 21:46:50

就我个人而言,我认为 WPF 视图应该只不过是 ViewModel 的完美反映。因此,View 实际上不应该将任何数据传递给 ViewModels - 相反,它应该反映 ViewModel 的数据。

在您的情况下,我会将一个属性附加到每个 DataGrid 行中显示的对象。例如,如果您的 DataGrid 包含 Agreement 对象,我将确保每个 Agreement 对象都有一个名为 AgreementDetails 的属性,可以从 AgreementDetails 中查看该属性。代码>工具提示

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

千笙结 2024-12-18 21:46:50

通过构造函数或属性传递该 ID 是完全合法且有效的。我不确定您的代码,但如果您的父级可以访问您的模型,您也可以将模型传递到您的视图模型中(即通过构造函数、属性或方法)。

在这种情况下,我经常做的一件事是在父级的 ViewModel 中添加如下属性:

object ActiveItem {get;set;}

然后将该 ActiveItem 绑定到网格中的 ActiveItem。

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

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文