1

我有基于 MVVM 的 WPF 项目。在我看来,我有下一个 ListBox

<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"));
        }
    }
}

我的数据集包含 2 个表,第一个表(“MainCategories”)包含 3 行。当我运行我的应用程序时,我只看到“MainCategories”表的第一行。

为什么 ListBox 只显示 1 行?我想显示整个表格。

谢谢

4

1 回答 1

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"/>
于 2012-12-15T20:50:20.077 回答