0

In my wpf application, Iam using the MVVM design pattern, and faced a situation where I need to execute a method that exists in the view backend (xaml.cs), from the ViewModel. How could I perform that?

Better if I could avoid breaking MVVM pattern rules

Galilo Galilo
  • 519
  • 5
  • 22
  • Are the data properties bound to elements in the view? – Neil May 15 '19 at 03:14
  • I'm wondering why you need to "reload the board" from the view. Why can't you use viewmodel first to generate a new board. – Andy May 15 '19 at 06:52
  • I need to preserve some values from viewmodel, and i am using navigation service, but i knew now that it is generating a new datacontext for every call to naigationservice.navigate().... – Galilo Galilo May 15 '19 at 07:19
  • 1
    In which case. Isn't the better approach to avoid creating a new viewmodel each time you use your navigationservice? Maybe you should just be using viewmodel first from your mainwindowviewmodel. Impossible to tell from what little you've explained.. – Andy May 15 '19 at 09:39
  • *"I am trying to execute a function in the view, on some data changes in the view model"* -- What does this function do? The view should simply display the state of the viewmodel, updating as the viewmodel raises change notifications. If some logic needs to happen with data from the viewmodel, that likely belongs in a [value converter](https://learn.microsoft.com/en-us/dotnet/api/system.windows.data.ivalueconverter?view=netframework-4.8) or some such thing. We need more detail about exactly what you are trying to do. – 15ee8f99-57ff-4f92-890c-b56153 May 15 '19 at 17:56
  • In my case, I need to change grid columns, rows, and more details based on a specific algorithm, I can't use multiple views. – Galilo Galilo Nov 15 '19 at 06:26

2 Answers2

-1

I agree with Ilian - this does seem like a good use case for MVVM Light's Messenger, or equivalent for whatever MVVM framework you are using, (or none). In a nutshell, we want to implement a mediator pattern to communicate to your view.

I provide a good example and overview on this, and another alternative by using a IViewService that the View provides to its ViewModel..

(DataTriggers would be another possible approach, but your question lacks some details on how your view is constructed, data bound, etc.)

flyte
  • 1,242
  • 11
  • 18
-1

A cheap and easy way to do this is to create a propertydependancy and then bind it to your viewmodel in the xaml

    public static readonly DependencyProperty RefreshViewProperty =
        DependencyProperty.Register("RefreshView", typeof(bool), typeof(MyView), new PropertyMetadata(false, OnRefreshViewChanged));

    private static void OnRefreshViewChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        MyView mv = (MyView)d;

        mv.DoStuff();

        ((MyViewModel)mv.DataContext).RefreshFromViewModel = false;
    }

xaml would be:

<MyView RefreshView="{Binding RefreshFromViewModel}" />
Matthew Sanford
  • 1,069
  • 1
  • 13
  • 21