Hi,@saurabh alliumsepa . To achieve navigation between two views, each bound to its own ViewModel, and ensure that data changes are retained when switching between views, you could use a Frame element in the main window and navigate between the views within that frame. Here's a simple example using the MVVM pattern:
<Window.Resources>
<DataTemplate DataType="{x:Type local:View1ViewModel}">
<local:View1/>
</DataTemplate>
<DataTemplate DataType="{x:Type local:View2ViewModel}">
<local:View2/>
</DataTemplate>
</Window.Resources>
<Window.DataContext>
<local:MainWindowViewModel />
</Window.DataContext>
<StackPanel>
<StackPanel Orientation="Horizontal" Height="30" VerticalAlignment="Top">
<Button Content="View 1" Command="{Binding GotoView1Command}" />
<Button Content="View 2" Command="{Binding GotoView2Command}" />
</StackPanel>
<Frame Content="{Binding CurrentView}" />
</StackPanel>
View1:
<UserControl.DataContext>
<local:View1ViewModel/>
</UserControl.DataContext>
<StackPanel>
<TextBlock Text="Number1" Margin="5" />
<TextBox Text="{Binding Text1,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Margin="5" />
<TextBlock Text="Number2" Margin="5" />
<TextBox Text="{Binding Text2,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Margin="5" />
<TextBlock Text="Addition of Number1 and Number2" Margin="5" />
<TextBox Text="{Binding Sum,Mode=OneWay,UpdateSourceTrigger=PropertyChanged}" Margin="5" />
<Button Content="Increment Text1" Command="{Binding IncreaseMyText1Command}" Margin="5" />
<Button Content="Increment Text2" Command="{Binding IncreaseMyText2Command}" Margin="5" />
<TextBox Margin="5" />
</StackPanel>
View1.cs:
public partial class View1: UserControl
{
public View1()
{
InitializeComponent();
}
}
public class View1ViewModel : ViewModelBase
{
#region Text1
private int MyText1 = 30;
public int Text1
{
get
{
return MyText1;
}
set
{
MyText1 = value;
OnPropertyChanged(nameof(Text1));
OnPropertyChanged(nameof(Sum));
}
}
#endregion
#region Text2
private int MyText2 = 40;
public int Text2
{
get
{
return MyText2;
}
set
{
MyText2 = value;
OnPropertyChanged(nameof(Text2));
OnPropertyChanged(nameof(Sum));
}
}
#endregion
#region Sum
//int _sum = 0;
public int Sum
{
get
{
try
{
return Text1+ Text2;
}
catch (FormatException)
{
return 0;
}
}
}
#endregion
private ICommand _increaseMyText1Command;
private ICommand _increaseMyText2Command;
public ICommand IncreaseMyText1Command
{
get
{
return _increaseMyText1Command ?? (_increaseMyText1Command = new RelayCommand(
x =>
{
IncreaseMyText1();
}));
}
}
public ICommand IncreaseMyText2Command
{
get
{
return _increaseMyText2Command ?? (_increaseMyText2Command = new RelayCommand(
x =>
{
IncreaseMyText2();
}));
}
}
private ICommand _navigateToView2Command;
public View1ViewModel()
{
}
private void IncreaseMyText1( )
{
Text1++;
}
private void IncreaseMyText2()
{
Text2++;
}
}
View2:
<Grid>
<TextBox Name="tb" Text="hello view2" />
<!--<Button
Content="Goto View 1" Command="{Binding GotoView1Command}"
HorizontalAlignment="Center" Margin="10,10,0,0"
VerticalAlignment="Center" Width="75"></Button>-->
</Grid>
The restul:
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.