Project with MVVM architecture

0

I'm having some doubts about the MVVM architecture.

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

There is a singleton that has a variable that holds an extremely important variable.

My project does not use the MVVM concept, but I'm trying to implement it, or at least get to know it better, so that I know how to do it, if it's an option, in a project like that.

The changes made in each panel are sent via event to the responsible tab so that the modified configuration's Save is made (all are independent). Changes to the tab configuration are sent to the main form that stores the tab configuration data in question. Form changes 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'm moving directly to the btnBackground when I could associate a variable with the background, but it's 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 responsible tab that some data has changed. In case of MVVM implementation it would look 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 want to directly access the backgroundOpacity value, it would be:

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


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

I have some doubts as to why my forms need to communicate all the time, even mainform clicks should have an effect on music, depending on what is done. I'm thinking that MVVM would not be suitable for my case.

Thank you!

    
asked by anonymous 20.09.2018 / 19:43

0 answers