WPF mvvm模式下,DataGrid自动刷新行号

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
 
namespace IngoDesignPDMS
{
    /// <summary>
    /// 显示行号
    /// </summary>
    public class DataGridBehavior
    {
        #region RowNumbers property
 
        public static readonly DependencyProperty RowNumbersProperty =
            DependencyProperty.RegisterAttached("RowNumbers", typeof(bool), typeof(DataGridBehavior),
            new FrameworkPropertyMetadata(false, OnRowNumbersChanged));
 
        private static void OnRowNumbersChanged(DependencyObject source, DependencyPropertyChangedEventArgs args)
        {
            DataGrid grid = source as DataGrid;
            if (grid == null)
                return;
            if ((bool)args.NewValue)
            {
                grid.LoadingRow += onGridLoadingRow;
                grid.UnloadingRow += onGridUnloadingRow;
            }
            else
            {
                grid.LoadingRow -= onGridLoadingRow;
                grid.UnloadingRow -= onGridUnloadingRow;
            }
        }
 
        private static void refreshDataGridRowNumbers(object sender)
        {
            DataGrid grid = sender as DataGrid;
            if (grid == null)
                return;
 
            foreach (var item in grid.Items)
            {
                var row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromItem(item);
                if (row != null)
                    row.Header = row.GetIndex() + 1;
            }
        }
 
        private static void onGridUnloadingRow(object sender, DataGridRowEventArgs e)
        {
            refreshDataGridRowNumbers(sender);
        }
 
        private static void onGridLoadingRow(object sender, DataGridRowEventArgs e)
        {
            refreshDataGridRowNumbers(sender);
        }
 
        [AttachedPropertyBrowsableForType(typeof(DataGrid))]
        public static void SetRowNumbers(DependencyObject element, bool value)
        {
            element.SetValue(RowNumbersProperty, value);
        }
 
        public static bool GetRowNumbers(DependencyObject element)
        {
            return (bool)element.GetValue(RowNumbersProperty);
        }
 
        #endregion
    }
}
 
.xaml页面
控件顶部增加xmlns:local="clr-namespace:IngoDesignPDMS"
DataGrid应用
 <DataGrid Margin="0,0,0,5" x:Name="dgTopLine" CanUserAddRows="False" AutoGenerateColumns="False" CanUserSortColumns="False"  CanUserResizeRows="False" Grid.Column="2" Grid.RowSpan="6" 
                                        local:DataGridBehavior.RowNumbers="True" ItemsSource="{Binding ListTopLine}" SelectedItem="{Binding SelectTopLine}" SelectItems="{Binding SelectDatas}" SelectionChanged="dgTopLine_SelectionChanged">
                                <DataGrid.Columns>
                                    <DataGridTextColumn Header="X起始位置"  IsReadOnly="True" Width="Auto" Binding="{Binding Path=XStartPostion}"></DataGridTextColumn>
                                    <DataGridTextColumn Header="Y起始位置"  IsReadOnly="True" Width="Auto" Binding="{Binding Path=YStartPostion}"></DataGridTextColumn>
                                    <DataGridTextColumn Header="X结束位置"  IsReadOnly="True" Width="Auto" Binding="{Binding Path=XEndPostion}"></DataGridTextColumn>
                                    <DataGridTextColumn Header="Y结束位置"  IsReadOnly="True" Width="Auto" Binding="{Binding Path=YEndPostion}"></DataGridTextColumn>
                                    <DataGridTextColumn Header="半径"  IsReadOnly="True" Width="Auto" Binding="{Binding Path=Radius}"></DataGridTextColumn>
                                    <DataGridCheckBoxColumn Header="是否顺时针转"  IsReadOnly="True" Width="Auto" Binding="{Binding Path=IsClockwise}"></DataGridCheckBoxColumn>
                                    <DataGridTextColumn Header="偏移设定" IsReadOnly="True" Width="Auto" Binding="{Binding Path= OffsetSetting}"></DataGridTextColumn>
                                </DataGrid.Columns>
                            </DataGrid>
 
 
参考资料:
https://stackoverflow.com/questions/4663771/wpf-4-datagrid-getting-the-row-number-into-the-rowheader

 

posted on 2018-07-12 17:33  潇潇烟雨  阅读(1389)  评论(0编辑  收藏  举报