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 = "﹣";
    }
}

数据显示效果

相关推荐
dotent·2 天前
C#基于WPF UI框架的通用基础上位机测试WPF框架
ui·c#·wpf
咩图3 天前
WPF+Prism8.0.0.1909+C#创建一个桌面程序
c#·wpf·prism
雁于飞3 天前
分布式基础
java·spring boot·分布式·spring·wpf·cloud native
oioihoii3 天前
WPF入门指南:解析默认项目结构
wpf
极客智造4 天前
深入解析 ReactiveUI:WPF 响应式 MVVM 开发的 “终极方案”
wpf
Macbethad5 天前
使用WPF编写一个多维度伺服系统的程序
大数据·hadoop·wpf
lingxiao168885 天前
WPF Prism框架应用
c#·wpf·prism
Macbethad6 天前
使用WPF编写一个Ethercat主站的程序
wpf
难搞靓仔6 天前
WPF 弹出窗体Popup
wpf·popup
Macbethad6 天前
使用WPF编写一个MODBUSTCP通信的程序
wpf