I am running into a peculiar problem with MVVM and WPF application. I will explain it briefly to make it as clear as possible.
First of all I have binded the text of a TextBlock property in the XAML file
XAML
<TextBlock
x:Name="ProjectCredentials"
Text="{Binding Path=HeaderName}"/>
View Model - created in MainWindow
namespace Test
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
public class MainWindowViewModel : INotifyPropertyChanged
{
private string _tabitemHeader;
public string HeaderName
{
get
{
return _tabitemHeader;
}
set
{
_tabitemHeader = value;
OnPropertyChanged("HeaderName");
}
}
}
// Continue in next code block
}
Below is the LoginScreen Window or Window 1 from where I want to give value to my TextBlock.Text property.
namespace Test
{
public partial class LoginScreen : Window
{
public LoginScreen()
{
InitializeComponent();
}
private void LoginButton_Click(object sender, RoutedEventArgs e)
{
MainWindow win2 = new MainWindow();
win2.DataContext = new MainWindow.MainWindowViewModel();
MainWindow.MainWindowViewModel object_change = win2.DataContext as MainWindow.MainWindowViewModel;
object_change.HeaderName = "Project Name: " + SQLSeverName.Text + " - " + SQLDatabasesComboBox.SelectedItem;
Debug.WriteLine(object_change.HeaderName);
//Successfully returns the string
}
}
}
Now that I have given the value "Project Name: ..."
I should call this string in MainWindow inside the method GetHeaderDetails()
namespace Test
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
public class MainWindowViewModel : INotifyPropertyChanged
{
private string _tabitemHeader;
public string HeaderName
{
get
{
return _tabitemHeader;
}
set
{
_tabitemHeader = value;
OnPropertyChanged("HeaderName");
}
}
}
public List<string> GetHeaderDetails()
{
string projectdetails_option1 = ?.HeaderName; //how can I call the HeaderName of MainWindow DataContext which is already filled from LoginScreen window?
Debug.Assert(projectdetails_option1 != null, "Project name not found!"); //this returns null
string connectiondetails = projectdetails_option1.Split(": ")[1];
string servernamedetails = connectiondetails.Split(" - ")[0].Trim();
string projectnamedetails = connectiondetails.Split(" - ")[1].Trim();
List<string> connectiondetailslist = new List<string>();
connectiondetailslist.Add(servernamedetails);
connectiondetailslist.Add(projectnamedetails);
return connectiondetailslist;
}
public List<string> ConnectionDetailsList { get => GetHeaderDetails(); set => ConnectionDetailsList = value; }
}
The problem here is how can I call the HeaderName of MainWindow DataContext which is already filled from the LoginScreen window?
I know that if I open a new instance of the MainWindow DataContext in the GetHeaderDetails() method I will get a NullReferenceException because the HeaderName will be lost and a new DataContext will be introduced. So I want to find a way to bypass this.
Please note that I am aware of the NullReferenceException . Although, I am looking for answers that may help me to understand how to locate the problem.
Regards.
I have figured it out, finally. To retrieve the value of the TextBlock.Text value in MainWindow, the only thing that I had to do is:
Instead of this:
string projectdetails_option1 = ?.HeaderName;
I tried this:
string projectdetails_option1 = ProjectCredentials.Text; //The TextBlock that got its value from LoginScreen.
And successfully I retrieved the value of the ProjectCredentials textblock, without opening a new instance of the DataContext.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.