Project with MVVM architecture

Asked

Viewed 117 times

0

I’m having some doubts regarding the MVVM architecture.

I am developing a project that has the following characteristics: - Main form with dynamic content - Main form generates some tabs according to configuration - Each tab generates some panels according to the configuration

There is a Singleton that has a variable that holds a variable of extreme importance.

My project does not use the MVVM concept but I am willing to implement it, or at least to know it better to know how to accomplish, if it is an option, in a project like this.

The changes made in each panel, are sent through event to the responsible tab to be done the Save of the modified configuration (all are independent).
Changes in the tab configuration are sent to the main form that stores the data of the tab configuration in question. Changes in the form are saved by itself.

My code originally looks like this:

    public double OpacityBackground
    {
        get { return btnBackground.Opacity; }
        set
        {
            btnBackground.Opacity = value;
            OnPropertyChanged("OpacityBackground");

            OnPanelChangeData?.Invoke(this, new OnPanelChangeArgs()
            {
                Value = btnBackground.Opacity.ToString(),
                Type = OnChangeType.Opacity
            });
        }
    }

(yes, I am touching btnBackground directly when I could associate a variable to the background, but it is case study).

In it I’m Binding like this:

<Slider 
    Grid.Row="1"
    Margin="5,5,5,5"
    Width="100" 
    Name="sliderOpacity" 
    Minimum="0" 
    Maximum="1" 
    TickFrequency="0.1" 
    IsSnapToTickEnabled="True"
    Value="{Binding OpacityBackground,Mode=TwoWay}"/>

The Onpanelchangedata function warns the tab responsible that some data has been changed. In case of MVVM implementation it would be something like this:

private double opacityBackground;
public double OpacityBackground
{
    get { return opacityBackground; }
    set
    {
        opacityBackground = value;
        OnPropertyChanged();
    }
}

Ok, I associate the datacontext with an instance of the above class but how do I call the related Event to warn the tab? If I wanted to directly access the value of backgroundOpacity, it would be:

MyViewModel viewModel = new MyViewModel();
this.DataContext = viewModel;


// outra parte do código, outra função
viewModel.backgroundOpacity ?

I’m having some questions as my Foms need to communicate all the time, even mainform clicks should have an effect on music, depending on what you do. I’m thinking that the MVVM would not be suitable for my case.

Thank you!

  • If you want to learn in practice to use MVVM with WPF, I would indicate that playlist with excellent videos on the subject.

  • I’ll take a look at that playlist, thanks for the recommendation!

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.