I am working on a WP8 project which needs to binding custom objects to a listbox item. Here is how I am doing it:
XAML file:
<ListBox x:Name="mMenuList">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Tap="OnMenuListItemTapped" DataContext="{Binding Dest}">
<TextBlock Text="{Binding MenuTitle}" Visibility="Visible" FontSize="20" MaxWidth="350" HorizontalAlignment="Left"/>
<Image Source="/Assets/img/search_next_icon.png" Width="20" Height="20" VerticalAlignment="Center" Visibility="{Binding Visibility}" HorizontalAlignment="Right" Tap="OnGotoChildIconTapped"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
and a class for the data items:
public class PDFMenuItem
{
private String _menuTitle;
public String MenuTitle
{
get { return _menuTitle; }
set { _menuTitle = value; }
}
private int _dest;
public int Dest
{
get { return _dest; }
set { _dest = value; }
}
private Visibility _visibility;
public Visibility Visibility
{
get { return _visibility; }
set { _visibility = value; }
}
}
Binding them together:
PDFMenuItem item = new PDFMenuItem();
item.MenuTitle = "Title";
item.Dest = "1";
item.Visibility = Visibility.Visible;
mMenuList.Items.Add(item);
With the implementation, data for StackPanel and Image can be bind correctly, but nothing was bind to the TextBlock. How should I correct this? I also tried to add all items into a list and set it to mMenuList.ItemsSource. Nothing changed.
B.R./Alex