Hi,@Kalpana. To achieve data transfer between the two ViewModels , you could establish a communication channel between the two ViewModels. One way to accomplish this is by setting up an intermediary class that acts as a mediator between the ViewModels. Here is an example of how to modify the ViewModel to facilitate data transfer.
View1:
<StackPanel >
<TextBox Name="name" Text="{Binding FullName, UpdateSourceTrigger=PropertyChanged}" />
<Button Content="login" Command="{Binding LoginCommand}" />
</StackPanel>
View2:
<Grid >
<TextBlock Text="{Binding Name,UpdateSourceTrigger=PropertyChanged}" Width="100" Background="AliceBlue" />
</Grid>
Codebedhind:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var viewModel1 = new LoginVM();
var viewModel2 = new LoggedInVM();
DataContext = viewModel1;
var view2 = new View2 { DataContext = viewModel2 };
view2.Show();
}
}
public class LoginVM : ViewModelBase
{
private User user;
public ICommand LoginCommand { get; }
public LoginVM()
{
user = new User();
LoginCommand = new RelayCommand((param) => LoggedIn(param));
}
public event EventHandler<string> FullNameChanged;
public string Password
{
get => user.Password;
set
{
user.Password = value;
OnPropertyChanged(nameof(Password));
}
}
public string FullName
{
get => user.FullName;
set
{
user.FullName = value;
OnPropertyChanged(nameof(FullName));
Mediator.NotifyViewModel1FullNameChanged(value);
}
}
internal void LoggedIn(object parameter)
{
FullName = FullName;
MessageBox.Show($"Logged in successfully as {FullName}");
}
}
public static class Mediator
{
public delegate void ViewModel1FullNameChangedEventHandler(string fullName);
public static event ViewModel1FullNameChangedEventHandler ViewModel1FullNameChanged;
public static void NotifyViewModel1FullNameChanged(string fullName)
{
ViewModel1FullNameChanged?.Invoke(fullName);
}
}
public class LoggedInVM : ViewModelBase
{
private User user=new User();
private readonly LoginVM loginVM;
private string name;
public string Name
{
get => name;
set
{
name = value;
OnPropertyChanged(nameof(Name));
}
}
public LoggedInVM()
{
user = new User();
Mediator.ViewModel1FullNameChanged += UpdateName;
}
private void UpdateName(string fullName)
{
Name = fullName;
}
}
public class User
{
public int Id { get; set; }
public string FullName { get; set; }
public string Password { get; set; }
}
public class ViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
var handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyName));
}
protected virtual void SetValue<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
{
field = value;
OnPropertyChanged(propertyName);
}
}
public class RelayCommand : ICommand
{
private readonly Action<object> _execute;
private readonly Predicate<object> _canExecute;
public RelayCommand(Action<object> execute)
: this(execute, null)
{
}
public RelayCommand(Action<object> execute, Predicate<object> canExecute)
{
if (execute == null)
throw new ArgumentNullException("execute");
_execute = execute;
_canExecute = canExecute;
}
public bool CanExecute(object parameter)
{
return _canExecute == null ? true : _canExecute(parameter);
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public void Execute(object parameter)
{
_execute(parameter);
}
}
The result:
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.