views:

138

answers:

4

I'm rather taken with MEF and plan to use it to build a demo application to load different tabs. I am a begineer at MEF and WPF and although MEF is loading the assemblies I'm stuck at loading the controls into the TabItem I have created. My code looks a bt like this ..

            foreach (var page in pages)
            {
                TabItem item = new TabItem();
                item.Header = page.PageTitle;

                /// Errm???

                // Add each page
                tcPageControl.Items.Add(item);
            }

The tabs are Pages, so I might be doing it totally wrong, and any help would be appreciated.

Thanks in advance

A: 

There aren't many details here - but the basic idea would be to [Export] each "page", potentially within a custom class that gives you page titles, etc.

You'd then use [ImportMany] to import a collection of pages, and build a "tab" for each imported page.

Reed Copsey
This bit I think is working, in is the adding of each pages contents to the control that is failing.
Mmarquee
+1  A: 

Don't have intellisense handy, but I think you need to set the content of the item to be your page. Something like:

    foreach (var page in pages)
    {
        TabItem item = new TabItem();
        item.Header = page.PageTitle;

        item.Children.Add(page);
        //or item.Grid.Children.Add(page) or something like that.

        // Add each page
        tcPageControl.Items.Add(item);
    }
Robaticus
A: 

You simply need to set the Content property of your TabItem to be the page, like this:

foreach (var page in pages) 
{ 
  TabItem item = new TabItem(); 
  item.Header = page.PageTitle; 

  item.Content = page;

  tcPageControl.Items.Add(item); 
} 

Here is a much more elegant way to code it using LINQ:

tcPageControl.ItemsSource =
  from page in pages
  select new TabItem
  {
    Header = page.PageTitle,
    Content = page,
  };

In general in WPF you never have to write a "foreach" loop if you structure your code correctly.

Ray Burns
A: 

I solved my problem, Pages can only have Frames as their parents, so adding this code addresses it.

            foreach (var page in pages)
            {
                TabItem item = new TabItem();
                item.Header = page.PageTitle;

                // Now add some controls

                Frame frame = new Frame();

                frame.Content = page.View;

                item.Content = frame;

                // Add each page
                tcPageControl.Items.Add(item);

                //tcPageControl.Children.Add(view.Value);
            }
Mmarquee