2015-06-17 30 views
0

我已經創造了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這樣的結合似乎是正確的。

它看起來像處理程序始終爲空。請幫忙!!!!我究竟做錯了什麼???

回答

1

通過Resources屬性可以在後面的代碼中找到您的資源,該屬性是合併的資源字典。您的物品將通過您提供的密鑰提供。假設您的示例代碼用於設置FullName屬性位於您的Window代碼後面,下面的代碼應允許您將值更新到綁定的實例。

List<GET_CONTACT_Result> contactResultList = modsqlContact.GET_CONTACT(contactID); 
var contact = (CheckoutVM)Resources["chkOutViewModel"]; 
contact.FullName = contactResultList[0].cFULLNAME; 
+0

可能沒有爲MVVM做所有最好的方法。但是這個代碼解決了我的問題。 – user3059155

+1

MVVM是一個藥丸,你必須吞下整個。如果您打算進行大量的WPF開發,我會推薦研究並採用主流MVVM工具集之一。 –

+0

您有任何有關使用MVVM工具設置的建議嗎? – user3059155

1

問題是您的checkOutContact與您的控件中使用的CheckoutVM不是同一個CheckoutVM實例。

解決此問題的一種方法是在您的Window的代碼隱藏中設置視圖模型。事情是這樣的:

public CheckoutVM ViewModel 
{ 
    get { return (CheckoutVM) DataContext; } 
    set { DataContext = value; } 
} 

然後你從網格中刪除的DataContext。默認情況下,窗口中的控件將採用與您的窗口相同的DataContext。

<Grid> 
    ... 
    <Label Name="txtContactCheckingOut" Content="{Binding FullName}"/> 
    ... 
</Grid> 

你初始化窗口是這樣的:

YourWindow yourWindow = new YourWindow(); 
yourWindow.ViewModel = new CheckoutVM(); 
yourWindow.ViewModel.FullName = contactResultList[0].cFULLNAME; 

這應該做的伎倆。

+0

您不需要在後面的代碼中設置數據上下文 - 您建議的內容與在Xaml中設置窗口上的datacontext相同。你應該儘可能避免代碼。 – kidshaw

+0

如果我沒有按照建議在代碼背後設置viewmodel,我該如何將我的變量設置爲在XAML中定義的ViewModel的實例。 – user3059155

+0

@kidshaw。我同意你的觀點,在「純粹」的MVVM應用程序中,你不需要這種代碼隱藏功能。但是因爲我不知道他的應用程序是如何完成的,這是一個簡單的解決方案,可以幫助他了解問題所在。 – Absolom

相關問題