I got a problem. I added a frame in the window xaml to load pages in. I can directly load a page into frame with Source tag of the frame. It works. I need to use the code in C# to refer to the link from listbox menu a poplulate an apropriate link when an listbox item is selected. My problem is that I cannot refer the frame in C# code, it just cannot be seen.
I defined the frame with x:Name=”ContentFrame”. When I refer to in in C#, Intellisense tells that “The name “ContentFrame” does not exist in the current context”. What I am doing wrong? I am lost here. Any ideas are highly appreciated.
Here is the code:
XAML:
<Frame x:Name="ContentFrame" JournalOwnership="OwnsJournal" NavigationUIVisibility="Hidden" Grid.Column="2" </Frame>
C#
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("Page1" ) )
{
ContentFrame.Source = new Uri("Pages/Page1.xaml", UriKind.Relative);
Canvas_Frame.NavigationUIVisibility = NavigationUIVisibility.Hidden;
}
}
`
You did it almost right. The only problem is the binding to the selected item. Since the Source property of the frame is of type Uri, and has no dynamic converter, you need an own converter, which does the job:
You now bind the selected item directly with no xpath and the converter extracts the uri string and builds an Uri object. Here is the fully working xaml (no code behind, except the converter):
The pages have to be in the pages folder of the same directory of the window of course. In my example, I had two pages with a TextBlock “I am Page1/Page2”.
Hope i could help you 🙂