I have a ListBox, a Show Button, and a TextBlock in my Windows Phone application.
Whenever the user clicks on the Show Button, an item from the ListBox
should be shown in TextBlock. If the user clicks on Show Button again, the next item should be shown.
XAML
<ListBox x:Name="FavoriteListBox"
SelectionChanged="FavoriteListBox_SelectionChanged"
ItemContainerStyle="{StaticResource CustomListBoxItemStyle}"
Height="300" Width="250">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock x:Name="FavoriteListBoxTextBlock"
FontSize="40" FontWeight="SemiBold"
Text="{Binding AnswerName}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<TextBlock x:Name="DisplayTextBlock"/>
<Button x:Name="ShowButton" Click="ShowButton_Click"/>
C#
private void ShowButton_Click(object sender, EventArgs e)
{
if(FavoriteListBox != null)
{
// ??????
}
}
How can achieve such functionality?
This can be quite easily using indices directly.
Suppose the list you use for the
ListBoxitems is calledlistobj, then you can use the following:Note you don't have to check whether
FavoriteListBoxisnull, because such situation will never happen - all controls are initialized with theInitializeComponentcall in the constructor.