Wpf:datagrid automatically generate line numbers

Source: Internet
Author: User

Let's share a way to automatically generate row numbers for DataGrid data by Datagridrowheader. It takes only one valueconverter to get it done.

  1. Value Converters
    1     classAutonumbervalueconverter:imultivalueconverter2     {3         #regionImultivalueconverter Members4 5          Public ObjectConvert (Object[] values, Type targetType,Objectparameter, System.Globalization.CultureInfo culture)6         {7             varitem = values[0];8             varItems = values[1] asitemcollection;9 Ten             varindex =items. IndexOf (item); One             return(Index +1). ToString (); A         } -  -          Public Object[] Convertback (ObjectValue, type[] targettypes,Objectparameter, System.Globalization.CultureInfo culture) the         { -             return NULL; -         } -  +         #endregion -}

  2. Front Code
    1<window x:class="Datagridautorownumberdemo.mainwindow"2xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"3xmlns:x="Http://schemas.microsoft.com/winfx/2006/xaml"4xmlns:demo="Clr-namespace:datagridautorownumberdemo"5title="MainWindow"6Width="525"7height=" -">8<Window.Resources>9<demo:autonumbervalueconverter x:key="Autonumbervalueconverter"/>Ten</Window.Resources> One<datagrid x:name="DataGrid"> A<DataGrid.RowHeaderStyle> -<style targettype="{x:type Datagridrowheader}"> -<setter property="Content"> the<Setter.Value> -<multibinding converter="{StaticResource Autonumbervalueconverter}"> -<binding/> -<binding path="Items"Relativesource="{RelativeSource Ancestortype={x:type DataGrid}}"/> +</MultiBinding> -</Setter.Value> +</Setter> A</Style> at</DataGrid.RowHeaderStyle> -</DataGrid> -</Window>

    Get it done!
    Sample code: Download

Wpf:datagrid automatically generate line numbers

Related Article

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.