You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WPF中如何在ListView展示选中客户的Purchase列表数据?

解决WPF中ListView展示选中客户采购记录无内容的问题

问题核心原因

  • Customer类中的lp是字段而非属性,WPF数据绑定仅支持绑定公共属性,无法识别字段
  • 未正确将选中客户的采购记录集合绑定到目标ListView,也未为采购记录配置显示模板

修复步骤及代码示例

1. 修正Customer类,将lp改为公共属性

WPF绑定要求目标为公共属性,同时建议初始化集合避免绑定到null:

public class Customer
{
    public string name { get; set; }
    public string surname { get; set; }
    // 改为带getter的公共属性,并初始化集合
    public ObservableCollection<Purchase> lp { get; set; } = new ObservableCollection<Purchase>();
}

2. 调整主窗口配置,确保数据上下文正确

将客户集合lc改为属性,并在构造函数中设置窗口数据上下文,让XAML能识别绑定源:

namespace WpfApp1
{
    public partial class MainWindow : Window
    {
        // 改为公共属性,支持XAML绑定
        public ObservableCollection<Customer> lc { get; set; } = new ObservableCollection<Customer>();

        public MainWindow()
        {
            InitializeComponent();
            // 将窗口自身设为数据上下文
            DataContext = this;

            // 添加测试数据验证功能
            lc.Add(new Customer
            {
                name = "张三",
                surname = "张",
                lp = new ObservableCollection<Purchase>
                {
                    new Purchase { date = DateTime.Now, shop = "生鲜超市" },
                    new Purchase { date = DateTime.Now.AddDays(-2), shop = "连锁便利店" }
                }
            });
        }
    }
}

3. XAML布局:实现客户选择+采购记录展示

通过两个ListView分别实现客户选择和采购记录展示,绑定选中客户的lp集合:

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="客户采购记录" Height="450" Width="800">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="2*"/>
        </Grid.ColumnDefinitions>

        <!-- 左侧:客户选择列表 -->
        <ListView x:Name="CustomerList" ItemsSource="{Binding lc}" Grid.Column="0" Margin="10">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding name}" Margin="0 0 5 0"/>
                        <TextBlock Text="{Binding surname}"/>
                    </StackPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

        <!-- 右侧:选中客户的采购记录列表(表格布局) -->
        <ListView ItemsSource="{Binding ElementName=CustomerList, Path=SelectedItem.lp}" Grid.Column="1" Margin="10">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="采购日期" DisplayMemberBinding="{Binding date, StringFormat='yyyy-MM-dd HH:mm'}"/>
                    <GridViewColumn Header="店铺名称" DisplayMemberBinding="{Binding shop}"/>
                </GridView>
            </ListView.View>
        </ListView>
    </Grid>
</Window>

如果偏好自由布局,可将右侧ListView的View替换为ItemTemplate:

<ListView.ItemTemplate>
    <DataTemplate>
        <StackPanel Orientation="Horizontal" Margin="5">
            <TextBlock Text="{Binding date, StringFormat='yyyy-MM-dd'}" Width="120"/>
            <TextBlock Text="{Binding shop}"/>
        </StackPanel>
    </DataTemplate>
</ListView.ItemTemplate>

4. 可选:实现属性变更通知(动态更新UI)

如果需要在修改客户/采购记录属性时自动刷新UI,让Customer和Purchase实现INotifyPropertyChanged接口:

using System.ComponentModel;
using System.Runtime.CompilerServices;

public class Customer : INotifyPropertyChanged
{
    private string _name;
    public string name
    {
        get => _name;
        set
        {
            _name = value;
            OnPropertyChanged();
        }
    }

    private string _surname;
    public string surname
    {
        get => _surname;
        set
        {
            _surname = value;
            OnPropertyChanged();
        }
    }

    private ObservableCollection<Purchase> _lp;
    public ObservableCollection<Purchase> lp
    {
        get => _lp;
        set
        {
            _lp = value;
            OnPropertyChanged();
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

Purchase类可参照上述代码实现相同接口。

内容的提问来源于stack exchange,提问作者aya

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 08:05:33