0

我想在我的 UI 中显示一个数据网格。我将它绑定到我的视图模型中的数据集。这是代码:

看法:

        <StackPanel Orientation="Horizontal" Height="Auto" Name="stackPanel4" Width="Auto" Grid.Row="4">
            <DataGrid Name="QueryGrid" AutoGenerateColumns="True" Height="1000" Width="1000" ItemsSource="{Binding QueryTable}" Visibility="{Binding Path=QueryGridVisiblity, Converter={StaticResource BoolToVis}}" />
        </StackPanel>

视图模型:

    private void OnRunQuery()
    {
        int count = 0;
        DataSet queryDataset = null;
        if (flag1 == true)
            count++;
        else if (flag2 == true)
            count++;
        else if (flag3 == true)
            count++;
        else if (flag4 == true)
            count++;
        else if (flag5 == true)
            count++;
        else if (flag6 == true)
            count++;
        if (paramCount > 0 && sqlQuery != null && paramCount == count)
        {
            queryDataset = _service.GetQueryDataSource(sqlQuery);
            m_QueryTable = queryDataset;
            OnPropertyChanged("QueryTable");
            m_Visibility = true;
            OnPropertyChanged("QueryGridVisiblity");
        }
    }
    private DataSet m_QueryTable;

    public DataSet QueryTable
    {
        get
        {
            return m_QueryTable;
        }
        set
        {
            m_QueryTable = value;
            OnPropertyChanged("QueryTable");
        }
    }

在执行应用程序时,不会填充数据网格。但我在我的数据集中得到数据。

有什么建议我缺少逻辑吗?我是 WPF 的新手。

4

3 回答 3

2

You'll need to bind to a property on your view model that returns a DataView. Have a look at http://www.codeproject.com/Articles/30905/WPF-DataGrid-Practical-Examples for more information.

E.g:

public DataView Items
{
    get
    {
        return m_QueryTable.Tables[0].DefaultView;
    } 
}

Alternatively (and preferably), you could use entities and a repository pattern to abstract the data access logic away from the view models.

于 2013-06-18T22:12:26.077 回答
1

使用不带引号的表名

例如:

ItemsSource="{Binding ProductsDataSet.Tables[Products]}"

于 2014-05-24T14:53:48.197 回答
1

ADataSet是一组DataTables 并且 aDataGrid只显示一个DataTable。所以公开一个DataTableProperty 而不是DataSet.

于 2013-06-18T23:53:19.027 回答