Removed the code-behind file from MainWindow.xaml and then re-created it. When adding events to markup on elements, VS says:

"The document element has no program part code file. Before adding event handlers, add the program part code file and class definition."

How to reconnect the code file and markup? Structure

  • 2
    You have different names, in one case MainWindow , and in the second MainWindows , it should be without "s" - Bulson

2 answers 2

This can be done by manually editing the .csproj file. But the easiest way is to simply copy the text of the .xaml and .xaml.cs files, delete them, add a new window through Visual Studio, and replace the generated text with the saved one.


If you really want to dig into .csproj, the element describing the dependent .xaml.cs file should look like this:

 <Compile Include="MainWindow.xaml.cs"> <DependentUpon>MainWindow.xaml</DependentUpon> <SubType>Code</SubType> </Compile> 

Pay attention to the DependentUpon part.

And do not forget to unload Visual Studio at the time of editing the project.

    x:Class="ВашеНазваниеПроекта.MainWindow and

     namespace ВашеНазваниеПроекта { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } }