Bind to a property value

Topics: Getting Started
Jan 5, 2013 at 6:35 AM

How can you bind something to a property value, so I have a collection of items like:

 

public class Item
{
  public string Title { get; set; }
  public Detail Details { get; set; }
}

public class Detail
{
  public string Snippet { get; set; }
  public string SubTitle { get; set; }
}


This is the result from deserialized json response. 


I'm wondering how I can bind a TextBlock value to 


Details.Subtitle


Is it possible or do I need to flattern the object before binding it?

Jan 6, 2013 at 2:06 AM
Edited Jan 6, 2013 at 2:11 AM

is the collection going to change? if yes then BindableCollection in CM (which is derived from ObservableCollection).

BindableCollection<Item> _items= BindableCollection<Item>();
_items.AddRange(jsoncontext.ToList());

public BindableCollection<Item> Items{get;set;} //contents left for drill

ListView control or your preference, do as necessary to show your data that you want with standard template

<!--Convention-->
<ListView x:Name="Items">                
 <ListView.ItemTemplate>
     <DataTemplate>
          <TextBlock Text="{Binding Path=Details.SubTitle}" />
     </DataTemplate>
  </ListVIew.ItemTemplate>
</ListView>

<!--Non-convention-->
<ListView ItemsSource="{Binding Items}" >
  <ListView.ItemTemplate>
     <DataTemplate>
          <TextBlock Text="{Binding Path=Details.SubTitle}" />
     </DataTemplate>
  </ListVIew.ItemTemplate>
</ListView>