Click here to Skip to main content
15,880,469 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Good morning companions. I would like to know how I can modify the DataContext of a view with its ViewModel, from another ViewModel or from the same ViewModel.

The program is a test to be used later in another project. Basically they are two UserControl inside a window, each with its respective ViewModel. Implement the INotifyPropertyChanged and ICommand interfaces with NotifyBase and RelayCommand.

In view1

<Button Grid.Column="0" Margin="30" Height="120"    CommandParameter="Button1ViewModelIzdo" Command="{Binding Btn1Command}">


In ViewModel1:

public class Window1ViewModel : NotifyBase
{
 public delegate void EventHandler(object sender, CustomEventArgs e);
 public event EventHandler ThrowEvent;// = delegate { };

 private ICommand _btn1Command;
 public ICommand Btn1Command
 {
     get
     {
         return _btn1Command ?? (_btn1Command = new  RelayCommand((parameter) => Bnt1Action(parameter)));
     }
 }

 private void Bnt1Action(object parameter)
 {
     String msg = parameter as String;
     ThrowEvent?.Invoke(this, new CustomEventArgs(msg));
 }


In View2

<TextBlock TextAlignment="Center" x:Name="Data" Text="{Binding   Path=Data,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"></TextBlock


In ViewModel2

public class Window2ViewModel : NotifyBase
{
    private Window1ViewModel _Thrower;

    public Window2ViewModel()
    {
        _Thrower = new Window1ViewModel();
        _Thrower.ThrowEvent += ( emisor, e) => { ChangeData(emisor , e);      };
    }

    private string _data;

    public string Data
    {
        get { return _data; }
        set{
                if (_data != value)
                {
                    _data = value;
                    OnPropertyChanged("Data");
                }
            }
     }

    private void ChangeData(object emisor, CustomEventArgs e)
    {
        Data = e.msg;
        MessageBox.Show("Hell0");
    }
}   


The CustomEventArgs class has a msg propedad. But the event does not launch. What am I doing wrong ?

Many thanks in advance. Greetings César.

What I have tried:

Use delegates and events Search for examples in stackoverflow and codeproject
Posted
Comments
Dirk Bahle 8-Sep-18 4:54am    
Hi I am not sure what your exact question is but you should research behaviors https://stackoverflow.com/questions/8360209/how-to-add-system-windows-interactivity-to-project

or 'attached behaviors' if you want to propagate events from a WPF control to the viewmodel using command binding (if you cannot change the source code of the control)
cesar.iriso 8-Sep-18 4:58am    
Thans Dirk. I'm reading a https://stackoverflow.com/questions/1939414/wpf-should-a-user-control-be-supplied-with-a-viewmodel?rq=1 .
It seems that it is not a good idea to associate ViewModels with UserControl
Greeting César.
Dirk Bahle 8-Sep-18 10:50am    
An association between viewmodel and usercontrol via datacontext is in principal OK - but there are technical reasons why events/commands should be with their particular patterns ...
Dirk Bahle 8-Sep-18 10:57am    
Not sure if it helps, but it might be something to consider.
Here is an example of a control that will watch the DataContextChanged event and attach itself to a viewmodel that implements the IEditBox interface:
https://github.com/Dirkster99/InplaceEditBoxLib/blob/master/source/InplaceEditBoxLib/Views/EditBox.xaml.cs

Doing so allows the viewmodel to bubble notifications through the control. This means the viewmodel does not have to worry about the UI part of the notification - its worring about the required event only ...
cesar.iriso 8-Sep-18 15:38pm    
Too complex for me, I'm a little newbie :)
Thank you anyway.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900