0

I want to be able to access public members of my Window from within its TabControl Pages. To do that, I've tried switched from using XAML:

    <TabControl TabStripPlacement="Left" HorizontalContentAlignment="Left" Name="wizardTabs">
        <TabItem Header="Login">
            <Frame Source="LoginPage.xaml" IsTabStop="False"/>
        </TabItem>
    </TabControl>

to code-based population of the tabs instead, during the Loaded event of the main window:

        newFrame = new Frame();
        newFrame.Source = new Uri(@"\LoginPage.xaml", UriKind.Relative);
        newFrame.IsTabStop = false;
        tabItem = new TabItem();
        tabItem.Header = "Login";
        tabItem.Content = newFrame;
        wizardTabs.Items.Add(tabItem);

I'm using a Frame in the Tab and loading a XAML Page in the Frame. In this way, I thought that the constructor would be available to me so that I can pass a this pointer into the Page class:

        LoginPage loginPage = newFrame.Content as LoginPage;
        loginPage.parent = this;

but I have discovered that loading XAML requires an empty constructor. Also, newFrame.Content is always NULL. I'm thinking it's related to the fact that the XAML hasn't been loaded at this stage, but I can't figure out when that XAML load will be performed, and if it's later, how I can set the "parent" pointer.

Thanks for any suggestions. I'm a newbie WPF coder, so please guide me.

Jarvis
  • 681
  • 8
  • 33
  • Is there a reason you can't use data binding to access the properties? – Raj Ranjhan Feb 01 '12 at 18:21
  • Well it's not related to the data--I'm trying to update a status indicator on the main window when the user clicks a particular list item within the tab. – Jarvis Feb 01 '12 at 21:03

2 Answers2

0

Take a look at this answer. You can use Setter Property to bind your frame to the content property:

<TabControl TabStripPlacement="Left" HorizontalContentAlignment="Left" Name="wizardTabs" ItemsSource= [YourBinding]>
  <TabControl.ItemContainerStyle>
    <Style TargetType="TabItem">        
        <Setter Property="Content" Value="{Binding newFrame}"/>
    </Style>
</TabControl.ItemContainerStyle>
    <TabItem Header="Login">
        <Frame Source="LoginPage.xaml" IsTabStop="False"/>
    </TabItem>
</TabControl>
Community
  • 1
  • 1
Raj Ranjhan
  • 3,869
  • 2
  • 19
  • 29
0

Ok I found a solution. Instead of using the .Source property, I can use the Navigate() method using an instance of the object directly instead of using a URI. So the code looks like this instead:

newFrame = new Frame();
newFrame.Navigate(new LoginPage(this));
newFrame.IsTabStop = false;
tabItem = new TabItem();
tabItem.Header = "Login";
tabItem.Content = newFrame;
wizardTabs.Items.Add(tabItem);
Jarvis
  • 681
  • 8
  • 33