我尝试使用具有预定义布局的对象填充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')..
答案 0 :(得分:1)
您绑定到公共字段不属性,这正是错误消息告诉您的内容。将模型的字段更改为属性:
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;
}
}