The DataGrid control is a control that displays data, obtains information from a collection of objects, and displays information in a grid with rows and cells. Each row corresponds to a single object, and each column corresponds to an attribute in the object.
The DataGrid control adds many skills that are required to process data in WPF. Its column-based model provides significant formatting flexibility, and its selection model allows you to select a single row, multiple rows, or a combination of cells whose editing support is very powerful, and you can use the DataGrid control as a unified data editor for simple data and complex data.
Described above are introduced, the following I will use the function of the DataGrid in my project to share with you, but also hope that you can point out the shortcomings.
<datagrid x:name="grid1" autogeneratecolumns="True"> </DataGrid>
- DataGrid Data Binding
- Set AutoGenerateColumns to "True" to automatically generate columns and populate datagrid:grid1 by setting the Itemsource property. ItemsSource = prolist; effects such as:
- Using automatically generated columns is not a good way to control the display of the column, set the AutoGenerateColumns to "False", so we can define the display of the column, we usually use the most. Here I bind the columns of the five DataGrid-supported types separately:
<DataGrid.Columns> <datagridcheckboxcolumn header="Select"Width=" -"></DataGridCheckBoxColumn> <datagridtextcolumn header="Product Name"Width=" -"binding="{Binding Path=proname}"> </DataGridTextColumn> <datagridcomboboxcolumn header="Product Category"Width=" the"selecteditembinding="{Binding Path=procategory}"Itemssource="{Binding source={staticresource keycategory}}"></DataGridComboBoxColumn> <!--Combox binding to get the resource keycategory defined above. Get the category's data from the background--&L T;datagridhyperlinkcolumn header="Product Link"Width=" -"binding="{Binding Path=prolink}"></DataGridHyperlinkColumn> <datagridtemplatecolumn header="Product Image"Width=" -"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <image source="{Binding Path=proimage}"Width=" -"height=" -"></Image> </DataTemplate> </datagridtemplatecolumn.celltemplat E> </DataGridTemplateColumn> </DataGrid.Columns>
View CodeBackground settings:
Public enum ProductCategory { travel,deception,tools,general }
View CodeThe interface is as follows:
WPF DataGrid Data Binding