ListView 中的 WPF ListView

标签 wpf data-binding

我确信我错过了一些简单/明显的东西,但我似乎无法在 ListView 中绑定(bind) ListView 的数据

<Window x:Class="TestList.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <DataTemplate x:Key="InsideListTemplate">
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="test" Width="50"></TextBlock>
            <TextBlock Text="{Binding OrderId}" Width="50"></TextBlock>
        </StackPanel>
    </DataTemplate>
    <DataTemplate x:Key="OrdersTemplate">
        <ListView HorizontalAlignment="Stretch"
                  HorizontalContentAlignment="Stretch"
                  MinWidth="100"
                  MinHeight="25"
            ItemsSource="{Binding Orders}" 
            ItemTemplate="{StaticResource InsideListTemplate}" 
        >
        </ListView>
    </DataTemplate>
    <DataTemplate x:Key="CustomersTemplate">
        <StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch">
            <TextBlock Text="{Binding CustomerId}" Width="50" Foreground="Navy" VerticalAlignment="Center" />
            <ListBox ItemsSource="{Binding Orders}" ItemTemplate="{StaticResource OrdersTemplate}" HorizontalContentAlignment="Stretch"></ListBox>
        </StackPanel>
    </DataTemplate>

</Window.Resources>
<DockPanel LastChildFill="True">
    <ListView Name="listView" ItemTemplate="{StaticResource CustomersTemplate}" >
    </ListView>
</DockPanel>

using System.Collections.Generic;
namespace TestList
{
public partial class MainWindow
{
    public class Customer
    {
        public int CustomerId { get; set; }
        public List<Order> Orders { get; set; }
    }

    public class Order
    {
        public int OrderId { get; set; }
    }
    public MainWindow()
    {
        InitializeComponent();
        DataContext = this;
        var customers = new List<Customer>
                            {
                                new Customer
                                    {
                                        CustomerId = 1,
                                        Orders = new List<Order>
                                                     {
                                                         new Order {OrderId = 1},
                                                         new Order {OrderId = 2}
                                                     }
                                    },
                                new Customer
                                    {
                                        CustomerId = 2,
                                        Orders = new List<Order>
                                                     {
                                                         new Order {OrderId = 1},
                                                         new Order {OrderId = 2}
                                                     }
                                    }
                            };
        listView.ItemsSource = customers;
    }
  }
}

enter image description here

最佳答案

这是哈迪斯答案的解释:

您正在将 ListBox 绑定(bind)到客户模板中的 Orders 集合。然后在订单模板中再次定义与订单的 ListView 绑定(bind)。这意味着此时的绑定(bind)路径是 customer.orders.orders ,但它不存在。

如果您只是删除 OrdersTemplate 并将 ListView 放置在客户模板中 ListBox 的位置,那么它就可以工作。

关于ListView 中的 WPF ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5999317/

相关文章:

Windows 应用程序中的 WPF 组件

c# - 在后面的 UserControl 代码中获取依赖属性值

c# - 如何验证 DataGridView 输入?

c# - 如何使用 Telerik Rad 网格绑定(bind)将数据传递给 Ajax 服务?

c# - 如何报告实体对象的自定义(添加)计算属性已更改?

c# - 如何仅使用获取访问器绑定(bind)到属性

C#- 在 'enclosing' 本地范围内使用变量?

wpf - MVVM:依赖注入(inject)和按需创建 ViewModel

javascript - AngularJS 表达式不会在输入的值字段中进行计算

data-binding - WinRT ImageSource 与 cookie 绑定(bind)