How can I connect xaml and xaml.cs files

I had a VB projected and converted it to C# using online conversion tools. Now the problem is xaml and xaml.cs file do not connect to each other, that is they don't recognize their dependencies (Red area in Fig). Actually it should appear like Window1 Files (Green Area in the image.) How can I achieve this.

Solution Explorer

I am trying my hands on WPF so may be a layman sort of question.


This is simple, try to add in project existing items and select the XAML (not .cs, etc.) files in list of formats. In VS2010 thats helps.


If you cannot get the IDE to do it (Papa John's post), then you can do it by editing the project file.

That information is in the .csproj file (which is an XML file -- you can open it in a text editor, or by right-clicking on it, choosing "unload", and then opening it -- choose reload to load it up as a project again).

Here is the information to look for, you would need to add the "DependentUpon" tag.

<Compile Include="TheFile.xaml.cs">
  <DependentUpon>TheFile.xaml</DependentUpon>
</Compile>

Easiest Way!!!

I came across the same. I got the way out. Here is how to get the .xaml.cs nested under the .xaml in Solution Explorer:

  1. In Windows File Explorer (outside of Visual Studio), open the folder where the required files are.
  2. Select both files (.xaml and .xaml.cs) together.
  3. Drag it onto your project name in the Solution Explorer.
  4. Its done! :)

Using a Xamarin PCL Solution:

1) Go to your PCL folder and open your MySolution.csproj file

2) There should be several groups of <ItemGroup> tags. One of them declares <EmbeddedResource> tags and another will contain, <Compile> <DependentUpon></DependentUpon></Compile> groups of tags.

3) For MyPage.xaml and MyPage.xaml.cs files to be linked, you must have a group of xmls that declare your xaml page.

<EmbeddedResource Include="MyPage.xaml">
      <Generator>MSBuild:UpdateDesignTimeXaml</Generator>
      <LogicalName>MyPage.xaml</LogicalName>
</EmbeddedResource>
<Compile Include="MyPage.xaml.cs">
      <DependentUpon>MyPage.xaml</DependentUpon>
</Compile>

Note that if your page is in a folder you should specify that like so:

<Compile Include="Views\MyPage.xaml.cs">
      <DependentUpon>MyPage.xaml</DependentUpon>
</Compile>
<EmbeddedResource Include="Views\MyPage.xaml">
      <Generator>MSBuild:UpdateDesignTimeXaml</Generator>
      <LogicalName>MyPage.xaml</LogicalName>
</EmbeddedResource>

Note that this works with OSX and Windows