zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    在小程序开发的新风口 看华为云如何助立创科技抢占市场红利
    华为云软件开发云助力集时通软件敏捷开发
    一站式云端安卓软件开发工具的体验之路!
    一名优秀的全栈工程师必需的开发工具
    学哪种编程语言更有“钱”赚?
    深度剖析:最新云端开发工具如何实现敏捷+DevOps开发落地
    软件开发未来发展五大趋势,从业者们注意了!
    十个最有“钱景”的IT技能, 你掌握了哪个?
    “敏捷开发”之白话篇
    解决软件开发中的多个痛点——华为软件开发云
  • 原文地址:https://www.cnblogs.com/jnyyq/p/9300705.html
Copyright © 2011-2022 走看看