WPF FAQ - Window

How do I show a MessaegBox after my main Window and it’s contents are all loaded and visible?

Use the Dispatcher object to make your method run after all the ‘background’ operations are completed, as follows:

public Window1()
        {
            InitializeComponent();
            this.Dispatcher.BeginInvoke(DispatcherPriority.Background, new SomeMethodDelegate(DoSomething));
        }

        private delegate void SomeMethodDelegate();

        private void DoSomething()
        {
            MessageBox.Show('Everything is loaded.');
        }
Permalink
How do I load a Window from a XAML file dynamically?

You can load a xaml file dynamically as follows:

[C#]
// This file should then be next to the exe. If not specify a relative path.
FileStream xamlFile = new FileStream('ChartWindow.xaml', FileMode.Open, FileAccess.Read);

Window win = XamlReader.Load(xamlFile) as Window;

win.Show();

Note that any code-behind associated with the above xaml will not be loaded/processed.

In case, you have an assembly reference to a custom dll in the above xaml file, you should reference that assembly with a fully qualified namespace as follows:

[XAML]
xmlns:syncfusion='clr-namespace:Syncfusion.Windows.Chart;assembly=Syncfusion.Chart.WPF,Version=6.303.0.6,Culture=neutral,PublicKeyToken=3d67ed1f87d44c89'

The above assembly should then be present in the GAC for the runtime to find it.

Permalink
Does WPF support MDI ?

No. WPF from version 1 does not include support for MDI. If there is a need for MDI in your application, you can use the windows forms interop or create a custom child window manager.

Permalink
How do I host Windows Forms control in a WPF Window ?

Windows forms can be hosted using the WindowsFormsHost markup extension of the ‘windowsformsintegration.dll’. References for ‘windowsformsintegration.dll’ and ‘System.Windows.Forms.dll’ have to be added.

The following code snippet is used to add a control to the Windows Forms.

Note: windowsformsintegration.dll will be available in the following location,
Program files\Reference Assemblies\Microsoft\Framework\v3.0\WindowsFormsIntegration.dll.

[XAML]

<WindowsFormsHost>
<wf:MaskedTextBox x:Name='mtxamt' Mask='00000.00'/>        </WindowsFormsHost>
Permalink
How do I create a child window in WPF ?

In WPF, any of the windows can be designated as child windows. A child window is like any other window but it gets minimized or closed when the parent is minimized or closed respectively. This type of window is called modeless dialog. To make a window a child of another window, the ‘Owner’ property of the child window has to be set as the parent window but only after the parent window is shown. Child windows of a window can be enumerated using the ‘OwnedWindows’ read-only property.

Permalink