我有以下問題,我不知道爲什麼我的DataBinding不起作用(我已經在我的程序中以同樣的方式做了一個,它的工作原理)。也許它不起作用,因爲我傳遞了MainWindow並轉到Window2,但是我的引用僅在Window2上爲MainWindow設置。不知道如何解決它。我的代碼(很抱歉不得不削減它,我希望我沒有忘記任何事,因爲兩個窗口超過600或700行代碼)2個WPF Windows與2個文本框之間的數據綁定
private void btLogin_Click(object sender, RoutedEventArgs e)
{
var Window2 = new Window2();
Login USERTB= new Login(); //reference to reach the Login table and check if we can read the logged user from it
Window2.Show();
USERTB.Username = tbLoginUser.Text; //checking if it knows the username- working yet
this.Hide();
}
在此之後,問題開始,我的文本框的從主窗口沒價值即使我試圖用USER參考來獲取它,也不會傳遞給第二個。這裏是Window2.xaml & Window2.cs:
Window2.cs:
string action;
public string Action
{
get { return this.action; }
set { this.action= value; OnPropertyChanged("Action"); }
}
private void SmthHappens(object sender, DependencyPropertyChangedEventArgs e)
{
var felh = new MainWindow();
Action= felh.USER; //in here didn't pass the MW's tb value ,so here is the problem
tbShow.Text = Action;
}
我嘗試通過MW的TB值W2 tbShow。我想我的代碼在SmthHappens上是錯誤的,因爲我已經通過了MW,並且失去了tbLoginUser的價值。但我不知道如何解決它。請幫忙!
已解決!感謝你的回答,我以同樣的方式解決了這個問題。也許我會稍後檢查MVVM,但現在我沒有太多的離開我的程序,所以我結束它:)
THX的答案!解決它沒有MVVM或單身。但我會檢查它:) – Tom026 2014-10-11 06:02:39