WPF DataGrid binding and column centering

Source: Internet
Author: User
Tags wpf datagrid

Basic Data binding

Binds a collection's field (property) to the DataGrid's binding property to bind the data-bound list

1     Public classCashitem {2         Public intValue {Get;Set; }3         Public intCount {Get;Set; }4         Public intAmount {Get{returnValue *Count;} }5}
1             varItems =NewList<cashitem>() {2                 NewCashitem{value= -, count= at},3                 NewCashitem{value= -, count=Ten},4                 NewCashitem{value= -, count=5},5                 NewCashitem{value=Ten, count= One}6             };7              This. Cassetters.itemssource = items;
1   <DataGridName= "Cassetters"headersvisibility= "Column"gridlinesvisibility= "Horizontal"AutoGenerateColumns= "False">2       <Datagrid.columns>3           <DatagridtextcolumnHeader= "Face value"Width= "a"Binding="{Binding Value}">4           </Datagridtextcolumn>5           <DatagridtextcolumnHeader= "Number of sheets"Width= "a"Binding="{Binding Count}"></Datagridtextcolumn>6           <DatagridtextcolumnHeader= "Amount"Width="*"Binding="{Binding Amount}"></Datagridtextcolumn>7       </Datagrid.columns>8   </DataGrid>

Center DataGrid Column

The center of the column needs to use the template, but the center of the column header needs to define its own style, the data is bound to the same way as before by the binding property

1   <DataGridName= "Cassetters"headersvisibility= "Column"gridlinesvisibility= "Horizontal"AutoGenerateColumns= "False">2       <Datagrid.columns>3           <DatagridtextcolumnHeader= "Face value"Width= " the"Binding="{Binding Value}">4           </Datagridtextcolumn>5           <DatagridtextcolumnHeader= "Number of sheets"Width= " the"Binding="{Binding Count}"></Datagridtextcolumn>6           <DatagridtextcolumnHeader= "Amount"Width= " the"Binding="{Binding Amount}"></Datagridtextcolumn>7           <DatagridtemplatecolumnHeader= "template column"Width="*">8               <Datagridtemplatecolumn.headerstyle>9                   <StyleTargetType= "Datagridcolumnheader">Ten                       <Setter Property= "Horizontalcontentalignment"Value= "Center"></Setter> One                   </Style> A               </Datagridtemplatecolumn.headerstyle> -               <datagridtemplatecolumn.celltemplate> -                   <DataTemplate> the                       <TextBlockHorizontalAlignment= "Center"Text="{Binding Amount}"></TextBlock> -                   </DataTemplate> -               </datagridtemplatecolumn.celltemplate> -           </Datagridtemplatecolumn> +       </Datagrid.columns> -   </DataGrid>

WPF DataGrid binding and column centering

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.