In visual studio i want to add a second code behind file to a xaml window (my main form). i know i can have another (or as many) files that form partial parts of a class, and if they are in the same project they will be included, but can i make more than one file sit in the expander (in the solution explorer) when i expand the xaml file to see its code behind?
3 Answers
I blogged about this a few months ago, you will find the explanation in this blog post
Basically, you just need to add a <DependentUpon>
element to your extra code-behind file :
<Compile Include="Window1.Foo.cs">
<DependentUpon>Window1.xaml</DependentUpon>
</Compile>
As a side note : why would you want a second code-behind file ? I think one is bad enough ;). If you use a pattern like MVVM, you will almost never need to write any code-behind...

- 286,951
- 70
- 623
- 758
Your project file (.csproj for example) is actually XML. Open the .csproj file from the open file dialog, and you will see how it is structured. You can hand edit the project file from there.

- 10,141
- 3
- 34
- 46
-
1that works, thank you. before : the new code behind file
just copied the old dependency info from the other one, now i have two code behinds there. file.xaml
one such reason is code organization. in a lot of MVVM implementations which ..
- don't separate V & VM code by projects)
- have a 1-to-1 between View/Xaml & ViewModel
.. it might be advantageous to have your solution explorer look like:
MyView.xaml
-- MyView.xaml.cs
-- MyViewModel.cs
check out this VisualStudio add-in:

- 15,418
- 12
- 44
- 65

- 2,745
- 2
- 40
- 64
-
-
This was when we found a bunch of URL shortened links and were in the middle of a cleanup. – Sep 09 '11 at 01:29