惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

G
Google Developers Blog
F
Fortinet All Blogs
Microsoft Azure Blog
Microsoft Azure Blog
腾讯CDC
Vercel News
Vercel News
Recent Announcements
Recent Announcements
博客园 - Franky
小众软件
小众软件
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
The Cloudflare Blog
宝玉的分享
宝玉的分享
I
InfoQ
博客园 - 聂微东
Jina AI
Jina AI
J
Java Code Geeks
V
V2EX
U
Unit 42
Stack Overflow Blog
Stack Overflow Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
阮一峰的网络日志
阮一峰的网络日志
L
LangChain Blog
T
The Blog of Author Tim Ferriss
量子位

博客园 - FredGrit

WPF customize via three combied custom controls WPF DataGrid DataGridTemplateColumn DataTemplate ContentPresenter ContentTemplate WPF Customcontrol NumUpDownScroller WPF CustomControl override Template in Generic.xaml and invoke different style WPF HierarchicalDataTemplate customize via ToggleButton WPF DataGrid DataGridTemplateColumn DataTemplate call predefined DataTemplate via ContentPresenter and ContentTemplate WPF ListBox load data via contentcontrol an ItemTemplate WPF DataGrid load data from Asp.Net Core WebAPI WPF display grouped data with GroupBox and ItemsControl WPF two listbox scroll syncchronously via behavior WPF invoke data from WebAPI,DataGridTemplate call pre defined DataTemplate via ContentPresenter WPF ListBox load data from WCF WPF datagrid load data from WCF via json, export selected items to json file WPF ItemsControl load data from WCF,DataTemplate, ContentControl WPF customize rotated wheel relentlessly via custom control WPF embed DataTemplate in HierchicalDataTemplate of TreeView WPF ContentControl, ItemsControl, ItemsPanelTemplate,VirtualizingStackPanel,convert xml string to List<T> WPF parse web.config recursively, TreeView and HierarchicalDataTemplate WPF Custom control in cs and Generic.xaml WPF ContextMenu independent visual tree resolved via Freezable implemented class WPF custom control GetTemplateChild vs FindName,NameScope separation between Logical Tree and Visual Tree, WPF ListBox ListView Datatemplate, parse xml to List via XmlSerializer and StringReader WPF TreeView HierarchicalDataTemplate, parse xml via traverse in XmlElement WPF parse xml via [XmRoot] and [XmlElement] attributes together, contentcontrol's template is ControlTemplate from Resources WPF ContentControl invoke Template from resource, reuse datatemplate WPF deserialize xml string as List via [XmlRoot] and [XmlElement] attribute WPF ContentControl Template WPF DatagridTemplate Binding DataTemplate WPF TreeView explicitly given key name to HierarchicalDataTemplate WPF, parse XMlDocument as List
WPF TreeView HierarchicalDataTemplate with grouped Data
FredGrit · 2026-08-30 · via 博客园 - FredGrit
  <DataTemplate DataType="{x:Type local:Book}"
                x:Key="BookRowDataTemplate">
      <Border BorderBrush="Gray"
              BorderThickness="2"
              Margin="5">
          <Grid Margin="5">
              <Grid.Resources>
                  <Style TargetType="TextBlock" BasedOn="{StaticResource TbkStyle}"/>
              </Grid.Resources>
              <Grid.RowDefinitions>
                  <RowDefinition/>
                  <RowDefinition/>
                  <RowDefinition/>
              </Grid.RowDefinitions>
              <Grid.ColumnDefinitions>
                  <ColumnDefinition/>
                  <ColumnDefinition/>
                  <ColumnDefinition/>
              </Grid.ColumnDefinitions>
              <TextBlock Text="{Binding Id}" Grid.Row="0" Grid.Column="0"/>
              <TextBlock Text="{Binding Name}" Grid.Row="0" Grid.Column="1"/>
              <TextBlock Text="{Binding Comment}" Grid.Row="0" Grid.Column="2"/>
              <TextBlock Text="{Binding Content}" Grid.Row="1" Grid.Column="0"/>
              <TextBlock Text="{Binding ISBN}" Grid.Row="1" Grid.Column="1"/>
              <TextBlock Text="{Binding Summary}" Grid.Row="1" Grid.Column="2"/>
              <TextBlock Text="{Binding Author}" Grid.Row="2" Grid.Column="0"/>
              <TextBlock Text="{Binding Title}" Grid.Row="2" Grid.Column="1"/>
              <TextBlock Text="{Binding Topic}" Grid.Row="2" Grid.Column="2"/>
          </Grid>
      </Border>
  </DataTemplate>

  <HierarchicalDataTemplate DataType="{x:Type local:GroupedBook}"
                            ItemsSource="{Binding BksList}"
                            ItemTemplate="{StaticResource BookRowDataTemplate}"
                            x:Key="TreeViewItemTemplate">
      <GroupBox Header="{Binding GroupName}"
                FontSize="80"
                Margin="10,10,10,150"
                BorderBrush="Blue"
                BorderThickness="5"/>
  </HierarchicalDataTemplate>
<Window x:Class="WpfApp4.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp4"
        mc:Ignorable="d"
        Title="{Binding MainTitle}" WindowState="Maximized">
    <Window.DataContext>
        <local:MainVM/>
    </Window.DataContext>
    <Window.Resources>
        <Style TargetType="TextBlock" x:Key="TbkStyle">
            <Setter Property="FontSize" Value="30"/>
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Foreground" Value="Red"/>
                    <Setter Property="FontWeight" Value="ExtraBold"/>
                </Trigger>
            </Style.Triggers>
        </Style>

        <DataTemplate DataType="{x:Type local:Book}"
                      x:Key="BookRowDataTemplate">
            <Border BorderBrush="Gray"
                    BorderThickness="2"
                    Margin="5">
                <Grid Margin="5">
                    <Grid.Resources>
                        <Style TargetType="TextBlock" BasedOn="{StaticResource TbkStyle}"/>
                    </Grid.Resources>
                    <Grid.RowDefinitions>
                        <RowDefinition/>
                        <RowDefinition/>
                        <RowDefinition/>
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition/>
                        <ColumnDefinition/>
                        <ColumnDefinition/>
                    </Grid.ColumnDefinitions>
                    <TextBlock Text="{Binding Id}" Grid.Row="0" Grid.Column="0"/>
                    <TextBlock Text="{Binding Name}" Grid.Row="0" Grid.Column="1"/>
                    <TextBlock Text="{Binding Comment}" Grid.Row="0" Grid.Column="2"/>
                    <TextBlock Text="{Binding Content}" Grid.Row="1" Grid.Column="0"/>
                    <TextBlock Text="{Binding ISBN}" Grid.Row="1" Grid.Column="1"/>
                    <TextBlock Text="{Binding Summary}" Grid.Row="1" Grid.Column="2"/>
                    <TextBlock Text="{Binding Author}" Grid.Row="2" Grid.Column="0"/>
                    <TextBlock Text="{Binding Title}" Grid.Row="2" Grid.Column="1"/>
                    <TextBlock Text="{Binding Topic}" Grid.Row="2" Grid.Column="2"/>
                </Grid>
            </Border>
        </DataTemplate>

        <HierarchicalDataTemplate DataType="{x:Type local:GroupedBook}"
                                  ItemsSource="{Binding BksList}"
                                  ItemTemplate="{StaticResource BookRowDataTemplate}"
                                  x:Key="TreeViewItemTemplate">
            <GroupBox Header="{Binding GroupName}"
                      FontSize="80"
                      Margin="10,10,10,150"
                      BorderBrush="Blue"
                      BorderThickness="5"/>
        </HierarchicalDataTemplate>

        <Style TargetType="TreeViewItem">
            <Setter Property="IsExpanded" Value="False"/>
        </Style>
    </Window.Resources>
    <Grid>
        <TreeView ItemsSource="{Binding GroupedBksCollection}"
                  ItemTemplate="{StaticResource TreeViewItemTemplate}"/>
    </Grid>
</Window>




using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Net;
using System.Net.Http;
using System.Runtime.CompilerServices;
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 Newtonsoft.Json;

namespace WpfApp4
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }

    public class MainVM : INotifyPropertyChanged
    {
        private static HttpClient httpClient = new HttpClient()
        {
            Timeout = TimeSpan.FromHours(1)
        };

        string url = "http://localhost:5000/api/book/getbooks/";
        private bool isLoading = false;

        public MainVM()
        {
            if (!DesignerProperties.GetIsInDesignMode(new DependencyObject()))
            {
                _ = InitBooksAsync(10);
            }
        }

        private async Task InitBooksAsync(int cnt = 1000)
        {
            if (isLoading)
            {
                return;
            }
            isLoading = true;

            MainTitle = $"{DateTime.Now},loading...";
            try
            {
                string jsonStr = await httpClient.GetStringAsync($"{url}{cnt}");
                var bks = JsonConvert.DeserializeObject<List<Book>>(jsonStr);
                if (bks != null && bks.Any())
                {
                    var tempGroupedBks = bks.GroupBy(x => x.CategoryName);
                    if (tempGroupedBks != null && tempGroupedBks.Any())
                    {
                        GroupedBksCollection = new ObservableCollection<GroupedBook>();
                        foreach (var g in tempGroupedBks)
                        {
                            GroupedBksCollection.Add(new GroupedBook()
                            {
                                GroupName = g.Key,
                                BksList = g.ToList()
                            });
                        }
                    }
                    MainTitle = $"{DateTime.Now},loaded {bks.Count} items" +
                        $"FirstId:{bks.FirstOrDefault()?.Id}" +
                        $"LastId:{bks.LastOrDefault()?.Id}";
                    System.Diagnostics.Debug.WriteLine(MainTitle);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex?.Message);
            }
            finally
            {
                isLoading = false;
            }
        }

        private string mainTitle = $"{DateTime.Now}";
        public string MainTitle
        {
            get
            {
                return mainTitle;
            }
            set
            {
                if (value != mainTitle)
                {
                    mainTitle = value;
                    OnPropertyChanged();
                }
            }
        }

        private ObservableCollection<GroupedBook> groupedBksCollection;
        public ObservableCollection<GroupedBook> GroupedBksCollection
        {
            get
            {
                return groupedBksCollection;
            }
            set
            {
                if (value != groupedBksCollection)
                {
                    groupedBksCollection = value;
                    OnPropertyChanged();
                }
            }
        }

        public event PropertyChangedEventHandler? PropertyChanged;
        private void OnPropertyChanged([CallerMemberName] string propName = "")
        {
            var handler = Volatile.Read(ref this.PropertyChanged);
            if (handler == null)
            {
                return;
            }
            handler(this, new PropertyChangedEventArgs(propName));
        }
    }

    public class GroupedBook
    {
        public string GroupName { get; set; }
        public List<Book> BksList { get; set; }
    }

    public class Book
    {
        public long Id { get; set; }
        public string Name { get; set; }
        public string Author { get; set; }
        public string CategoryName { get; set; }
        public string Comment { get; set; }
        public string Content { get; set; }
        public string ISBN { get; set; }
        public string Summary { get; set; }
        public string Title { get; set; }
        public string Topic { get; set; }
    }
}

image

image

image