用预定义的布局填充列表框
本文关键字:列表 填充 布局 预定义 | 更新日期: 2023-09-27 18:03:16
我试图用预定义布局的对象填充ListBox,但我找不到一个坚实的例子。这是我目前所做的。如有任何建议,我将不胜感激。
XAML:<ListBox x:Name="listBox1">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<Image Source="{Binding Icon}"/>
<TextBlock Text="{Binding Name}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
PopulationCode:
List<JObject> json = JsonConvert.DeserializeObject<List<JObject>>(response);
for (int i = 0; i < json.Count(); i++)
{
JObject location = json[i];
pages.Add(new Page(location.GetValue("name").ToString(), location.GetValue("icon").ToString()));
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
if (this.listBox1 == null)
{
Out("list==null");
}
else if (pages == null)
{
Out("pages==null");
}
else
listBox1.ItemsSource = pages;
});
}
数据对象:public partial class Page
{
public String Name;
public String Icon;
public Page(String name, String icon)
{
Name = name;
Icon = icon;
}
}
错误:System.Windows.Data Error: BindingExpression path error: 'Icon' property not found on 'Mavie_Base.MainPage+Page' 'Mavie_Base.MainPage+Page' (HashCode=45797138). BindingExpression: Path='Icon' DataItem='Mavie_Base.MainPage+Page' (HashCode=45797138); target element is 'System.Windows.Controls.Image' (Name=''); target property is 'Source' (type 'System.Windows.Media.ImageSource')..
System.Windows.Data Error: BindingExpression path error: 'Name' property not found on 'Mavie_Base.MainPage+Page' 'Mavie_Base.MainPage+Page' (HashCode=45797138). BindingExpression: Path='Name' DataItem='Mavie_Base.MainPage+Page' (HashCode=45797138); target element is 'System.Windows.Controls.TextBlock' (Name=''); target property is 'Text' (type 'System.String')..
System.Windows.Data Error: BindingExpression path error: 'Icon' property not found on 'Mavie_Base.MainPage+Page' 'Mavie_Base.MainPage+Page' (HashCode=5425146). BindingExpression: Path='Icon' DataItem='Mavie_Base.MainPage+Page' (HashCode=5425146); target element is 'System.Windows.Controls.Image' (Name=''); target property is 'Source' (type 'System.Windows.Media.ImageSource')..
System.Windows.Data Error: BindingExpression path error: 'Name' property not found on 'Mavie_Base.MainPage+Page' 'Mavie_Base.MainPage+Page' (HashCode=5425146). BindingExpression: Path='Name' DataItem='Mavie_Base.MainPage+Page' (HashCode=5425146); target element is 'System.Windows.Controls.TextBlock' (Name=''); target property is 'Text' (type 'System.String')..
System.Windows.Data Error: BindingExpression path error: 'Icon' property not found on 'Mavie_Base.MainPage+Page' 'Mavie_Base.MainPage+Page' (HashCode=5541955). BindingExpression: Path='Icon' DataItem='Mavie_Base.MainPage+Page' (HashCode=5541955); target element is 'System.Windows.Controls.Image' (Name=''); target property is 'Source' (type 'System.Windows.Media.ImageSource')..
System.Windows.Data Error: BindingExpression path error: 'Name' property not found on 'Mavie_Base.MainPage+Page' 'Mavie_Base.MainPage+Page' (HashCode=5541955). BindingExpression: Path='Name' DataItem='Mavie_Base.MainPage+Page' (HashCode=5541955); target element is 'System.Windows.Controls.TextBlock' (Name=''); target property is 'Text' (type 'System.String')..
您绑定到公共字段 not属性,这正是错误消息告诉您的。将模型的字段更改为属性:
public partial class Page
{
public String Name { get; private set; }
public String Icon { get; private set; }
public Page(String name, String icon)
{
Name = name;
Icon = icon;
}
}