WPF XAML ViewModel 与数据网格和列表视图的绑定

WPF XAML ViewModel Binding with datagrid and listview

这是我要实现的最小工作示例:

XAML:

<Window x:Class="Forms.Wizards.CustomerOrderWizard"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:Forms.Wizards"
    mc:Ignorable="d">
<Grid>
    <DataGrid Name="AssetGrid" 
              AutoGenerateColumns="False"
              HorizontalAlignment="Left"
              Height="209" Margin="0,68,0,0"
              VerticalAlignment="Top" Width="793"
              Loaded="AssetGrid_Loaded" ItemsSource="{Binding Path=Assets}">
        <DataGrid.Columns>
            <DataGridTextColumn Header="#" Binding="{Binding Id}" />
            <DataGridTemplateColumn Header="Estado">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <ListView Margin="10" ItemsSource="{Binding States}">
                            <ListView.ItemTemplate>
                                <DataTemplate>
                                    <WrapPanel>
                                        <TextBlock Tag="{Binding Path=Id}" Text="{Binding Path=Name}" FontWeight="Bold" />
                                    </WrapPanel>
                                </DataTemplate>
                            </ListView.ItemTemplate>
                        </ListView>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>
</Grid>

视图模型:

 public class CustomerOrderViewModel
{
    public ObservableCollection<CustomerOrder> Assets { get; set; }
    public ObservableCollection<ItemState> States { get; set; }
    private ObservableCollection<Customer> Customers { get; set; }

    public CustomerOrderViewModel()
    {
        CustomerOrderService myService = new CustomerOrderService();
        CustomerService myCustomerService = new CustomerService();

        var assets = myService.GetAllOrders();
        var states = myService.GetOrderStates();
        var customers = myCustomerService.GetAllCustomers();

        States = new ObservableCollection<ItemState>();
        Customers = new ObservableCollection<Customer>();
        Assets = new ObservableCollection<CustomerOrder>();

        foreach (var asset in assets)
        {
            Assets.Add(asset);
        }

        foreach (var state in states)
        {
            States.Add(state);
        }

        foreach (var customer in customers)
        {
            Customers.Add(customer);
        }
    }
}

我的资产已正确加载和绑定(每个客户订单一行)但未显示状态和客户列表但它们确实有项目。

我认为我绑定列表视图的方式是错误的,但我找不到原因。

我必须使用 ListView 对象的 RelativeSource 属性来检索原始 DataContext,如下所示:

<ListView Margin="10" ItemsSource="{Binding   
                       Path=DataContext.Customers, 
                       RelativeSource={RelativeSource AncestorType=Window},  
                       Mode=Default}">