ListView 中的 WPF ListView

发布于 2024-11-07 20:03:16 字数 3395 浏览 0 评论 0原文

我确信我错过了一些简单/明显的东西,但我似乎无法在 ListView 中绑定 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;
    }
  }
}

在此处输入图像描述

I am sure I am missing something simple/obvious, but I cannot seem to bind the data of a ListView within a 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

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

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

发布评论

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

评论(2

〗斷ホ乔殘χμё〖 2024-11-14 20:03:16

这是 Hadis 答案的解释:

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

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

This is an explanation of Hadis answer:

You are binding a ListBox to the Orders collection within the customer template. And then in the orders template you define a ListView binding again to the orders. This means that the binding path at that point is customer.orders.orders which does not exists.

If you just remove the OrdersTemplate and place the ListView where the ListBox is in the customer template then it works.

回眸一遍 2024-11-14 20:03:16

如何更改它列表:

public partial class MainWindow : Window
{
    public class Customer
    {
        public int CustomerId { get; set; }
        public List<Order> Orders { get; set; }
    }

    public class Order
    {
        public int OrderId { get; set; }
        public List<OrderItem> Items { get; set; }
    }

    public class OrderItem
    {
        public int No { get; set; }
        public string Name { get; set; }
    }

    public MainWindow()
    {
        InitializeComponent();
        DataContext = this;
        var customers = new List<Customer>
        {
            new Customer
            {
                CustomerId = 1,
                Orders = new List<Order>
                {
                    new Order {OrderId = 1, Items = new List<OrderItem>(new[] { new OrderItem { Name = "CD Player", No = 1}, new OrderItem { Name = "VCR Player", No = 2} })},
                    new Order {OrderId = 2, Items = new List<OrderItem>(new[] { new OrderItem { Name = "DVD Player", No = 1} })}
                }
            },
            new Customer
            {
                CustomerId = 2,
                Orders = new List<Order>
                {
                    new Order {OrderId = 1},
                    new Order {OrderId = 2}
                }
            }
        };
        listView.ItemsSource = customers;
    }
}

并在您的 Xaml 上像这样修改它:

<DataTemplate x:Key="InsideListTemplate">
    <StackPanel Orientation="Horizontal">
        <TextBlock Text="{Binding No}" Width="50"></TextBlock>
        <TextBlock Text="{Binding Name}" Width="50"></TextBlock>
    </StackPanel>
</DataTemplate>
<DataTemplate x:Key="OrdersTemplate">
    <StackPanel>
        <TextBlock Text="{Binding OrderId}" />
        <ListView HorizontalAlignment="Stretch"
              HorizontalContentAlignment="Stretch"
              MinWidth="100"
              MinHeight="25"
              ItemsSource="{Binding Items}" 
              ItemTemplate="{StaticResource InsideListTemplate}" />
    </StackPanel>
</DataTemplate>

您的输出将显示详细信息 Binding

How about changing it list this:

public partial class MainWindow : Window
{
    public class Customer
    {
        public int CustomerId { get; set; }
        public List<Order> Orders { get; set; }
    }

    public class Order
    {
        public int OrderId { get; set; }
        public List<OrderItem> Items { get; set; }
    }

    public class OrderItem
    {
        public int No { get; set; }
        public string Name { get; set; }
    }

    public MainWindow()
    {
        InitializeComponent();
        DataContext = this;
        var customers = new List<Customer>
        {
            new Customer
            {
                CustomerId = 1,
                Orders = new List<Order>
                {
                    new Order {OrderId = 1, Items = new List<OrderItem>(new[] { new OrderItem { Name = "CD Player", No = 1}, new OrderItem { Name = "VCR Player", No = 2} })},
                    new Order {OrderId = 2, Items = new List<OrderItem>(new[] { new OrderItem { Name = "DVD Player", No = 1} })}
                }
            },
            new Customer
            {
                CustomerId = 2,
                Orders = new List<Order>
                {
                    new Order {OrderId = 1},
                    new Order {OrderId = 2}
                }
            }
        };
        listView.ItemsSource = customers;
    }
}

and on your Xaml modify it like this:

<DataTemplate x:Key="InsideListTemplate">
    <StackPanel Orientation="Horizontal">
        <TextBlock Text="{Binding No}" Width="50"></TextBlock>
        <TextBlock Text="{Binding Name}" Width="50"></TextBlock>
    </StackPanel>
</DataTemplate>
<DataTemplate x:Key="OrdersTemplate">
    <StackPanel>
        <TextBlock Text="{Binding OrderId}" />
        <ListView HorizontalAlignment="Stretch"
              HorizontalContentAlignment="Stretch"
              MinWidth="100"
              MinHeight="25"
              ItemsSource="{Binding Items}" 
              ItemTemplate="{StaticResource InsideListTemplate}" />
    </StackPanel>
</DataTemplate>

And your output will display the details Binding

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