我正在處理需要將自定義對象綁定到列表框項目的WP8項目。這裏是我正在做它:綁定ListBox的自定義對象
XAML文件:
<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>
和數據項的類:
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; }
}
}
綁定在一起:
PDFMenuItem item = new PDFMenuItem();
item.MenuTitle = "Title";
item.Dest = "1";
item.Visibility = Visibility.Visible;
mMenuList.Items.Add(item);
隨着實現,StackPanel和Image的數據可以正確綁定,但沒有任何內容綁定到TextBlock。我應該如何糾正這一點?我也嘗試將所有項目添加到列表中,並將其設置爲mMenuList.ItemsSource。沒有改變。
B.R./Alex
爲什麼你爲你的StackPanel使用DataContext並綁定到Dest? –