Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Generic Windows

I want to make a reusable WPF Window suitable for different types T. I have a designer and a codebehind file.

can I do something like this?

/*  Code behind file */
public partial class MyWindows<T> : Window
{}
like image 365
theSpyCry Avatar asked Aug 24 '09 11:08

theSpyCry


1 Answers

Shamelessly copied from here (and thus not tested)

public class ViewBase<T> : Window, IView where T : class, IViewModel 
{ 
    public virtual T Model 
    { 
        get { return DataContext as T; } 
        set { DataContext = value; } 
    } 
} 

and XAML

<src:ViewBase 
    x:Class="View" 
    x:TypeArguments="src:IViewModel" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:src="clr-namespace:MyNamespace" 
    Height="480" Width="640"> 
... 
</src:ViewBase> 
like image 128
Mikhail Poda Avatar answered Oct 05 '22 00:10

Mikhail Poda