WPF中listview控件绑定了数据但是运行的时候数据不显示是为什么?怎么解决呢??

无悔青春_j进无止境 2017-06-08 09:29:16

private void Button_Click_2(object sender, RoutedEventArgs e)
{

ObservableCollection<PersonalInfo> personalInfoList = new ObservableCollection<PersonalInfo>();//声明一个动态数据集合类的对象
//给数据集合添加类成员
personalInfoList.Add(new PersonalInfo("雀巢咖啡",5,10,"无"));
personalInfoList.Add(new PersonalInfo("脉动",20,5,"无"));
personalInfoList.Add(new PersonalInfo("三只松鼠",10,10,"无"));
personalInfoList.Add(new PersonalInfo("康师傅方便面",15,4.5,"无"));
personalInfoList.Add(new PersonalInfo("薯片",50,8,"无"));
listView.ItemsSource = personalInfoList;//将数据和UI绑定在一起

}

这是其中一段代码,我想把这些数据显示在列表里,但运行的时候是这样的
...全文
1519 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
Goods_Name = GoodsName; ->GOODSNAME = GoodsName;
xuggzu 2017-06-08
  • 打赏
  • 举报
回复
而且你虽然用了INotifyPropertyChanged,但没使用它。。。。 当然,你的代码不需要实现INotifyPropertyChanged。
xuggzu 2017-06-08
  • 打赏
  • 举报
回复
。。。 xaml绑定要用属性名,不是private的变量名,要用这个GOODSNAME。。。。
  • 打赏
  • 举报
回复
引用 4 楼 xuggzu 的回复:
估计楼主的PersonalInfo的写法有问题:: wpf下,类的属性必须手动实现get方法,否则缺省只有set,即: class PersonalInfo { public string Goods_Name {get; set;} ..... } 不能这样: class PersonalInfo { public string Goods_Name = string.Empty; ..... }
我上面写的类有问题吗
  • 打赏
  • 举报
回复
引用 4 楼 xuggzu 的回复:
估计楼主的PersonalInfo的写法有问题:: wpf下,类的属性必须手动实现get方法,否则缺省只有set,即: class PersonalInfo { public string Goods_Name {get; set;} ..... } 不能这样: class PersonalInfo { public string Goods_Name = string.Empty; ..... }

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;

namespace Demo3
{
    class PersonalInfo:INotifyPropertyChanged  //实现接口
    {
        private string Goods_Name;
        private int Quantity_;
        private double Price_;
        private string Prepare_;
        //构造方法对字段初始化
        public PersonalInfo(string GoodsName, int Quantity, double Price, string Prepare)
        {
            Goods_Name = GoodsName;
            Quantity_ = Quantity;
            Price_ = Price;
            Prepare_ = Prepare;
        }
        //商品名称
        public string GOODSNAME
        {
            get { return Goods_Name; }
            set { Goods_Name = value; }
        }
        //商品数量
        public int QUANTITY
        {
            get { return Quantity_; }
            set { Quantity_ = value; }
        }
        //价格
        public double PRICE
        {
            get { return Price_; }
            set { Price_ = value; }
        }
        //备注
        public string PREPARE
        {
            get { return Prepare_; }
            set { Prepare_ = value; }
        }

        
    }
}

xuggzu 2017-06-08
  • 打赏
  • 举报
回复
估计楼主的PersonalInfo的写法有问题:: wpf下,类的属性必须手动实现get方法,否则缺省只有set,即: class PersonalInfo { public string Goods_Name {get; set;} ..... } 不能这样: class PersonalInfo { public string Goods_Name = string.Empty; ..... }
  • 打赏
  • 举报
回复
Quantity_ 怎么又 下划线,调试看看属性都有值么
  • 打赏
  • 举报
回复
引用 1 楼 duanzi_peng 的回复:
前台xaml,你需要设定listView的视图,类似,

<ListView>
<ListView.View>
                    <GridView>
                        <GridView.Columns>
                            <GridViewColumn Header="商品名称" Width="70" DisplayMemberBinding="{Binding Name}">
                            </GridViewColumn>
                        </GridView.Columns>
                    </GridView>
                </ListView.View>
            </ListView>
DisplayMemberBinding 用来显示属性值
谢谢啦,前台的代码我写了,哪里有问题

<ListView x:Name="listView" HorizontalAlignment="Left" Height="252" Margin="30,81,0,0" VerticalAlignment="Top" Width="501">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="商品名称" Width="150" DisplayMemberBinding="{Binding Path=Goods_Name}"/>
                    <GridViewColumn Header="数量" Width="150" DisplayMemberBinding="{Binding Path=Quantity_}"/>
                    <GridViewColumn Header="价格" Width="150" DisplayMemberBinding="{Binding Path=Price_}"/>
                    <GridViewColumn Header="备注" Width="30" DisplayMemberBinding="{Binding Path=Prepare_}"/>
                </GridView>
            </ListView.View>
        </ListView>
  • 打赏
  • 举报
回复
前台xaml,你需要设定listView的视图,类似,

<ListView>
<ListView.View>
                    <GridView>
                        <GridView.Columns>
                            <GridViewColumn Header="商品名称" Width="70" DisplayMemberBinding="{Binding Name}">
                            </GridViewColumn>
                        </GridView.Columns>
                    </GridView>
                </ListView.View>
            </ListView>
DisplayMemberBinding 用来显示属性值
  • 打赏
  • 举报
回复
引用 9 楼 duanzi_peng 的回复:
Goods_Name = GoodsName; ->GOODSNAME = GoodsName;
明白了
  • 打赏
  • 举报
回复
引用 7 楼 xuggzu 的回复:
。。。 xaml绑定要用属性名,不是private的变量名,要用这个GOODSNAME。。。。
好的明白了

111,130

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧