李sir_Blog

博客园 首页 联系 订阅 管理
  705 随笔 :: 58 文章 :: 134 评论 :: 193万 阅读

2025年2月20日

两种方式,第一种,用Group

第二种用Expander

这里写图片描述

在Window的Resources里面设置Style, GroupHeaderStyle:

        <Style x:Key="GroupHeaderStyle" TargetType="{x:Type GroupItem}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type GroupItem}">
                        <Expander IsExpanded="True">
                            <Expander.Header>
                                <TextBlock Text="{Binding Path=Name}"/>
                            </Expander.Header>
                            <ItemsPresenter />
                        </Expander>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

把这个Style应用到DataGrid上面:

            <DataGrid.GroupStyle>
                <GroupStyle ContainerStyle="{StaticResource GroupHeaderStyle}">
                    <GroupStyle.Panel>
                        <ItemsPanelTemplate>
                            <DataGridRowsPresenter/>
                        </ItemsPanelTemplate>
                    </GroupStyle.Panel>
                </GroupStyle>
            </DataGrid.GroupStyle>

整体xaml文件:

<Window x:Class="DataGridGroupDeamon.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <Style x:Key="GroupHeaderStyle" TargetType="{x:Type GroupItem}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type GroupItem}">
                        <Expander IsExpanded="True">
                            <Expander.Header>
                                <TextBlock Text="{Binding Path=Name}"/>
                            </Expander.Header>
                            <ItemsPresenter />
                        </Expander>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    <Grid>
        <DataGrid x:Name="dataGrid1"  AutoGenerateColumns="False" Margin="10">
            <DataGrid.GroupStyle>
                <GroupStyle ContainerStyle="{StaticResource GroupHeaderStyle}">
                    <GroupStyle.Panel>
                        <ItemsPanelTemplate>
                            <DataGridRowsPresenter/>
                        </ItemsPanelTemplate>
                    </GroupStyle.Panel>
                </GroupStyle>
            </DataGrid.GroupStyle>

            <DataGrid.Columns>
                <DataGridTextColumn Header="File" Binding="{Binding Name}"/>
                <DataGridTextColumn Header="Attachment Name" Binding="{Binding Sex}"/>

            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

数据准备文件:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace DataGridGroupDeamon
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        ObservableCollection<Model> data = new ObservableCollection<Model>();

        public MainWindow()
        {
            InitializeComponent();

            dataGrid1.ItemsSource = data;
            for (int i = 0; i < 10; i++)
            {
                Model m = CreateModel("张" + i, i / 2 + "年级", (i % 2 == 0 ? "女" : "男"));
                data.Add(m);
            }
            ICollectionView vw = CollectionViewSource.GetDefaultView(data);
            vw.GroupDescriptions.Add(new PropertyGroupDescription("Category")); 
        }

        private Model CreateModel(string name, string cate, string sex)
        {
            Model model = new Model();
            model.Name = name;
            model.Category = cate;
            model.Sex = sex;
            return model;
        }
    }

    public class Model
    {
        public string Name
        {
            get;
            set;
        }

        public string Category
        {
            get;
            set;
        }

        public string Sex
        {
            get;
            set;
        }
    }
}

<ItemsControl ItemsSource="{Binding  AAA}" Grid.Row="0">
<ItemsControl.Template>
<ControlTemplate TargetType="ItemsControl">
<ScrollViewer x:Name="scrollViewer" VerticalScrollBarVisibility="Auto">
<ItemsPresenter/>
</ScrollViewer>
</ControlTemplate>
</ItemsControl.Template>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Expander Header="{Binding Key}"   IsExpanded="True" Margin="0 5">
<DataGrid Style="{DynamicResource VirtualDataGrid}" VirtualizingPanel.IsVirtualizingWhenGrouping="True" AutoGenerateColumns="False" Name="dataGrid_LotList" ItemsSource="{Binding Value}" behaviours:CustomSortBehaviour.AllowCustomSort="True" CanUserAddRows="False" CanUserResizeColumns="True" CanUserResizeRows="False" HorizontalAlignment="Left"
FontFamily="Arial" Width="1585" FontSize="14">
<DataGrid.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="#FF0078D7"/>
</DataGrid.Resources>

<DataGrid.Columns>

<DataGridTextColumn Header="Create Time" Width="200" Binding="{Binding CreateTime, Mode=OneWay}" CanUserSort="True" CanUserReorder="False" IsReadOnly="True" CanUserResize="False" >
</DataGridTextColumn>
<DataGridTextColumn Header="Delete Time" Width="200" Binding="{Binding DeleteTime, Mode=OneWay}" CanUserSort="True" CanUserReorder="False" IsReadOnly="True" CanUserResize="False" >
</DataGridTextColumn>
<DataGridTemplateColumn Width="220" Header="Action">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Button Width="80" Height="25" Content="Export" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<micro:ActionMessage MethodName="Export">
</micro:ActionMessage>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>

</StackPanel>

</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<micro:ActionMessage MethodName="WaferHistoryChanged">
<micro:Parameter Value="{Binding ElementName=dataGrid_LotList, Path=SelectedItem}"/>
</micro:ActionMessage>
</i:EventTrigger>
</i:Interaction.Triggers>
</DataGrid>
</Expander>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>




posted @ 2025-02-20 10:49 李sir 阅读(5) 评论(0) 推荐(0) 编辑

2025年1月24日

摘要: 随着NoSQL的应用越来越广泛,很多面试都需要熟悉了解NoSQL【如:Redis,MongoDB等】,本文以一个简单的小例子,简述如何通过C#来操作MongoDB,进行数据的读写操作,仅供学习分享使用,如有不足之处,还请指正。 涉及知识点 MongoDB的基础知识,CRUD(增删改查)等【关于Mon 阅读全文
posted @ 2025-01-24 16:26 李sir 阅读(32) 评论(0) 推荐(0) 编辑

摘要: 在MQTT协议中,一个MQTT数据包由:固定头(Fixed header)、 可变头(Variable header)、 消息体(payload)三部分构成。 MQTT 数据包结构 固定头(Fixed header),存在于所有MQTT数据包中,表示数据包类型及数据包的分组类标识 可变头(Varia 阅读全文
posted @ 2025-01-24 13:54 李sir 阅读(22) 评论(0) 推荐(0) 编辑

摘要: 消息中间件是一种用于在分布式系统中进行异步通信的技术,常用于解耦应用程序的不同组件、实现消息传递、提高系统的可伸缩性和可靠性等。以下是关于消息中间件的知识点以及可能会在面试中被问到的一些问题和答案: 消息中间件的知识点: 消息队列(Message Queue): 消息中间件通常基于消息队列的概念,它 阅读全文
posted @ 2025-01-24 13:11 李sir 阅读(20) 评论(0) 推荐(0) 编辑

摘要: MahApps.Metro: https://mahapps.com/docs/ 官方网站:MahApps.Metro - Home 开源代码:MahApps · GitHub MaterialDesignInXAML: http://materialdesigninxaml.net/ gong-w 阅读全文
posted @ 2025-01-24 10:59 李sir 阅读(11) 评论(0) 推荐(0) 编辑

2023年3月16日

摘要: 一、前言 有个项目需要用到时间编辑控件,在大量搜索无果后只能自己自定义一个了。MFC中倒是有这个控件,叫CDateTimeCtrl。大概是这个样子: 二、要实现的功能 要实现的功能包含: 编辑时、分、秒(可按数字键输入编辑) 获取焦点后可实现递增或递减 三、WFP实现原理 四个TextBox和两个T 阅读全文
posted @ 2023-03-16 08:37 李sir 阅读(399) 评论(0) 推荐(1) 编辑

摘要: 一、前言 WPF没有内置IP地址输入控件,因此我们需要通过自己定义实现。 我们先看一下IP地址输入控件有什么特性: 输满三个数字焦点会往右移 键盘←→可以空光标移动 任意位置可复制整段IP地址,且支持x.x.x.x格式的粘贴赋值 删除字符会自动向左移动焦点 知道以上特性,我们就可以开始动手了。 二、 阅读全文
posted @ 2023-03-16 08:26 李sir 阅读(260) 评论(0) 推荐(0) 编辑

2023年3月15日

摘要: 一、前言 滚动条一般用于加载进度,我们在看视频的时候或者在浏览网页的时候经常能看到加载进度的页面。在程序开发中,默认的进度加载样式可能跟程序风格不太一样,或者加载进度的时候需要更改一下加载的样式。这个时候就需要通过修改ProgressBar的样式来实现。 二、ProgressBar的基本样式 Pro 阅读全文
posted @ 2023-03-15 19:24 李sir 阅读(159) 评论(0) 推荐(0) 编辑

摘要: 一、前言 默认的MenuItem样式比较普通,这次自定义MenuItem的样式也只是对MenuItem的颜色风格进行变化。需要其他功能的变化,大家可以根据样式代码进行扩展。 MenuItem的样式代码: <!--MenuItem--> <Style TargetType="MenuItem"> <S 阅读全文
posted @ 2023-03-15 19:12 李sir 阅读(767) 评论(0) 推荐(0) 编辑

摘要: 一、前言 程序中经常会用到TabControl控件,默认的控件样式很普通。而且样式或功能不一定符合我们的要求。比如:我们需要TabControl的标题能够居中、或平均分布;或者我们希望TabControl的标题能够进行关闭。要实现这些功能我们需要对TabControl的样式进行定义。 二、实现Tab 阅读全文
posted @ 2023-03-15 17:10 李sir 阅读(622) 评论(0) 推荐(0) 编辑

点击右上角即可分享
微信分享提示