0

I am really new to mvvm and wpf in c# and got stuck at some very basic stuff.In this example I am using Fody.PropertyChanged. I have a basic viewmodel that holds a string called Test which is binded to a textblock.

 public class Model : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged = (sender, e) => { };

        public string Test { get; set; }
    }

Then,in a separate file and class called Data,I have a simple function that increments an int and converts it to a string.

public class Data
    {
        public static int i = 0;
        public static string IncTest { get; set; }
        public static void Inc()
        {
            i++;
            IncTest = i.ToString();
        }
    }

How do I update the Test variable inside the viewmodel when calling the Inc() function? For example, when clicking a button

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = new Model();
        Data.Inc();
    }

    private void Increment_Click(object sender, RoutedEventArgs e)
    {
        Data.Inc();
    }
Catalin
  • 147
  • 1
  • 2
  • 11
  • What value is Test supposed to be set to? Do you want to set Model.Test to Data.IncTest? – Katana Aug 17 '18 at 21:27
  • Possible duplicate of [Binding to static property](https://stackoverflow.com/questions/936304/binding-to-static-property) – Matthew Aug 17 '18 at 21:27
  • @Naman exactly. Whenever I call the Inc() function, I want Model.Test to be set to Data.IncTest – Catalin Aug 17 '18 at 21:29
  • If all you need is to reflect the latest IncTest in your xaml through a binding to Model.Test, you can try changing the Test's get to this get { return Data.IncTest; } – Katana Aug 17 '18 at 21:33
  • @Naman that will only take effect at first run time, after calling the function, Model.Test will not update the UI. – Catalin Aug 17 '18 at 21:38
  • Where and when do you call Data.Inc()? – Katana Aug 17 '18 at 21:42
  • @Naman inside the main method after clicking a button for example. I updated the thread with the code. – Catalin Aug 17 '18 at 21:43
  • Try making test as static and updating it Increment_Click. Refer to Matthew's comment for binding to static properties – Katana Aug 17 '18 at 21:57

1 Answers1

3

In MVVM, the model does not update the view model, its actually opposite, The view model updates the model properties.

Here is an example.

MODEL:

public class Model
{
    public string Test
    {
        get;
        set;
    }
}

View Model:

public class ViewModel : INotifyPropertyChanged
{
    private Model _model;

    public string Test
    {
        get
        {
            return _model.Test;
        }
        set
        {
            if(string.Equals(value, _model.Test, StringComparison.CurrentCulture))
            {
                return;
            }
            _model.Test = value;
            OnPropertyChanged();
        }
    }

    public ViewModel(Model model)
    {
        _model = model;
    }

}

Your views will bind to your view models.

UPDATE: In regards to your question

public class SomeClass
{
    public static void Main(string [] args)
    {
        Model model = new Model();
        ViewModel viewModel = new ViewModel(model);

        //Now setting the viewmodel.Test will update the model property
        viewModel.Test = "This is a test";
    }
}
Adam
  • 224
  • 1
  • 5
  • And how do I update the ViewModel.Test string when calling a function from the main method ? Or outside the viewmodel ? This is what I cant figure out. – Catalin Aug 17 '18 at 21:40
  • Your class will need to create an instance of the view model. Once that is done, you can simply set the property. – Adam Aug 17 '18 at 21:46
  • You mean by doing so ? Model model = new Model(); model.Test = "String should change?"; However, this wont work. Should I make a global instance and then set the datacontext to it ? Will that work ? – Catalin Aug 17 '18 at 21:51
  • Please check the update in my post to see what I mean. – Adam Aug 17 '18 at 21:57