WPF 中的数据网格绑定 [英] Datagrid binding in WPF

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

问题描述

我知道这已经被问到了,但我已经完成了开发人员建议的几乎所有事情.

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?

推荐答案

请不要使用 object 作为类名:

PLEASE do not use object as a class name:

public class MyObject //better to choose an appropriate name
{
    string id;
    DateTime date;
    public string ID
    {
       get { return id; }
       set { id = value; }
    }
    public DateTime Date
    {
       get { return date; }
       set { date = value; }
    }
}

您应该为此类实现INotifyPropertyChanged,当然在属性设置器上调用它.否则更改不会反映在您的用户界面中.

You should implement INotifyPropertyChanged for this class and of course call it on the Property setter. Otherwise changes are not reflected in your ui.

您的Viewmodel 类/对话框类应该具有MyObject 列表的 Property.ObservableCollection 是要走的路:

Your Viewmodel class/ dialogbox class should have a Property of your MyObject list. ObservableCollection<MyObject> is the way to go:

public ObservableCollection<MyObject> MyList
{
     get...
     set...
}

在您的 xaml 中,您应该将 Itemssource 设置为您的 MyObject 集合.(Datacontext 必须是你的对话框类!)

In your xaml you should set the Itemssource to your collection of MyObject. (the Datacontext have to be your dialogbox class!)

<DataGrid ItemsSource="{Binding Source=MyList}"  AutoGenerateColumns="False">
   <DataGrid.Columns>                
     <DataGridTextColumn Header="ID" Binding="{Binding ID}"/>
     <DataGridTextColumn Header="Date" Binding="{Binding Date}"/>
   </DataGrid.Columns>
</DataGrid>

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

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