I'm working in a WPF project and a newer on this, I'm using the MVVM patter in my project.
I have a MainWindows which calls User Controls and I have a MainWindowsModel which helps me to Create an instance, save them in a variable for opening in my MainWindows...
My problems comes here, when I use the menu of my window (look the photos below) the User Control is refresing like creating a new instance...
I create a repository where i simulate my problem, try to write in the textBox and then click on other bottom Menu, Then return to the before Button you won't be able to watch the message you wrote there.
Hope some helps me with this because i have no idea how to manage User Control just creating one Instance and don't save data when i change between UserControls.
Look at the image if I didn't explain well
The repo for this question with interface in the photo: Click on here to open the Repo
I was looking for examples on other post but i didn't fine a way to make my information stays on my UserControl. I will be active for this question...
CodePudding user response:
The information is not supposed to "stay in the UserControl
.
You should bind the TextBox
to a property of the view model and store the string
value there. It won't/shouldn't be stored in the view that gets unloaded.
HomeView.xaml:
<TextBox Text="{Binding Text}"
FontSize="20"
Margin="10,10,0,0"
Foreground="Green"
Width="300"
Height="50"/>
HomeViewModel:
public class HomeViewModel : ViewModelBase
{
private string _text = "Write something";
public string Text
{
get { return _text; }
set { _text = value; OnPropertyChanged(nameof(Text)); }
}
}