20

I want to add a folder containing xaml and cs files plus other directories to a different project on VS. For some reason when I drag the folder from windows explorer to the project where I will like to place that directory visual studio will not let me. Because of that I am manually adding each file and every time I encounter a directory I have to create it. Maybe it is because I am using team foundation server.

Anyways I am adding the files manually so I click on the folder that I want to add the files on visual studio then click on add existing files. Then I select the xaml and code behind file:

enter image description here

when I click add the files get added but visual studio does not recognize that Bytes.xaml.cs is the code behind!

enter image description here

Do I have to manually add a window then copy and paste the contents of the file?

Tono Nam
  • 34,064
  • 78
  • 298
  • 470

3 Answers3

24

Edit your .csproj file to add a "DependentUpon" element below your "Compile" element for the .xaml.cs file so that it will appear "inside" the .xaml file not simply below it.:

<Compile Include="BytesDisplay\SubControls\Bytes.xaml.cs">
  <DependentUpon>Bytes.xaml</DependentUpon>
</Compile>

To easily edit the .csproj file:

Right-click the project and select "Unload Project"
Right-click the project node again and select "Edit [projectname].csproj"
Edit the XML, then close the file
Right-click the project node again and select "Reload Project"
poy
  • 10,063
  • 9
  • 49
  • 74
Vasanthan
  • 310
  • 6
  • 18
19

If you drag-and-drop the .xaml file from Windows Explorer into the Solution Explorer window, it will automatically add the .xaml with the code-behind .cs file.

youzer
  • 493
  • 4
  • 11
  • 5
    This is so much easier than the accepted answer. If you've already added the files, simply exclude them from the project, and then do the drag and drop from Windows Explorer. Applies to VS 2017 as well. – CtrlDot Oct 21 '17 at 12:10
  • In VS 2017 you need to drag over the xaml and xaml.cs files over together – Gerry Mar 12 '19 at 16:58
  • 2
    I tried this but it failed. I discovered that you can't just drag and drop the files into the solution explorer window, you have to drop them right on the project node. VS 2017 – Ross Kelly Mar 24 '19 at 10:34
  • 1
    It's important to drag and drop the xaml file onto the "project name", not in the open space underneath. – ttom Oct 27 '19 at 15:38
0

thanks for the write-up. If you edit the xaml and .cs files class declaration to match without class collisions - then when you add the .xaml file it will pick up the .xaml.cs automatically if it is on the same folder. (vs 2013)