如何绑定另一个实体的实体

时间:2014-07-11 10:52:47

标签: c# wpf mvvm

我有两个课程,即客户和订单。实体,VM和xaml代码如下。我想要实现的是当我在左边的数据网格中选择一个客户时,我希望在右边的数据网格上看到他/她的订单。我可以在左侧数据网格上看到客户,但是我无法看到他们的订单。我正在使用Entity框架和MVVMLight。如果有人能说出我失踪的东西,我会很感激。谢谢。 这是基类:

  public class BaseEntity : INotifyPropertyChanged
    {

        public virtual int Id { get; set; }

        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string propName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propName));
            }
        }
}

这是Customer类:     公共类客户:BaseEntity     {         private string _name;

    public string Name
    {
        get { return _name; }
        set
        {
            _name = value;
            OnPropertyChanged("Name");
        }
    }

    private ICollection<Order> _orders;

    public ICollection<Order> Orders
    {
        get { return _orders; }
        set
        {
            _orders = value;
            OnPropertyChanged("Orders");
        }
    }

}

这是订单类:  公共类订单:BaseEntity     {         private string _explanation;

    public string Explanation
    {
        get { return _explanation; }
        set
        {
            _explanation = value;
            OnPropertyChanged("Explanation");
        }
    }


    private Customer _customer;

    public Customer Customer
    {
        get { return _customer; }
        set
        {
            _customer = value;
            OnPropertyChanged("Customer");
        }
    }


}

在ViewModel中,我只有客户虚拟机:

 public class CustomerVM : ViewModelBase
    {

        protected CustomerOrderContext _context;

        protected ObservableCollection<Customer> _entities;

        protected Customer _selectedentity;

        public  ObservableCollection<Customer> Entities
        {
            get { return _entities; }
            set
            {
                _entities = value;
                RaisePropertyChanged(() => this.Entities);
                DisplayEntityInfo();
            }
        }

        public  Customer SelectedEntity
        {
            get { return _selectedentity; }
            set
            {
                _selectedentity = value;
                RaisePropertyChanged(() => this.SelectedEntity);
                DisplayEntityInfo();


            }
        }

        public CustomerVM()
        {
            _context = new CustomerOrderContext();

            Entities = GetEntities();

            SelectedEntity = Entities.FirstOrDefault();
        }

        public ObservableCollection<Customer> GetEntities()
        {
            ObservableCollection<Customer> entities;

            entities = new ObservableCollection<Customer>(_context.Set<Customer>());

            return entities;
        }


    }

这是我的.xaml文件:

<Window x:Class="CustomerOrder.App.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:cm="clr-namespace:System.ComponentModel;assembly=System"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:vm="clr-namespace:CustomerOrder.App.ViewModel"
        xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
        mc:Ignorable="d"
        DataContext="{Binding Source={StaticResource Locator}, Path=CustomerView}"
        Title="MainWindow" Height="500" Width="900">
    <Grid>
        <Canvas>
            <DataGrid x:Name="maingrid" ItemsSource="{Binding Entities}" SelectedItem="{Binding SelectedEntity}" AutoGenerateColumns="False" Canvas.Left="10" Canvas.Top="265">
                <DataGrid.Columns>
                    <DataGridTextColumn Binding="{Binding Name}" Header="Name" Width="200"></DataGridTextColumn>
                </DataGrid.Columns>
            </DataGrid>
            <DataGrid x:Name="ordergrid" ItemsSource="{Binding ElementName=maingrid, Path=SelectedItem.Orders}" AutoGenerateColumns="True" CanUserDeleteRows="True" Canvas.Top="265" Canvas.Left="597">
                <DataGrid.Columns>
                    <DataGridTextColumn Binding="{Binding Explanation}" Header="Orders" Width="200"></DataGridTextColumn>
                </DataGrid.Columns>
            </DataGrid>
        </Canvas>

    </Grid>
</Window>

1 个答案:

答案 0 :(得分:2)

第一个DataGrid的SelectedItem绑定到'SelectedEntity'。您应该将第二个DataGrid的ItemSource属性绑定到ViewModel上相同的“SelectedEntity”属性

<DataGrid x:Name="ordergrid" ItemsSource="{Binding SelectedEntity.Orders}" AutoGenerateColumns="True" CanUserDeleteRows="True" Canvas.Top="265" Canvas.Left="597">