Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF: Page to page navigation from listbox menu employing frame technique?

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;
    }
}

`

like image 249
vladc77 Avatar asked Aug 20 '10 09:08

vladc77


People also ask

How do I navigate to another page in WPF?

To package content for navigation, WPF provides the Page class. You can navigate from one Page to another declaratively, by using a Hyperlink, or programmatically, by using the NavigationService. WPF uses the journal to remember pages that have been navigated from and to navigate back to them.

How do I navigate between windows in WPF?

NavigationService is for browser navigation within WPF. What you are trying to do is change to a different window TrainingFrm . To go to a different window, you should do this: private void conditioningBtn_Click(object sender, RoutedEventArgs e) { var newForm = new TrainingFrm(); //create your new form.

What is frame in WPF?

The Frame control supports content navigation within content. Frame can be hosted by a root element like Window, NavigationWindow, Page, UserControl, FlowDocument, or as an island within a content tree that belongs to a root element.

What is a WPF page?

Windows Presentation Foundation (WPF) supports browser-style navigation that can be used in two types of applications: standalone applications and XAML browser applications (XBAPs). To package content for navigation, WPF provides the Page class.


1 Answers

I cant exactly understand why your frame cant be referenced. Did you try another name? I would like you to suggest another smarter way to do this. You can use a Binding for the source. Here is a little example:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="24"/>
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>

    <ComboBox x:Name="SourceBox" Grid.Row="0"
              VerticalAlignment="Top"
              DisplayMemberPath="Label"
              TextBlock.Foreground="Black"></ComboBox>

    <Frame NavigationUIVisibility="Hidden" 
           JournalOwnership="OwnsJournal" Grid.Row="1"
           Source="{Binding ElementName=SourceBox, Path=SelectedItem.Source}"/>
</Grid>

Notice the Source binding on the frame. There is also no more x:Name on the Frame.

In the code behind you have to build a propper ItemSource for the combobox. Therefore i built a simple object, which holds a lable and a source.

public class SourceHolder
{
    public string Label { get; set; }
    public Uri Source { get; set; }
}

In the constructor of your window you can assign the itemssource to your combobox:

public Window1()
{
    List<SourceHolder> sources = new List<SourceHolder>();
    sources.Add(new SourceHolder()
                {
                    Label = "Page1",
                    Source = new Uri("Page1.xaml", UriKind.Relative)
                }
        );

    sources.Add(new SourceHolder()
                {
                    Label = "Page2",
                    Source = new Uri("Page2.xaml", UriKind.Relative)
                }
        );

    InitializeComponent();

    this.SourceBox.ItemsSource = sources;

}

The result is, that the combobox has two items (Page1, Page2). If you change the item, the frame updates it's content with the given Source of the selected combobox item.

Jan

like image 59
JanW Avatar answered Oct 12 '22 09:10

JanW