DataGrid merge Cells (WPF)

Source: Internet
Author: User

Search for WPF merged cells on the Web, has not been searched, no way, can only be done by themselves. Actually is the DataGrid sets the DataGrid, in order to facilitate support column drag, in the merged datagridcolumn that column of the header also put a DataGrid, but the merge of this continuous column supports only one column of drag, the effect is as follows,

The code is as follows

First define two templates, one for the merge header display

<datatemplate x:key= "Mergeheader" >             <datagrid horizontalalignment= "Stretch"  horizontalgridlinesbrush= "Transparent"   borderthickness= "0"  margin= "0"  canuserreordercolumns= "false"  canusersortcolumns= "false"   canuserresizecolumns= "False" >                 <DataGrid.Columns>                     <datagridtextcolumn header= "Product lot number"  Width= "*"  isreadonly= "True"  />                     <datagridtextcolumn header= "Production date"  IsReadOnly= " True " width="  />                     <datagridtextcolumn header= "valid until"  isreadonly= "True"   width= "102"  / >                     <datagridtextcolumn header= "TEDSD"  width= "126"  isreadonly= "True"  / >                     <datagridtextcolumn header= "TESDDFF"  width= "  isreadonly=" "True"/ >                </ datagrid.columns>                 <DataGrid.Resources>                     <style targettype= "Datagridcolumnheader" >                         < Setter property= "Template"  value= "{Staticresource stylemergeheader}"/>                     </ style>                </ Datagrid.resources>            </datagrid >        </DataTemplate>
Another template is used to display the merge details, ItemsSource to bind to a list property of your data source, meaning that you put the merged details into the Sub list property of the main DataGrid data source because the header is not displayed in the details, so hide the header

<datatemplate x:key= "Mergecolumn" >             <datagrid name= "Dgmergegrid"  itemssource= "{binding meragedata}"   Horizontalscrollbarvisibility= "Hidden"  verticalscrollbarvisibility= "Hidden"  loaded= "dgMergeGrid_ Loaded " borderthickness=" 0,0,0,0 " horizontalalignment=" Center " horizontalcontentalignment=" Center " verticalalignment=" center " verticalcontentalignment=" center " headersvisibility=" None "   autogeneratecolumns= "False"  isreadonly= "True" >                 <DataGrid.Columns>                     < Datagridtextcolumn header= "Product lot number"  width= "*"  isreadonly= "True"  binding= "{binding  Producebatchno} ">                     </datagridtextcolumn>                      <datagridtextcolumn header= "Production date"   isreadonly= "True"  width= " " Binding= "{binding producedate}"  />                     <datagridtextcolumn header= "valid until"    isreadonly= "True"   width= "102"  binding= "{binding expriedate}"  />                      <datagridtextcolumn header= "Dfafa"   width= "126"  isreadonly= "True"  Binding= "{ Binding smallnum} " />                   &nbSp; <datagridtextcolumn header= "FAFSFDFD"   width= "" "  isreadonly=" True "  binding= "{binding packunit}"  />                 </DataGrid.Columns>                 <DataGrid.Resources>                     <style targettype= "DataGrid" >                         <!--grid Line Color-->                         < Setter property= "Horizontalgridlinesbrush" >              &nBsp;              <setter.value>                                  <SolidColorBrush  Color= "Transparent"/>                             </Setter.Value>                          </Setter>                         <Setter  Property= "Verticalgridlinesbrush" >                             <setter.value>                                  <solidcolorbrush color= "LightBlue"/>                              </Setter.Value>                          </Setter>                     </Style>                 </DataGrid.Resources>             &lT;/datagrid>        </datatemplate>

Then set the template on the column that displays the details in your main DataGrid.
<datagridtemplatecolumn  header= "Batch Information"  width= "580"  headertemplate= "{StaticResource  mergeheader} " celltemplate=" {staticresource mergecolumn} ">                     < datagridtemplatecolumn.headerstyle>                         <style targettype= " Datagridcolumnheader ">                             <setter property= " Background " value=" LightBlue "></Setter>                         </Style>          &Nbsp;          </datagridtemplatecolumn.headerstyle>                 </ Datagridtemplatecolumn>
Original link: http://www.cnblogs.com/renzhendewo/p/4238757.html

DataGrid merge Cells (WPF)

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.