C# 将 WPF DataGrid 控件数据绑定到 System.Data.DataTable 对象?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1111804/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 08:31:04  来源:igfitidea点击:

Data-Binding a WPF DataGrid Control to a System.Data.DataTable Object?

c#.netwpfdata-bindingxaml

提问by Giffyguy

What do I need to do to get my WPF DataGrid control to bind to a DataTable object?

我需要做什么才能让我的 WPF DataGrid 控件绑定到 DataTable 对象?

I've been suffering over this for several days now. Even when the binding and the query both work correctly, and there is observable data in the table - nothing shows up in the data grid.

我已经为此痛苦了好几天了。即使绑定和查询都正常工作,并且表中有可观察的数据 - 数据网格中也没有显示任何内容。

My XAML code resembles this:

我的 XAML 代码类似于:

<Window x:Class="DataGridTest.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:TK="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit"
    Title="Window1" Height="300" Width="300">
        <Grid>
            <TK:DataGrid ItemsSource="{Binding Path=DTUsers}" />
        </Grid>
</Window>

My C# code-behind resembles this:
(DAO is my Data Access Object)

我的 C# 代码隐藏类似于:
(DAO 是我的数据访问对象)

public partial class Window1 : Window
{
    public dao DAO = new dao (ConnectionString) ;
    public System.Data.DataTable DTUsers { get; set; }

    public Window1()
    {
        InitializeComponent();

        if (DAO != null)
            DTUsers = DAO.Query( @"SELECT * FROM users" );
            // Returns a DataTable object containing user records.
            // I have confirmed that this returns my data correctly.
    }
}

I've checked the output, and there are no binding errors.
Why will this compile and run, but won't display any data???

我检查了输出,没有绑定错误。
为什么这会编译运行,但不会显示任何数据???

(If you don't know where the DataGrid control is, the WPF Toolkit is available here. Install it, add a reference to WPFToolkit.dll [which will show up in the "Add Reference" dialog under ".NET"], and provide the XML namespace declaration [in my XAML above] to use the WPF DataGrid control.)

(如果您不知道 DataGrid 控件在哪里,WPF Toolkit 可在此处获得。安装它,添加对 WPFToolkit.dll 的引用 [它将显示在“.NET”下的“添加引用”对话框中],然后提供 XML 命名空间声明 [在我上面的 XAML 中] 以使用 WPF DataGrid 控件。)

采纳答案by Pavel Minaev

InitializeComponent()will instantiate your markup, including the binding. At that point DTUsersis still null, so nothing is displayed. Then, you change DTUsers, but there's no way for the binding (and therefore the grid) to know that you did it - and so it does not do anything.

InitializeComponent()将实例化您的标记,包括绑定。那时DTUsers是 still null,所以什么都没有显示。然后,您更改了DTUsers,但绑定(以及网格)无法知道您已更改- 因此它不执行任何操作。

You should either make DTUsersa dependency property, or implement INotifyPropertyChangedon your class, and raise PropertyChangedevent after you change the value of the property.

您应该创建DTUsers一个依赖属性,或者INotifyPropertyChanged在您的类上实现,并PropertyChanged在更改属性值后引发事件。