wpf之datagrid 序号列

方法一:在datagrid最前面添加序列号

窗体加载时:
<span style="font-family:KaiTi_GB2312;font-size:24px;">private void MdiChild_Loaded(object sender, RoutedEventArgs e)
        {
            dgData.LoadingRow += new EventHandler<DataGridRowEventArgs>(dataGrid_LoadingRow);
            dgData.UnloadingRow += new EventHandler<DataGridRowEventArgs>(dataGrid_UnLoadingRow);
        }

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

public  void dataGrid_UnLoadingRow(object sender, DataGridRowEventArgs e)
        {
            dataGrid_LoadingRow(sender, e);
            if (dgData.Items != null)
            {
                for (int i = 0; i < dgData.Items.Count; i++)
                {
                    try
                    {
                        DataGridRow row = dgData.ItemContainerGenerator.ContainerFromIndex(i) as DataGridRow;
                        if (row != null)
                        {
                            row.Header = (i + 1).ToString();
                        }
                    }
                    catch { }
                }
            }
        }</span>

结果:

缺点:没有标识“序号”二字。

方法二:在datagrid中添加一列,用来放置序列号


界面上的代码:
<span style="font-family:KaiTi_GB2312;font-size:24px;"><DataGrid>
<DataGrid.Columns>    
                    <DataGridTemplateColumn Header=" 序号" Width="50" MinWidth="10" IsReadOnly="True">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type DataGridRow}}, Path=Header}" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="10,0,0,0"></TextBlock>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                <DataGridTextColumn Binding="{Binding Name}" Width="*" Header="账套名称" IsReadOnly="True" />
</DataGrid.Columns> 
</DataGrid></span>

后台添加一个datagrid的【LoadingRow】方法:
  <span style="font-family:KaiTi_GB2312;font-size:24px;"> private void dgData_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            e.Row.Header = e.Row.GetIndex() + 1;
        }</span>

结果:

缺点:有缺陷,怎么回事?是因为datagrid的LoadingRow】方法的存在,若这个方法去掉,则序号也不显示了。最终想了个折中的办法,将最前面的那一个序号列隐藏掉。

方法:在datagrid中加上一个属性【HeadersVisibility="Column" 】,OK,结果完美出现。

没有了最前面的那个边框,反而更好看了。








### 回答1: WPF(Windows Presentation Foundation)是一种用于创建Windows应用程序界面的框架。在WPF中,可以使用DataGrid控件来展示和编辑数据。折叠DataGrid可以让用户在需要时展开和收起数据,以便更好地组织和管理信息。 要在WPF中实现折叠DataGrid,可以使用一些额外的控件和代码逻辑。首先,可以使用TreeView控件作为外层容器,用于显示可展开和收起的树形结构。然后,在TreeView的各个节点中,嵌套使用DataGrid控件来展示具体的数据。 为了实现折叠和展开的功能,可以在TreeView的节点上使用ToggleButton等控件,以便用户点击时切换折叠或展开状态。在代码中,可以使用数据绑定来动态地添加和删除TreeView节点,以及相应地调整DataGrid的可见性。当用户点击折叠按钮时,可以通过修改绑定的标志位来隐藏对应的DataGrid,从而实现折叠效果。 此外,还可以通过自定义样式和模板来美化和定制DataGrid和TreeView的外观。可以修改控件的背景、边框、字体等属性,以便与应用程序的整体风格保持一致。 总之,在WPF中实现折叠DataGrid需要合理运用控件和代码逻辑,通过数据绑定和样式调整,来实现折叠和展开的功能,同时保持应用程序的美观和易用性。 ### 回答2: WPF中的DataGrid控件是一个非常强大和灵活的控件,可以用于展示和编辑数据。如果要实现折叠(DataGrid Grouping),我们可以通过使用CollectionViewSource和GroupDescription来实现。 首先,我们需要创建一个CollectionViewSource对象,并将DataGrid的ItemsSource绑定到该对象上。CollectionViewSource允许我们对数据进行分组和排序。 然后,我们可以使用GroupDescription对象来指定分组的属性。GroupDescription可以是一个字符串,表示要根据某个属性进行分组,也可以是一个自定义的实现了IGrouping接口的对象。 最后,我们还需要设置DataGrid的GroupStyle以定义分组的样式。GroupStyle可以包含一个HeaderTemplate,用于显示分组的标题,以及一个ItemsPanel,用于显示分组的内容。 下面是一个简单的示例,展示如何在WPF中折叠(DataGrid Grouping): 1. XAML代码: ```xml <Window x:Class="WpfApp1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="450" Width="800"> <Grid> <DataGrid x:Name="dataGrid"> <DataGrid.GroupStyle> <GroupStyle> <GroupStyle.HeaderTemplate> <DataTemplate> <TextBlock FontWeight="Bold" Text="{Binding Path=Name}"/> </DataTemplate> </GroupStyle.HeaderTemplate> <GroupStyle.ContainerStyle> <Style TargetType="{x:Type GroupItem}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type GroupItem}"> <StackPanel> <TextBlock FontWeight="Bold" Text="{Binding Path=Items.Count}"/> <ItemsPresenter/> </StackPanel> </ControlTemplate> </Setter.Value> </Setter> </Style> </GroupStyle.ContainerStyle> </GroupStyle> </DataGrid.GroupStyle> </DataGrid> </Grid> </Window> ``` 2. C#代码: ```C# public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); List<Person> people = new List<Person> { new Person{Name="Mike", Age=25}, new Person{Name="Tom", Age=30}, new Person{Name="Mike", Age=35}, new Person{Name="Tom", Age=40} }; CollectionViewSource cvs = new CollectionViewSource(); cvs.Source = people; cvs.GroupDescriptions.Add(new PropertyGroupDescription("Name")); dataGrid.ItemsSource = cvs.View; } } public class Person { public string Name { get; set; } public int Age { get; set; } } ``` 以上代码创建了一个简单的Window,其中有一个DataGrid控件,展示了一个包含姓名和年龄的Person类的集合。通过设置GroupStyle,我们让DataGrid根据姓名进行分组,每个分组显示一个标题和该分组中的数据。 总结起来,WPFDataGrid控件可以通过使用CollectionViewSource和GroupDescription来实现折叠或分组的效果。配合适当的样式设置,我们可以根据自己的需求,灵活地展示和编辑数据。 ### 回答3: WPF折叠DataGrid是一种在用户界面中显示表格数据的方法,可以通过折叠视图以及展开视图来管理和显示大量的数据。 折叠DataGrid可以通过使用WPF的Expander控件来实现。Expander控件提供了一个可折叠的容器,用户可以单击标题栏来展开或折叠容器内容。 在折叠DataGrid中,可以将DataGrid放置在Expander控件中。当用户点击折叠DataGrid的标题时,Expander会展开,显示DataGrid的内容;当用户再次点击标题时,Expander会折叠,隐藏DataGrid的内容。 在WPF中,我们可以使用XAML来定义折叠DataGrid。首先,我们创建一个Expander控件,将DataGrid作为其Content。然后,我们可以设置Expander的Header为DataGrid的标题。最后,我们可以通过设置Expander的IsExpanded属性来控制DataGrid的展开和折叠。 下面是一个简单的WPF折叠DataGrid的示例: ```xaml <Grid> <Expander Header="DataGrid" IsExpanded="False"> <DataGrid> <!-- DataGrid和数据绑定 --> </DataGrid> </Expander> </Grid> ``` 在这个示例中,DataGrid被放置在Expander内,并将Expander的Header设置为"DataGrid"。IsExpanded属性设置为False,表示DataGrid默认处于折叠状态。 通过这种方式,我们可以方便地实现DataGrid的折叠和展开,以便在需要时显示或隐藏大量的数据。这在处理大量数据或需要用户手动展开来查看数据时非常有用。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值