c# - 为 Window1 中 ViewModel 的 TextBlock.Text 赋值,并将 TextBlock.Text 的值用于 Window2
问题描述
我遇到了 MVVM 和 WPF 应用程序的一个特殊问题。我将简要解释一下,以使其尽可能清楚。
首先,我在 XAML 文件中绑定了 TextBlock 属性的文本
XAML
<TextBlock
x:Name="ProjectCredentials"
Text="{Binding Path=HeaderName}"/>
查看模型 - 在 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
}
下面是我想为我的 TextBlock.Text 属性赋予价值的LoginScreen 窗口或窗口 1。
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
}
}
}
现在我已经给出了值,"Project Name: ..."
我应该在方法内的 MainWindow 中调用这个字符串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; }
}
这里的问题是如何调用已经从 LoginScreen 窗口填充的 MainWindow DataContext 的 HeaderName?
我知道,如果我在 GetHeaderDetails() 方法中打开 MainWindow DataContext 的一个新实例,我将得到一个 NullReferenceException,因为 HeaderName 将丢失并且将引入一个新的 DataContext。所以我想找到一种方法来绕过这个。
请注意,我知道NullReferenceException。虽然,我正在寻找可以帮助我理解如何定位问题的答案。
问候。
解决方案
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.