January 27, 2010

Generic Constraints Syntax In C# (using where keyword)

On MSDN
or
Search for MSDN page with this

Takes the form:
... class SomeClass<args>
     where Args : class

Here is a list of valid constraints

where T: struct
The type argument must be a value type. Any value type except Nullable can be specified.

where T : class
The type argument must be a reference type; this applies also to any class, interface, delegate, or array type.

where T : new()
The type argument must have a public parameterless constructor. When used together with other constraints, the new() constraint must be specified last.

where T : <base class name>
The type argument must be or derive from the specified base class.

where T : <interface name>
The type argument must be or implement the specified interface. Can be more than one and the interfaces specified can be generic

where T : U
The type argument supplied for T must be or derive from the argument supplied for U.

January 22, 2010

Hosting WPF Controls In Windows Forms

Host WPF controls in a windows form control (using an ElementHost)
WPF for those who know Windows Forms (Large document)

You must add references to "WindowsBase", "WindowsFormsIntergration", "PresentationCore" and "PresentationFramework"
private ElementHost wpfCtrlHost;
private TestWpfControl testWpfCtrl;

public WindowsFormHost()
{
    InitializeComponent();
 ...
    HostWpfControl();
}

private void HostWpfControl()
{
    wpfCtrlHost = new ElementHost();
    wpfCtrlHost.Dock = DockStyle.Fill;
    this.Controls.Add(wpfCtrlHost);
    testWpfCtrl = new TestWpfControl();
    testWpfCtrl.InitializeComponent();
    wpfCtrlHost.Child = testWpfCtrl;
}