WPF DataGrid详细列表手动显示与隐藏

设置显示序号与折叠显示样式

xml 复制代码
<DataTemplate x:Key="dtNum">
    <Button BorderBrush="Transparent" Style="{x:Null}" Click="BtnRowDetail_ShowHideClick" FontSize="16" Background="Transparent">
        <StackPanel Orientation="Horizontal">
            <TextBlock x:Name="tag" Text="﹣"/>
            <!--注意此处绑定查询数据行序号值-->
            <TextBlock Text="{Binding rownum}"/>
        </StackPanel>
    </Button>
</DataTemplate>

设置DataGrid详细列表

xml 复制代码
<DataGrid ItemsSource="{Binding}"
          AutoGenerateColumns="False" IsReadOnly="True" 
          GridLinesVisibility="All"
          SelectionUnit="FullRow"
          RowDetailsVisibilityMode="Visible"
          ScrollViewer.CanContentScroll="False"
          AlternatingRowBackground="LightBlue"
          VerticalGridLinesBrush="{DynamicResource {x:Static SystemColors.MenuHighlightBrushKey}}"
          LoadingRowDetails="dgDetail_LoadingDetails"
          ColumnHeaderStyle="{StaticResource HeaderStyle}">
    <DataGrid.Columns>
        <!--关联详细列表隐藏样式-->
        <DataGridTemplateColumn Header="#" Width="Auto" MinWidth="60" CellTemplate="{StaticResource dtNum}"/>
        <DataGridTextColumn Header="id" Binding="{Binding Path=id}" Width="Auto" Visibility="Hidden"/>
        <DataGridTextColumn Header="工装编号" Binding="{Binding Path=gz_code}" Width="Auto" MinWidth="100"/>
        <DataGridTextColumn Header="表芯编号" TextBlock.TextAlignment="Center" Binding="{Binding Path=chip_code}" Width="Auto" MinWidth="100"/>
        <DataGridTemplateColumn Header="合格" Width="100" CellTemplate="{StaticResource dtJudge}"/>
        <DataGridTextColumn Header="时间" Binding="{Binding Path=time, StringFormat='yyyy年MM月dd日 HH:mm:ss'}" Width="*"/>
    </DataGrid.Columns>
    <DataGrid.RowDetailsTemplate>
        <DataTemplate>
            <DataGrid
              ItemsSource="{Binding}"
              AutoGenerateColumns="False" IsReadOnly="True" 
              GridLinesVisibility="All"
              SelectionUnit="FullRow"
              ScrollViewer.CanContentScroll="False"
              AlternatingRowBackground="LightBlue"
              Margin="20 0 0 0"
              VerticalGridLinesBrush="{DynamicResource {x:Static SystemColors.MenuHighlightBrushKey}}"
              PreviewMouseWheel="DataGridDetail_PreviewMouseWheel"
              ColumnHeaderStyle="{StaticResource HeaderStyle}">
                <DataGrid.Columns>
                    <DataGridTextColumn Header="#" Binding="{Binding rownum}"  Width="Auto" MinWidth="40"/>
                    <DataGridTextColumn Header="id" Binding="{Binding Path=id}" Width="Auto" Visibility="Hidden"/>
                    <DataGridTextColumn Header="角度(°)" Binding="{Binding Path=angle, StringFormat={}{0:F0}}" Width="100"/>
                    <DataGridTextColumn Header="电压(mv)" Binding="{Binding Path=voltage, StringFormat={}{0:F4}}" Width="100"/>
                    <DataGridTemplateColumn Header="合格" Width="100" CellTemplate="{StaticResource dtJudge}"/>
                    <DataGridTextColumn Header="时间" Binding="{Binding Path=time, StringFormat='yyyy年MM月dd日 HH:mm:ss'}" Width="*"/>
                </DataGrid.Columns>
            </DataGrid>
        </DataTemplate>
    </DataGrid.RowDetailsTemplate>
</DataGrid>

详细列表鼠标滚轮支持后台代码

csharp 复制代码
private void DataGridDetail_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
{
    var eventArg = new MouseWheelEventArgs(e.MouseDevice, e.Timestamp, e.Delta)
    {
        RoutedEvent = MouseWheelEvent,
        Source = sender
    };
    (sender as DataGrid).RaiseEvent(eventArg);
}

行详细信息显示后台代码

csharp 复制代码
private void BtnRowDetail_ShowHideClick(object sender, RoutedEventArgs e)
{
    var obj = sender as DependencyObject;
    var tb = obj.FindChild<TextBlock>("tag");
    if (null == tb) return;

    while (obj != null && !(obj is DataGridRow))
    {
        obj = VisualTreeHelper.GetParent(obj);
    }

    DataGridRow row = obj as DataGridRow;
    if (null == row) return;

    if (tb.Text.IndexOf("﹣") > -1)
    {
        row.DetailsVisibility = Visibility.Collapsed;
        tb.Text = "﹢";
    } else
    {
        row.DetailsVisibility = Visibility.Visible;
        tb.Text = "﹣";
    }
}

数据显示效果

相关推荐
Crazy Struggle14 小时前
.NET 8.0 通用管理平台,支持模块化、WinForms 和 WPF
vue·wpf·winform·.net 8.0·通用权限管理
呆萌很1 天前
HCIP-HarmonyOS Application Developer 习题(二十)
华为·wpf·harmonyos
九鼎科技-Leo2 天前
什么是 WPF 中的转换器?如何自定义一个值转换器?
开发语言·windows·c#·.net·wpf
为风而战2 天前
Deepin 系统中安装Rider和Uno Platform
linux·运维·.net·wpf
△曉風殘月〆2 天前
WPF MVVM入门系列教程(三、数据绑定)
c#·wpf·mvvm
^@^lemon tea^@^2 天前
.NET6中WPF项目添加System.Windows.Forms引用
wpf·.net6·windows.forms引用
九鼎科技-Leo2 天前
在 WPF 中,绑定机制是如何工作的?WPF数据绑定机制解析
windows·c#·.net·wpf
就是有点傻3 天前
WPF自定义翻页控件
wpf
eggcode3 天前
使用ookii-dialogs-wpf在WPF选择文件夹时能输入路径
c#·wpf
code bean3 天前
【wpf】ResourceDictionary 字典资源的用法
windows·c#·wpf