我已經創造了INotifyPropertyChanged的這個基類...無法獲取數據綁定工作(我看了很多帖子,並不能找出我在做什麼錯)
namespace BASECLASSES.HelperClasses
{
public class NotifyPropChangedBase : INotifyPropertyChanged
{
/// <summary>
/// The propertyChanged Event to raise to any UI object
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// The PropertyChanged Event to raise to any UI object
/// The event is only invoked if data binding is used
/// </summary>
/// <param name="propertyName"></param>
protected void RaisePropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = this.PropertyChanged;
if (handler != null)
{
PropertyChangedEventArgs args = new PropertyChangedEventArgs(propertyName);
handler(this, args);
}
}
}
}
我已經創建了實現此基類的一個視圖模型...
public class CheckoutVM : BASECLASSES.HelperClasses.NotifyPropChangedBase
{
private string fullName ;
public string FullName
{
get { return fullName; }
set
{
if (fullName != value)
{
fullName = value;
RaisePropertyChanged("FullName");
}
}
}
}
}
}
在XAML我定義爲類命名空間...
xmlns:bcbcns="clr-Namespace:BASECLASSES.HelperClasses;assembly=BASECLASSES"
我已經定義了一個窗口資源...
<Window.Resources>
<m:CheckoutVM x:Key="chkOutViewModel" />
</Window.Resources>
設置的DataContext到主電網...
<Grid DataContext="{Binding Source={StaticResource chkOutViewModel}}">
將標籤內容的路徑設置爲...
<Label Name="txtContactCheckingOut" Content="{Binding Path=FullName}"/>
接下來我用此代碼設置標籤...
List<GET_CONTACT_Result> contactResultList = modsqlContact.GET_CONTACT(contactID);
CheckoutVM checkOutContact = new CheckoutVM();
checkOutContact.FullName = contactResultList[0].cFULLNAME;
但是標籤沒有設置。
如果我添加一個構造函數來視圖模型這樣的....
public CheckoutVM()
{
FullName = "XXXXXXXXXXXXXXXXX";
}
的標籤設置XXXXXXXXXXXXXXXXX這樣的結合似乎是正確的。
它看起來像處理程序始終爲空。請幫忙!!!!我究竟做錯了什麼???
可能沒有爲MVVM做所有最好的方法。但是這個代碼解決了我的問題。 – user3059155
MVVM是一個藥丸,你必須吞下整個。如果您打算進行大量的WPF開發,我會推薦研究並採用主流MVVM工具集之一。 –
您有任何有關使用MVVM工具設置的建議嗎? – user3059155