2013-01-22 138 views
0

我已經開始了一個MVVM項目,現在我正在使用正確的DataBinding。 我的項目有:MVVM數據綁定

一個用戶控件白衣一個ViewModel作爲DataContext的,如:

public partial class TestUserControl: UserControl 
{ 
    public TestUserControl() 
    { 
     this.DataContext = new TestUserControlViewModel(); 
    } 
} 

視圖模型代碼是(BaseViewModel類包含PropertyChangedEventHandler):

public class TestUserControlViewModel : BaseViewModel 
{ 
    public KrankenkasseControlViewModel() 
    {} 

    public IEnumerable<DataItem> GetAllData 
    { 
     get 
     { 
      IGetTheData src= new DataRepository(); 
      return src.GetData(); 
     } 
    } 
} 

IGetTheData是接口的DataContext:

public interface IGetTheData 
{ 
    IEnumerable<DataItem> GetData(); 
} 

}

終於DataRepository代碼:

public class DataRepository : IGetTheData 
{ 
    private TestProjectDataContext dax = new TestProjectDataContext(); 

    public IEnumerable<DataItem> GetData() 
    { 
     return (from d in this.dax.TestData 
       select new DataItem 
       { 
        ID = d.ID, 
        SomeOtherData = d.SomeOtherData 
       }); 
    } 
} 

我的用戶有幾個文本框,但什麼是正確結合的最佳方式?

感謝您的幫助,問候。

+0

嗨,謝謝你的所有答案。也許我寫的太少了,上面的一些代碼對我的建議是錯誤的,但我有幾個文本框應該從數據庫中填充,用戶應該能夠鍵入一些文本,這些文本將被保存回數據庫,因此使用listbox/listview是對我無用。我以爲它能夠從數據庫中檢索所有項目到DataItem,然後將每個屬性綁定到文本框。但仍然不知道它是如何工作的。此致 – nukleos

+0

你應該更新你的問題,以更好地反映你的真實需求。一定要記住,在未來其他有同等問題的人會很樂意找到你的問題和答案;這隻有在問題得到妥善解釋的情況下才有效。 – Desty

+0

我編輯了我的答案,但不管這是否是您想知道的,編輯您的問題以便將來更好地理解! :) – Desty

回答

3

編輯:對多個文本框

綁定數據讀取您的評論後,我會闡述我的文本框的例子。

第一件重要的事情是ViewModel將對View中的事物建模,以便View獲取它需要的所有所需的信息。這意味着,如果在視圖中有多個文本樹,則需要在ViewModel中使用多個字符串屬性,每個文本框一個。

在XAML中,你可以有類似

<TextBox Text="{Binding ID, Mode=TwoWay}" /> 
<TextBox Text="{Binding SomeOtherData, Mode=TwoWay}" /> 

,並在您的視圖模型

public class TestUserControlViewModel : BaseViewModel { 
    private string id; 
    private string someOtherData; 

    public TestUserControlViewModel() { 
     DataItem firstItem = new DataRepository().GetData().First(); 
     this.ID = firstItem.ID; 
     this.SomeOtherData = firstItem.SomeOtherData; 
    } 

    public string ID { 
     get { 
      return this.id; 
     } 
     set { 
      if (this.id == value) return; 
      this.id = value; 
      this.OnPropertyChangedEvent("ID"); 
     } 
    } 

    public string SomeOtherData { 
     get { 
      return this.someOtherData; 
     } 
     set { 
      if (this.someOtherData == value) return; 
      this.someOtherData = value; 
      this.OnPropertyChangedEvent("SomeOtherData"); 
     } 
    } 
} 

這裏我假設你BaseViewModel有一個OnPropertyChangedEvent方法觸發相應的事件。這告訴View該屬性已經改變並且它必須更新自己。

請注意XAML中的Mode=TwoWay。這意味着,價值變化的哪一方並不重要,另一方將立即反映變化。因此,如果用戶更改TwoWay綁定的TextBox中的值,則相應的ViewModel屬性將自動更改!反之亦然:如果以編程方式更改ViewModel屬性,則視圖將刷新。

如果要爲多個數據項顯示多個文本框,則必須在ViewModel中引入更多的屬性並相應地綁定它們。也許ListBox與靈活的TextBox es裏面是一個解決方案,然後,就像@Haspemulator已經回答。

綁定數據對集合控制

TestUserControl我猜你有一個控制(如ListView),顯示加載的事情的清單。因此針對視圖模型的列表控件綁定與

<ListView ... ItemsSource="{Binding GetAllData}" ... /> 

首先,你必須明白,綁定的含義不是「讀取數據,然後忘記視圖模型」。相反,只要視圖持續,您就可以將視圖綁定到ViewModel(及其屬性)。從這個角度來看,AllDataGetAllData好得多(感謝@Malcolm O'Hare)。

現在,在您的代碼中,每次View讀取AllData屬性時,都會創建一個新的DataRepository。由於綁定,這不是您想要的,而是您希望在View的整個生命週期中都有一個DataRepository的實例,該實例用於讀取初始數據,稍後可用於更新View,如果基礎數據庫的變化(可能是一個事件)。

要啓用這種行爲,您應該將AllData屬性的類型更改爲ObservableCollection,以便在發生更改時View可以自動更新列表。

public class TestUserControlViewModel : BaseViewModel 
    private ObservableCollection<DataItem> allData; 

    public TestUserControlViewModel() { 
     IGetTheData src = new DataRepository(); 
     this.allData = new ObservableCollection<DataItem>(src.GetData()); 
    } 

    public ObservableCollection<DataItem> AllData { 
     get { 
      return this.allData; 
     } 
    } 

    public void AddDataItem(DataItem item) { 
     this.allData.Add(item); 
    } 
} 

現在,如果您稍後調用AddDataItem,ListView將自動更新自身。

2

您的房源名稱不好。你應該把它叫做AllData,而不是GetAllData。

既然你正在返回一個集合,你可能應該使用某種列表控件(ListBox,ListView)。

在這種情況下,你會做

<ListBox ItemsSource="{Binding GetAllData}" /> 
+1

+1作爲改進提及'AllData' – Desty

+0

@Desty隨意編輯到您的解決方案 –

0

Guten Abend。 :)如前所述,由於您要返回集合,因此最好使用ListBox。有關將ObservableCollection作爲緩存的評論也是絕對有效的。我想補充一點,如果你需要有你的數據編輯,你應該使用文本框ItemTemplate模板裏面:

<ListBox.ItemTemplate> 
    <DataTemplate> 
     <TextBox Text={Binding SomeOtherData,Mode=TwoWay} /> 
    </DataTemplate> 
</ListBox.ItemTemplate> 

在這種情況下,如果用戶編輯框中的文本,數據將在您的數據對象的更新,所以它可以稍後保存在數據庫中。