0

I have a .NET5 WPF project. In order to regulate my project, the files follows a specific pattern: There are files end with "View.xaml" which are WPF ContentControls, and files end with "ViewModel.cs" which are INotifyPropertyChanged C# classes. All of them exist in pairs. For the purpose of making the solution explorer more neat, I would like the ViewModel.cs files be nested under their corresponding View.xaml files, as shown in the following screenshot. enter image description here

This, what I have done already, was acheived by adding the following code to the csproj file:

    <ItemGroup>
        <Content Include="**\GameWorldViewModel.cs">
            <DependentUpon>GameWorldView.xaml</DependentUpon>
        </Content>
    </ItemGroup>

However, I don't want to respectively write such code for every one of them. I want it to be something like this:

    <ItemGroup>
        <Content Include="**\*ViewModel.cs">
            <DependentUpon>(GetFileNameWithout"ViewModel.cs")View.xaml</DependentUpon>
        </Content>
    </ItemGroup>

How to do this?

martinrhan
  • 362
  • 1
  • 18
  • Does this answer your question? [How to add a glob for resx files for new SDK csproj file](https://stackoverflow.com/questions/46584499/how-to-add-a-glob-for-resx-files-for-new-sdk-csproj-file) – stijn Sep 15 '21 at 14:47
  • See link: it's probably `$([System.String]::Copy('%(Identity)').Replace('ViewModel.cs',''))View.xaml` – stijn Sep 15 '21 at 14:48

1 Answers1

0

I found the answer.

  <ItemGroup>
    <ViewModelFiles Include="**\*ViewModel.cs"/>
    <Content Include="@(ViewModelFiles)">
      <DependentUpon>$([System.String]::Copy('%(FileName)').Replace('Model', '.xaml'))</DependentUpon>
    </Content>
  </ItemGroup>

Edit

the keyword "content" actually mark this files type as content which is usually not the correct one. Instead, it should be replaced by "compile" if its a c# code file. Further more, as by default all .cs files not declared in .csproj file are marked as compile, using include will cause double include error, so it should be update instead.

My much better version is like this:

<ItemGroup>
  <Compile Update="**\*ViewModel.cs"
      <DependentUpon>$([System.String]::Copy('%(FileName)').Replace('Model', '.xaml'))</DependentUpon>
  </Compile>
</ItemGroup>
martinrhan
  • 362
  • 1
  • 18