Project Structure in WPF
A default WPF application created from Visual Studio's 'WPF Application' template contains a small, fixed set of files: an App.xaml/App.xaml.cs pair defining the application object, a MainWindow.xaml/MainWindow.xaml.cs pair for the first window, an AssemblyInfo.cs, and an SDK-style .csproj with <UseWPF>true</UseWPF> and a Windows-specific target framework like net8.0-windows. Beyond that starting point, WPF doesn't mandate any particular folder layout, so most real projects adopt an MVVM-oriented structure with Views, ViewModels, Models, and Services folders to keep UI, presentation logic, and business logic cleanly separated.
Cricket analogy: Like a franchise's default squad list handed out at the start of the season before the coach organizes players into batting, bowling, and fielding units, a fresh WPF template gives you App.xaml and MainWindow.xaml before you organize the project into Views, ViewModels, and Models folders.
App.xaml and the Application Entry Point
App.xaml declares the Application class and typically sets StartupUri to MainWindow.xaml, which tells WPF which window to open when the app launches; it's also where app-wide resource dictionaries, themes, and global styles are merged so they're available to every window. App.xaml.cs is the code-behind partial class for Application, and overriding OnStartup(StartupEventArgs e) there is the standard place to run startup logic — reading command-line args, setting up dependency injection containers, or choosing which window to show programmatically instead of relying on StartupUri.
Cricket analogy: Like the toss deciding which team bats first before the match proper begins, App.xaml's StartupUri decides which window opens first when a WPF app launches, and OnStartup is where any pre-match setup like team announcements happens.
Organizing Views, ViewModels, and Resources
Most non-trivial WPF codebases group files into Views/ (XAML windows and UserControls), ViewModels/ (classes implementing INotifyPropertyChanged that expose bindable properties and ICommands), Models/ (plain data classes), and Services/ (things like data access or navigation abstracted behind interfaces) — mirroring the MVVM pattern rather than WPF enforcing any specific layout. A common naming convention pairs each view with its view-model by suffix, e.g., CustomerListView.xaml with CustomerListViewModel.cs, which makes it easy to navigate the codebase and keeps the binding relationship between the two obvious even without an explicit file-system link.
Cricket analogy: Like a team's staff being organized into Batting Coach, Bowling Coach, and Analytics units even though no cricket rule mandates that structure, a WPF codebase organizes into Views, ViewModels, and Services folders by convention, not by framework requirement.
Shared styles, colors, and control templates typically live in separate ResourceDictionary XAML files under a Resources or Themes folder — for example Colors.xaml and Styles.xaml — and get pulled into scope by merging them into App.xaml's Application.Resources using a MergedDictionaries collection. Because those merged dictionaries are attached at the application level, any window or control anywhere in the app can reference a resource like {StaticResource PrimaryButtonStyle} without re-declaring it, which is how WPF apps implement consistent theming and, with a bit more work, runtime theme switching by swapping which dictionaries are merged.
Cricket analogy: Like a franchise's kit sponsor supplying one uniform design that every player across the squad wears without each player commissioning their own kit, App.xaml's merged ResourceDictionaries supply one set of styles every window in a WPF app can reference without redefining them.
MyWpfApp/
├── App.xaml
├── App.xaml.cs
├── MyWpfApp.csproj
├── Views/
│ ├── MainWindow.xaml
│ └── CustomerListView.xaml
├── ViewModels/
│ ├── MainViewModel.cs
│ └── CustomerListViewModel.cs
├── Models/
│ └── Customer.cs
├── Services/
│ └── ICustomerService.cs
└── Resources/
├── Colors.xaml
└── Styles.xaml<!-- App.xaml -->
<Application x:Class="MyWpfApp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="Views/MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources/Colors.xaml"/>
<ResourceDictionary Source="Resources/Styles.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>The SDK-style .csproj for a WPF project needs both <UseWPF>true</UseWPF> and a Windows-qualified TargetFramework such as net8.0-windows — omitting the -windows suffix will fail to build since WPF APIs aren't part of the cross-platform .NET base.
Adding New Windows and User Controls
Adding a new top-level window means adding a new Window item (via Visual Studio's Add > Window template), which generates a paired .xaml/.xaml.cs file just like MainWindow; adding a reusable piece of UI meant to be embedded inside other windows means adding a UserControl instead, which is composed the same way but hosted inside a parent rather than shown with .Show() or .ShowDialog(). Both Window and UserControl files need their XAML's x:Class attribute to exactly match the namespace and class name declared in the code-behind partial class, since the XAML compiler uses that attribute to generate the InitializeComponent() wiring.
Cricket analogy: Like adding a new international venue to a cricket board's calendar being a bigger process than adding a new net-practice facility, adding a new top-level Window in WPF is a bigger structural addition than adding a reusable UserControl meant to be embedded elsewhere.
The x:Class value in a XAML file's root element must exactly match the namespace and class name in its code-behind file. A mismatch produces a build-time error or a XamlParseException at runtime because the generated InitializeComponent() call can't be resolved.
- A default WPF project has App.xaml/.cs (the Application entry point) and MainWindow.xaml/.cs (the first window).
- The .csproj needs <UseWPF>true</UseWPF> and a Windows-qualified TargetFramework like net8.0-windows.
- App.xaml's StartupUri picks the first window shown; OnStartup in App.xaml.cs is where startup logic runs.
- WPF doesn't mandate a folder layout, but most teams use Views/, ViewModels/, Models/, and Services/ for MVVM.
- Shared styles and colors live in ResourceDictionary files merged into Application.Resources via MergedDictionaries.
- New top-level windows are added as Window items; reusable embedded UI is added as UserControl items.
- A XAML file's x:Class must exactly match its code-behind's namespace and class name or the build fails.
Practice what you learned
1. Which file typically sets StartupUri to determine the first window a WPF app shows?
2. What must a WPF project's .csproj include to build correctly?
3. Where is the standard place to run WPF application startup logic like reading command-line arguments?
4. How are shared styles typically made available to every window in a WPF app?
5. What must match between a XAML file and its code-behind partial class?
Was this page helpful?
You May Also Like
What Is WPF?
An introduction to WPF, Microsoft's DirectX-based UI framework for building Windows desktop applications with XAML and data binding.
XAML Basics in WPF
The fundamentals of XAML syntax in WPF — elements, attributes, layout panels, markup extensions, and attached properties.
The WPF Application Lifecycle
How a WPF application starts up, runs its message loop via the Dispatcher, and shuts down, including window lifecycle events and ShutdownMode.
Related Reading
Related Study Notes in Microsoft Technologies
Browse all study notesWindows 10 / UWP Development Study Notes
.NET · 30 topics
Microsoft TechnologiesWindows Batch Scripting Study Notes
Batch · 30 topics
Microsoft TechnologiesMFC (Microsoft Foundation Classes) Study Notes
C++ · 30 topics
Microsoft TechnologiesSilverlight Study Notes
.NET · 30 topics
Microsoft TechnologiesXAML Study Notes
.NET · 30 topics
Microsoft TechnologiesMVVM Design Pattern Study Notes
.NET · 30 topics