2012-12-15 36 views
1

我有基於MVVM的WPF項目。在我看來,我的下一個列表框將數據集綁定到僅顯示1條記錄的列表框

<ListBox BorderBrush="#6797c8" BorderThickness="2" 
    ItemsSource="{Binding Path=CategoriesDS}" 
    DisplayMemberPath="MainCategories/Category"/> 

這是我的ViewModel代碼:

private DataSet categoriesDS; 

public DataSet CategoriesDS 
{ 
    get 
    { 
     if (categoriesDS == null) 
     { 
      categoriesDS = _dal.GetCategoriesTables(); 
     } 
     return categoriesDS; 
    } 
    set 
    { 
     categoriesDS = value; 
     if (this.PropertyChanged != null) 
     { 
      this.PropertyChanged(this, 
        new PropertyChangedEventArgs("CategoriesDS")); 
     } 
    } 
} 

我DataSet包含2個表和第一個表(「MainCategories」)包含3行。 當我運行我的應用程序時,我只看到「MainCategories」表的第一行。

爲什麼ListBox只顯示1行?我想要顯示整個表格。

謝謝

+0

嘗試返回一個表。不是數據集。 – Paparazzi

回答

1

您需要直接綁定到表。您可以創建只訪問CategoriesDS財產另一屬性,然後結合對新的屬性:

public DataView MainCategories 
{ 
    get { return CategoriesDS.MainCategories.DefaultView; } 
} 

public DataView MainCategories 
{ 
    get { return CategoriesDS.Tables[0].DefaultView; } 
} 

XAML

<ListBox BorderBrush="#6797c8" BorderThickness="2" 
    ItemsSource="{Binding Path=MainCategories}" 
    DisplayMemberPath="Category"/> 
相關問題