WPF DataGrid使用 自动显示行号、全选、三级联动、拖拽

1.DataGrid的使用自动显示行号(修复删除行时行号显示不正确) dgTool.LoadingRow += new EventHandlerDataGridRowEventArgs (dgTool_LoadingRow); dgTool.UnloadingRow += new EventHandlerDataGridRowEventArgs (dgTool_UnloadingRow); void dgTool_LoadingRow( object sender, DataGridRowEventArg
  http://www.silverlightchina.net/html/study/WPF/2013/0117/21507.html 

  1.DataGrid的使用自动显示行号(修复删除行时行号显示不正确)

 dgTool.LoadingRow += new EventHandler<DataGridRowEventArgs>(dgTool_LoadingRow);
        dgTool.UnloadingRow +=new EventHandler<DataGridRowEventArgs>(dgTool_UnloadingRow);

        void dgTool_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            e.Row.Header = e.Row.GetIndex() + 1;
        }

        void dgTool_UnloadingRow(object sender, DataGridRowEventArgs e)
        {
            dgTool_LoadingRow(sender, e);
            if (dgTool.Items != null)
            {
                for (int i = 0; i < dgTool.Items.Count; i++)
                {
                    try
                    {
                        DataGridRow row = dgTool.ItemContainerGenerator.ContainerFromIndex(i) as DataGridRow;
                        if (row != null)
                        {
                            row.Header = (i + 1).ToString();
                        }
                    }
                    catch { }
                }
            }

        }

  2.DataGrid新建一行,并为需要的单元格提供默认值

dgTool.InitializingNewItem += new InitializingNewItemEventHandler(dgTool_InitializingNewItem);
 dgTool.RowEditEnding += new EventHandler<DataGridRowEditEndingEventArgs>(dgTool_RowEditEnding);


 private void dgTool_InitializingNewItem(object sender, InitializingNewItemEventArgs e)
        {
            //这里的实体为你绑定数据源中的实体类型
            EntityType newItem = e.NewItem as EntityType;
            newItem.ID = dgTool.Items.Count - 2;
        }
        //这里是为了解决添加完成一行数据之后,行号显示不正确(当然还可以做其他操作)
        void dgTool_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
        {
            if (dgTool.Items != null)
            {
                for (int i = 0; i < dgTool.Items.Count; i++)
                {

                    try
                    {
                        DataGridRow row = dgTool.ItemContainerGenerator.ContainerFromIndex(i) as DataGridRow;
                        if (row != null)
                        {
                            row.Header = (i + 1).ToString();
                        }
                    }
                    catch { }
                }
            }
        }

  3.DataGrid实现全选和单选

  1)DataGrid绑定数据实体类Entity

public class Entity : INotifyPropertyChanged
    {

        //标记是否删除
        private bool isChecked = false;
        public bool IsChecked
        {
            get
            {
                return isChecked;
            }
            set
            {
                isChecked = value;
                NotifyPropertyChanged("IsChecked");
            }
        }


        //序号
        private int id = 0;
        public int ID
        {
            get { return id; }
            set
            {
                id = value;
                NotifyPropertyChanged("ID");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public void NotifyPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

    }

  2)前台界面XAML

 <DataGrid x:Name="dgTool" Grid.Row="2"  SelectionChanged="dgTool_SelectionChanged"  RowHeaderWidth="50" FontFamily="Microsoft YaHei" FontSize="16" FontWeight="Normal" SelectionMode="Single"  AutoGenerateColumns="False" CanUserAddRows="True" CanUserDeleteRows="True" IsReadOnly="False" CanUserSortColumns="False"  AlternatingRowBackground="LightGray">
            <DataGrid.ColumnHeaderStyle>
                <Style TargetType="DataGridColumnHeader">
                    <Setter Property="HorizontalContentAlignment" Value="Center">
                    </Setter>
                </Style>
            </DataGrid.ColumnHeaderStyle>
            <DataGrid.Columns>
                <DataGridTemplateColumn>
                    <DataGridTemplateColumn.Header>
                        <CheckBox Name="selectAll_checkBox" Content="全选" IsThreeState="True"   HorizontalAlignment="Center" Click="selectAll_checkBox_Click" />
                    </DataGridTemplateColumn.Header>
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <CheckBox Name="select_checkBox"  IsChecked="{Binding Path=IsChecked}"  Click="select_checkBox_Click"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTextColumn Header="ID" Width="*" Binding="{Binding Path=ID}"/>
            </DataGrid.Columns>

        </DataGrid>

你可能感兴趣的:(WPF DataGrid使用 自动显示行号、全选、三级联动、拖拽)