vladc

vladc

  • NA
  • 15
  • 0

How to create dynamically populated items in a listbox and use it as navigation?

Aug 20 2010 5:44 PM

Hello,
I am trying to create navigation inside a listbox menu. I load data from xml to populate a list. Each item in the list box should be a link or button. Then, I create a frame to load xaml pages in it. I tested to load pages employing button control, outside listbox with on click event and it worked well. However, I cannot make it work with list box items. I am hoping to get help from you. Any sample or ideas are highly appreciated.
Below is the code:
XAML:
 

 <ListBox x:Name="Nav_ListBox" Margin="0" ScrollViewer.HorizontalScrollBarVisibility="Hidden" Background="#FFF2F2F2" ItemTemplate="{DynamicResource pageTemplate}" ItemsSource="{Binding pageCollection}" Padding="6" IsTabStop="True" BorderThickness="0" Width="200" SelectedIndex="1" Style="{DynamicResource ListBoxStyle1}"/>

 
XML:
 

 XML:
<?xml version="1.0" encoding="utf-8"?>
<Pages>
<page id="page01">
 <name>Page 01</name>
</page>
<page id="page02">
 <name>Page 02</name>
</page>
</Pages>

C# to initiate a click o n listbox item and get the path to page:
 private void SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)        
{
ListBoxItem lbi = ((sender as ListBox).SelectedItem as ListBoxItem);
string itemName = lbi.Content.ToString();
if ( Nav_ListBox.SelectedItem.Equals("Page01" ) )
{
ContentFrame.Source = new Uri("Pages/Page01.xaml", UriKind.RelativeOrAbsolute);
}
}

Thank you in advance.