Rasto
Rasto

Reputation: 17794

Change default Visual Studio template for new Window and UserControl (WPF)

Where can I customize what Visual Studio puts into new class of certain type ? I want to add one line into template for WPF UserControl and Window so the code of new window when you create it looks like this:

public partial class MainWindow : Window {
    public MainWindow() {
        InitializeComponent();
        DataContext = this;
    }
}

Instead of what is generated right now:

public partial class MainWindow : Window {
    public MainWindow() {
        InitializeComponent();
    }
}

EDIT: DataContext="{Binding RelativeSource={RelativeSource Self}}" to the XAML template.

Upvotes: 3

Views: 1274

Answers (1)

Tejs
Tejs

Reputation: 41246

You can actually make your own templates and then use those instead of the default template. Simply make a new project and configure your files as you want them. Then, hit File > Export Template. You will choose Item template, then select the files you want to use as the template (in this case, your xaml and xaml.cs files). Then follow the wizard and drop the .zip file it creates in the right directory of your Visual Studio installation, and then 'Viola!' when you hit Add > New Item... > Your Template will be displayed.

Upvotes: 3

Related Questions