What would be the appropriate method for view models in a C# WPF project?

fatih uyanık 200 Reputation points
2025-01-22T07:17:10.66+00:00

Hello

I created record insertion and update view models in my c# wpf project. These view models have common save and cancel commands. When the save command is clicked, it calls the save function from the relevant service and if there are any error conditions, it catches them in the view model and prints them.

Since these are common to all of them, would it be a correct approach to create a single basic view model, define the common commands as public, define the error checkers here and use them by inheriting them in the relevant view model?

Can you share the most correct method with me in this regard?

Thank you.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,819 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,244 questions
0 comments No comments
{count} votes

Accepted answer
  1. Hongrui Yu-MSFT 4,200 Reputation points Microsoft Vendor
    2025-01-22T08:35:48.2866667+00:00

    Hi, @fatih uyanık. Welcome to Microsoft Q&A. 

    It is a correct approach to define common commands in the BaseViewModel and inherit them from the ChildViewModel, which could reduce duplicate code and improve the maintainability of the program.

    Here is a simple example(Assuming it is an MVVM project): Install CommunityToolkit.Mvvm via NuGet

    BaseViewModel.cs

        public class BaseViewModel:ObservableObject
        {
            public RelayCommand saveCommand {  get; set; }
    
            public RelayCommand cancelCommand { get; set; }
    
            public BaseViewModel() { 
                saveCommand = new RelayCommand(Save);
                cancelCommand = new RelayCommand(Cancel);
            }
    
            public void Save() {
                //TODO Save related services
                MessageBox.Show("Save Complete");
            }
    
            public void Cancel()
            {
                //TODO Cancel related services
                MessageBox.Show("Cancel Complete");
            }
        }
    

    MainViewModel.cs(Here as a child ViewModel)

        public class MainViewModel:BaseViewModel
        {
            public MainViewModel():base() {
    
            }
        }
    

    MainWindow.xaml

        <Grid>
            <Grid.DataContext>
                <vm:MainViewModel></vm:MainViewModel>
            </Grid.DataContext>
            <Button Content="Save" Command="{Binding saveCommand}" Width="200" Height="50" ></Button>
            <Button Content="Cancel" Command="{Binding cancelCommand}" Width="200" Height="50" Margin="0,200,0,0"></Button>
        </Grid>
    

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.