When you create a WPF application, the start-up window is by default one from the same project (by default called Window1.xaml).

< Application x:Class="WpfApplication1.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    StartupUri="Window1.xaml" >
    < Application.Resources >

    < /Application.Resources >
< /Application >

But what if you want to use a window from another project (class library)? The pack URI scheme, used by WPF, allows you to identify and load files from:

  • the current assembly
  • a referenced assembly
  • a location relative to an assembly
  • the site of origin for the application

The format of the pack URI is pack://authority/path. The authority identifies the type of package and the path the location of a part inside a package. There are two authorities supported by WPF:

  • application:/// identifies application data files (known at compile time)
  • siteoforigin:/// identifies site of origin files

To use resource files from a referenced assembly you need to use the application:/// authority, and the path must have the form AssemblyShortName[;Version][;PublicKey];component/Path. Version and PublicKey are optional.

Let’s say you want to use a XAML called SampleWindow.xaml from a referenced assembly called WpfDemoLib. The App.xaml file should look like this:

< Application x:Class="WpfApplication1.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    StartupUri="pack://application:,,,/WpfDemoLib;component/SampleWindow.xaml" >
    < Application.Resources >

    < /Application.Resources >
< /Application >

You can learn more about pack URIs in WPF from MSDN.

, , , , Hits for this post: 4324 .

A WPF TabControl contains multiple items (TabItem), just like any tab control, and adding new tabs is quite easy. The following example shows a tab control with two tab items.

Empty tab with two items


    
		
			
			
		
	

However, when you try adding child controls to a TabItem you’ll notice that you can actually add one single child. The following XAML code yields an error.


    
	
		
			
			
		
		
	
   

error MC3089: The object ‘TabItem’ already has a child and cannot add ‘TextBox’. ‘TabItem’ can accept only one child. Line 9 Position 6.
The solution however is simple: use a grid or panel (Grid, UniformGrid, StackPanel or WrapPanel) to host multiple items. In the example bellow a Grid control was used to host the label and textbox.


    
	
		
			
				
				
			
		
		
	
   

The result can be seen in the following image:

Multiple items in a grid in a tab item

You can create all that from code. The equivalent in C# is the following:

public Window1()
{
        InitializeComponent();

        // create a new tab control
        TabControl tab = new TabControl();

        // create tab items and add them to the tab control
        TabItem item1 = new TabItem();
        item1.Header = "First";
        tab.Items.Add(item1);

        TabItem item2 = new TabItem();
        item2.Header = "Second";
        tab.Items.Add(item2);

        // create a new grid to host other controls
        Grid grid1 = new Grid();

        // create a label and add it to the grid childred
        Label label1 = new Label();
        label1.Margin = new Thickness(10, 10, 0, 0);
        label1.VerticalAlignment = VerticalAlignment.Top;
        label1.HorizontalAlignment = HorizontalAlignment.Left;
        label1.Width = 66;
        label1.Content = "Name";
        grid1.Children.Add(label1);

        // create a textbox and add it to the grid childred
        TextBox textBox1 = new TextBox();
        textBox1.Margin = new Thickness(80, 10, 10, 0);
        textBox1.VerticalAlignment = VerticalAlignment.Top;
        grid1.Children.Add(textBox1);

        // set the grid as the content of a tab item
        item1.Content = grid1;

        // add the tab control to the main grid of the window
        masterGrid.Children.Add(tab);
}

where masterGrid is the main grid in the window:

public partial class Window1 :
     System.Windows.Window, System.Windows.Markup.IComponentConnector {

     internal System.Windows.Controls.Grid masterGrid;



Hits for this post: 13145 .