在DataGrid上绑定View和ViewModel [英] Binding View and ViewModel on a DataGrid

查看:131
本文介绍了在DataGrid上绑定View和ViewModel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个已经存在的ViewModel上的View和ViewModel集。您可以说这是放置第二个ViewModel的基础。

I'm using a View and ViewModel set on an already existing ViewModel. You could say that's the base with the second ViewModel placed on that.

当在 MainViewModel (在此特定示例中为 BrowseViewModel ),关联的ViewModel不会显示。

When placing another ViewModel in the underlying ViewModel of MainViewModel (BrowseViewModel in this particular sample), the associated ViewModel does not show up.

实现如下:

class MainViewModel : PropertyChangedBase
{
    private BrowseViewModel _BrowseViewModel= new BrowseViewModel();

    public BrowseViewModel BrowseViewModel
    {
        get { return _BrowseViewModel; }
        set
        {
            if (_BrowseViewModel== value) return;
            _BrowseViewModel= value;
            NotifyOfPropertyChange("BrowseViewModel");
        }
    }
}

class BrowseViewModel: PropertyChangedBase
{
    private ListingViewModel _ListingViewModel = new ListingViewModel();
    public ListingViewModel ListingViewModel
    {
        get { return ListingViewModel; }
        set
        {
            if (_ListingViewModel == value) return;
            _ListingViewModel = value;
            NotifyOfPropertyChange("ListingViewModel");
        }
    }
}

ListingViewModel...

(我在这里剪掉了不相关的代码)

(I clipped of the non-relevant code here)

标记的实现(MainView):

Implementation in my markup (MainView):

<ContentControl x:Name="BrowseViewModel"/>

并在 BrowseView 中:

<DataGrid.RowDetailsTemplate>
     <DataTemplate>
         <ContentControl x:Name="ListingViewModel"/>
      </DataTemplate>
</DataGrid.RowDetailsTemplate>

我今天在将另一个ViewModel添加到<$ c的现有基础ViewModel时遇到了完全相同的问题$ c> MainViewModel 。如果我没有基础的ViewModel,并且使用的是当前的 MainViewModel ,则一切正常。

I experienced the exact same problem today when adding another ViewModel to an existing underlying ViewModel of the MainViewModel. If I dont have a underlying ViewModel and if I'm using the current MainViewModel, everything works appropriately.

注意:我告诉Caliburn在以前命名的命名空间中查找View及其ViewModel,这没问题。

Note: I told Caliburn to look for Views and their ViewModels at the previously named namespaces, that is no issue.

推荐答案

似乎您正在将某些内容正确绑定到BrowseView中的DataGrid吗?

It appears you're binding something to a DataGrid in BrowseView correct?

声明类似内容

<ContentControl x:Name="ListingViewModel"/>

是声明的简写形式

<ContentControl cal:View.Model="{Binding ListingViewModel" />

因此,我假设(自从在WPF工作以来已经有一段时间了)DataContext在RowDetailsTemplate中,不是BrowseViewModel,而是绑定到DataGrid的任何对象。

Therefore I'm assuming (it's been a while since I've worked in WPF) that the DataContext in the RowDetailsTemplate is not BrowseViewModel but whatever you're binding to the DataGrid.

这篇关于在DataGrid上绑定View和ViewModel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆