Вопрос

I'm trying to populate a ListBox with Objects with a Predefined Layout, but I can't find a solid example. Here is what I currently do. Any tips would be appreciated.

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;
    });
}

Data Object:

public partial class Page
{
    public String Name;
    public String Icon;

    public Page(String name, String icon)
    {
        Name = name;
        Icon = icon;
    }
}

Error:

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')..
Это было полезно?

Решение

You're binding to the public field not property that's exactly what the error message tell's you. Change your model's fields to properties:

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;
    }
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top