WPF Devide .xaml.cs in several files

PK_112

New member
Joined
Feb 11, 2025
Messages
4
Programming Experience
5-10
Hello everyone,

I have many "void" functions with parameters in my MainWindow.xaml.cs, which I want to split into several files, depending on their functionality.

Do I use "classes" or only "partial classes" of MainWindow for this? Or should I subdivide my MainWindow.xaml.cs with comments?
I also want to create "void "s with transfer and return parameters for the calculation of the corner radius of buttons, because I don't get along with the Ivalueconverter...

I can send my code later, I'm still on the road at the moment…

Many thanks in advance, Frank
 
Easiest would be partial classes since the default code that you have in MainWindow.xaml.cs is already a partial class. Just create another file with a different name, but within the file declare the same partial class.

Another alternative is to make use of #region so that you can have areas within the same file that you can collapse and expand.

In general, if you find that a single class is growing bigger and bigger, it is a code smell that you might not be following the S in SOLID object oriented programming practices.

If you are using WPF the way it was meant to be used, your MainWindow.xaml.cs should be very small, and your MainWindowViewModel.cs should be the thing that pretty big, and MainWindowModel.cs would be small to medium sized.
 
Back
Top Bottom