在WPF中Datagrid绑定 [英] Datagrid binding in WPF

查看:833
本文介绍了在WPF中Datagrid绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这已经被问到了,但我几乎完成了开发人员的建议。

I know this has been asked already but I have done almost everything what is suggested by developers.

<DataGrid x:Name="Imported" VerticalAlignment="Top" DataContext="{Binding Source=list}"  AutoGenerateColumns="False" CanUserResizeColumns="True">
    <DataGrid.Columns>                
        <DataGridTextColumn Header="ID" Binding="{Binding Path=ID}"/>
        <DataGridTextColumn Header="Date" Binding="{Binding Path=Date}"/>
    </DataGrid.Columns>
</DataGrid>

我试图在模态对话框中显示这个,并在模态的构造​​函数中填充许可证列表对话框。
但是datagrid内还没有任何内容。

I am trying to show this in modal dialog box and populating the license list in the constructor of the modal dialog box. But still nothing is getting populated inside the datagrid.

构造方法代码:

public diagboxclass()
{
    List<object> list = new List<object>();
    list = GetObjectList();
}

public class object
{
    string id;
    DateTime date;
    public string ID
    {
        get { return id; }
        set { id = value; }
    }
    public DateTime Date
    {
        get { return date; }
        set { date = value; }
    }
}

你们是否认为与对象有任何关系列表?

Do you guys think something to do with the object list?

推荐答案

没有看到对象列表,我相信你应该绑定到DataGrid的 ItemsSource 属性,而不是它的 DataContext

Without seeing said object list, I believe you should be binding to the DataGrid's ItemsSource property, not its DataContext.

<DataGrid x:Name="Imported" VerticalAlignment="Top" ItemsSource="{Binding Source=list}"  AutoGenerateColumns="False" CanUserResizeColumns="True">
    <DataGrid.Columns>                
        <DataGridTextColumn Header="ID" Binding="{Binding ID}"/>
        <DataGridTextColumn Header="Date" Binding="{Binding Date}"/>
   </DataGrid.Columns>
</DataGrid>

(假设包含DataGrid的元素[UserControl等]具有

ItemsSource 属性中定义其绑定其行的集合,因此,如果列表不是一个绑定到控件DataContext的对象,您可能需要同时设置 DataContext = {Binding list} ItemsSource = {Binding list} 在DataGrid ...)

(This assumes that the element [UserControl, etc.] that contains the DataGrid has its DataContext bound to an object that contains the list collection. The DataGrid is derived from ItemsControl, which relies on its ItemsSource property to define the collection it binds its rows to. Hence, if list isn't a property of an object bound to your control's DataContext, you might need to set both DataContext={Binding list} and ItemsSource={Binding list} on the DataGrid...)

这篇关于在WPF中Datagrid绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆