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


    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
  • 相关阅读:
    好奇心
    ArcMap绘制错误
    ArcGISServer 发布地图服务 后在ArcGIS Service Directory中看不到
    软件安装顺序
    调试SOE程序应该注意的问题
    Win7语言栏不见了
    注册flash.ocx inno setup
    CSS个人收集,仅供参考
    看张亚飞《.Net for Flash FMS》的笔记
    GetDiskSerial DLL获取硬盘信息
  • 原文地址:https://www.cnblogs.com/jnyyq/p/9300705.html
Copyright © 2020-2023  润新知