none
WPF中datagrid如何使用? RRS feed

答案

  • 网上的资料很多,这是个很好的入门参考:http://www.wpftutorial.net/DataGrid.html

    当然更多的内容,你应当查看MSDN:http://msdn.microsoft.com/zh-cn/library/system.windows.controls.datagrid%28v=vs.100%29.aspx

    下面是一个简单的例子:

        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition />
                <RowDefinition />
            </Grid.RowDefinitions>
            <DataGrid Grid.Row="0" Name="defaultDataGrid">
            </DataGrid>
            <DataGrid Grid.Row="1" Name="customDataGrid" ItemsSource="{Binding}" AutoGenerateColumns="False">
                <DataGrid.Columns>
                    <DataGridTextColumn Header="ID" Binding="{Binding Path=ID}" />
                    <DataGridTextColumn Header="Title" Binding="{Binding Path=Title}" />
                    <DataGridTextColumn Header="Price" Binding="{Binding Path=Price, StringFormat=N2}" />
                    <DataGridTextColumn Header="Publish Date" Binding="{Binding Path=PublishDate, StringFormat=yyyy/MM/dd}" />
                </DataGrid.Columns>
            </DataGrid>
        </Grid>

            public void InitializeDataContext() {
                List<Book> sources = new List<Book>();
                sources.Add(new Book() { ID = 1, Title="A", Price=49.90, PublishDate = DateTime.Parse("2012-01-01") });
                sources.Add(new Book() { ID = 2, Title = "B", Price = 69.90, PublishDate = DateTime.Parse("2012-05-01") });
                sources.Add(new Book() { ID = 3, Title = "C", Price = 59.90, PublishDate = DateTime.Parse("2011-12-01") });
                defaultDataGrid.ItemsSource = sources;
                customDataGrid.DataContext = sources;
            }
    
        class Book
        {
            public int ID { set; get; }
    
            public string Title { set; get; }
    
            public double Price { set; get; }
    
            public DateTime PublishDate { set; get; }
        }


    Wanpeng wanpeng.ones@gmail.com

    2012年9月21日 2:15