c# - WPF - Binding to ListView cannot work -
code behind, try bind listview obserablecollection packageinfos. however, when run it, shows no information. can slove it?
public partial class mainwindow : window { public mainwindow() { initializecomponent(); packageinfos = new observablecollection<packageinfo>(); polulatepackageinfos(); datacontext = this; } public observablecollection<packageinfo> packageinfos { get; private set; } private void polulatepackageinfos() { packageinfos.add(new packageinfo() { name = "apple", pricerate = "10", imageurl = "images/greatwallofchina.jpg" }); packageinfos.add(new packageinfo() { name = "beijing", pricerate = "6", imageurl = "images/greatwallofchina.jpg" }); packageinfos.add(new packageinfo() { name = "china", pricerate = "4", imageurl = "images/greatwallofchina.jpg" }); } }
xaml section
<window 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" mc:ignorable="d" title="mainwindow" height="585" width="320" x:class="vendingmachine.mainwindow xmlns:local="clr-namespace:vendingmachine"> <grid style="{staticresource gridstyler}"> <grid.rowdefinitions> <rowdefinition height="3*"/> <rowdefinition height="3*"/> <rowdefinition height="3*"/> <rowdefinition height="1*"/> </grid.rowdefinitions> <scrollviewer scrollviewer.horizontalscrollbarvisibility="hidden" scrollviewer.verticalscrollbarvisibility="hidden" panningmode="both" grid.row="1"> <listview itemssource="{binding packageinfos, updatesourcetrigger=propertychanged}" > <listview.itemtemplate> <datatemplate datatype="{x:type local:packageinfo}"> <stackpanel orientation="horizontal"> <textblock text="{binding name}" /> <textblock text="{binding pricerate}" /> </stackpanel> </datatemplate> </listview.itemtemplate> </listview> </scrollviewer> </grid> </window>
you dont have code. class packageinfo im betting class incorrect. should
public class packageinfo { public string name { get; set; } public string pricerate { get; set; } public string imageurl { get; set; } }
i used code class , displayed fine me. oh had remove grid styler xaml because dont have code.
Comments
Post a Comment