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

推荐订阅源

博客园_首页
IT之家
IT之家
博客园 - Franky
Stack Overflow Blog
Stack Overflow Blog
宝玉的分享
宝玉的分享
Recent Announcements
Recent Announcements
Engineering at Meta
Engineering at Meta
S
SegmentFault 最新的问题
V
Visual Studio Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Last Week in AI
Last Week in AI
H
Help Net Security
V
V2EX
H
Hackread – Cybersecurity News, Data Breaches, AI and More
量子位
博客园 - 叶小钗
J
Java Code Geeks
博客园 - 【当耐特】
月光博客
月光博客
爱范儿
爱范儿
人人都是产品经理
人人都是产品经理
酷 壳 – CoolShell
酷 壳 – CoolShell
小众软件
小众软件

博客园 - 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 TreeView HierarchicalDataTemplate with grouped Data 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 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 ListBox ListView Datatemplate, parse xml to List via ...
FredGrit · 2026-08-01 · via 博客园 - FredGrit
[XmlRoot("ArrayOfBook", Namespace = "http://schemas.datacontract.org/2004/07/WcfService4")]
public class XmlBook
{
    [XmlElement(nameof(Book))]
    public List<Book> BksList { get; set; }
}

 private List<Book> DeserializeXmlStrToList(string xmlStr)
 {
     var xmlSerializer = new XmlSerializer(typeof(XmlBook));
     using (var reader = new StringReader(xmlStr))
     {
         var bks = (XmlBook)xmlSerializer.Deserialize(reader);
         return bks.BksList;
     }
 }
<Window x:Class="WpfApp8.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:WpfApp8"
        mc:Ignorable="d"
        Title="MainWindow"
        WindowState="Maximized">
    <Window.DataContext>
        <local:MainVM/>
    </Window.DataContext>
    <Window.Resources>
        <Style TargetType="TextBlock" x:Key="TbkStyle">
            <Setter Property="FontSize" Value="20"/>
            <Setter Property="FontWeight" Value="Normal"/>
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Foreground" Value="Red"/>
                </Trigger>
            </Style.Triggers>
        </Style>

        <DataTemplate DataType="{x:Type local:Book}"
                      x:Key="BookDataTemplate">
            <Border BorderBrush="Cyan"
                    BorderThickness="1"
                    Margin="5">
                <Grid Margin="5">
                    <Grid.Resources>
                        <Style TargetType="TextBlock" BasedOn="{StaticResource TbkStyle}"/>
                    </Grid.Resources>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition MinWidth="100" SharedSizeGroup="Id"/>
                        <ColumnDefinition MinWidth="100" SharedSizeGroup="Name"/>
                        <ColumnDefinition Width="*" SharedSizeGroup="ISBN"/>
                        <ColumnDefinition MinWidth="100" SharedSizeGroup="Author"/>
                        <ColumnDefinition MinWidth="100" SharedSizeGroup="Comment"/>
                        <ColumnDefinition MinWidth="100" SharedSizeGroup="CategoryName"/>
                    </Grid.ColumnDefinitions>
                    <TextBlock Text="{Binding Id}" Grid.Column="0"/>
                    <TextBlock Text="{Binding Name}" Grid.Column="1"/>
                    <TextBlock Text="{Binding ISBN}" Grid.Column="2"/>
                    <TextBlock Text="{Binding Author}" Grid.Column="3"/>
                    <TextBlock Text="{Binding Comment}" Grid.Column="4"/>
                    <TextBlock Text="{Binding CategoryName}" Grid.Column="5"/>
                </Grid>
            </Border>
        </DataTemplate>

        <ControlTemplate TargetType="ContentControl" x:Key="ContentControlTemplate">
            <ListBox Grid.IsSharedSizeScope="True"
                     ItemsSource="{Binding BksCollection}"
                     ItemTemplate="{StaticResource BookDataTemplate}"/>
        </ControlTemplate>
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <GroupBox Header="ContentControlTemplate"
                  FontSize="30"
                  Grid.Row="0"
                  Margin="10"
                  BorderBrush="Blue"
                  BorderThickness="5">
            <ContentControl Template="{StaticResource ContentControlTemplate}"
                            Margin="5"/>
        </GroupBox>

        <GroupBox Header="ListBox"
                  FontSize="30"
                  Grid.Row="1"
                  Margin="10"
                  BorderBrush="Blue"
                  BorderThickness="5">
            <ListBox ItemsSource="{Binding BksCollection}" Grid.IsSharedSizeScope="True"
                     Margin="10">
                <ListBox.ItemTemplate>
                    <DataTemplate DataType="{x:Type local:Book}">
                        <Border BorderBrush="Cyan"
                     BorderThickness="1"
                     Margin="5">
                            <Grid>
                                <Grid.Resources>
                                    <Style TargetType="TextBlock" BasedOn="{StaticResource TbkStyle}"/>
                                </Grid.Resources>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition MinWidth="100" SharedSizeGroup="Id"/>
                                    <ColumnDefinition MinWidth="100" SharedSizeGroup="Name"/>
                                    <ColumnDefinition Width="*" SharedSizeGroup="ISBN"/>
                                    <ColumnDefinition MinWidth="100" SharedSizeGroup="Author"/>
                                    <ColumnDefinition MinWidth="100" SharedSizeGroup="Comment"/>
                                    <ColumnDefinition MinWidth="100" SharedSizeGroup="CategoryName"/>
                                </Grid.ColumnDefinitions>
                                <TextBlock Text="{Binding Id}" Grid.Column="0"/>
                                <TextBlock Text="{Binding Name}" Grid.Column="1"/>
                                <TextBlock Text="{Binding ISBN}" Grid.Column="2"/>
                                <TextBlock Text="{Binding Author}" Grid.Column="3"/>
                                <TextBlock Text="{Binding Comment}" Grid.Column="4"/>
                                <TextBlock Text="{Binding CategoryName}" Grid.Column="5"/>
                            </Grid>
                        </Border>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </GroupBox>

        <GroupBox Header="ListView"
                  Grid.Row="2"
                  FontSize="30"
                  BorderBrush="Blue"
                  BorderThickness="10">
            <ListView ItemsSource="{Binding BksCollection}">
                <ListView.Resources>
                    <Style TargetType="TextBlock" BasedOn="{StaticResource TbkStyle}"/>
                </ListView.Resources>
                <ListView.View>
                    <GridView>
                        <GridViewColumn Header="Id" DisplayMemberBinding="{Binding Id}" Width="Auto"/>
                        <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" Width="Auto"/>
                        <GridViewColumn Header="ISBN" DisplayMemberBinding="{Binding ISBN}" Width="Auto"/>
                        <GridViewColumn Header="Author" DisplayMemberBinding="{Binding Author}" Width="100"/>
                        <GridViewColumn Header="Comment" DisplayMemberBinding="{Binding Comment}" Width="100"/>
                        <GridViewColumn Header="Category" DisplayMemberBinding="{Binding CategoryName}" Width="100"/>
                    </GridView>
                </ListView.View>
            </ListView>
        </GroupBox>
    </Grid>
</Window>


using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
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 System.Xml.Serialization;

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

    public class MainVM : INotifyPropertyChanged
    {
        private static HttpClient client = new HttpClient();
        private static string originUrl = "http://localhost:64660/BookService.svc/getbooks?cnt=";

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

        private async Task InitBksAsync(int cnt=10000)
        {
            string url = $"{originUrl}{cnt}";
            string xmlStr = await client.GetStringAsync(url);
            var bks = DeserializeXmlStrToList(xmlStr);
            BksCollection = new ObservableCollection<Book>(bks);
        }

        private List<Book> DeserializeXmlStrToList(string xmlStr)
        {
            var xmlSerializer = new XmlSerializer(typeof(XmlBook));
            using (var reader = new StringReader(xmlStr))
            {
                var bks = (XmlBook)xmlSerializer.Deserialize(reader);
                return bks.BksList;
            }
        }

        private ObservableCollection<Book> bksCollection;
        public ObservableCollection<Book> BksCollection
        {
            get
            {
                return bksCollection;
            }
            set
            {
                if (value != bksCollection)
                {
                    bksCollection = value;
                    OnPropertyChanged();
                }
            }
        }

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

    [XmlRoot("ArrayOfBook", Namespace = "http://schemas.datacontract.org/2004/07/WcfService4")]
    public class XmlBook
    {
        [XmlElement(nameof(Book))]
        public List<Book> BksList { get; set; }
    }

    public class Book
    {
        public long Id { get; set; }
        public string Name { get; set; }
        public string ISBN { get; set; }
        public string Author { get; set; }
        public string Comment { get; set; }
        public string CategoryName { get; set; }
    }
}
//WCF
//D:\C\WcfService4\WcfService4\IBookService.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace WcfService4
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IBookService" in both code and config file together.
    [ServiceContract]
    public interface IBookService
    {
        [OperationContract]
        [WebGet(UriTemplate ="/getbooks?cnt={cnt}")]
        List<Book> GetBooks(int cnt=1000);
    }

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

    enum BookCategoryEnum
    {
        Science,
        Technology,
        Engineering,
        Math
    }
}


//D:\C\WcfService4\WcfService4\BookService.svc.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Threading;

namespace WcfService4
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "BookService" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select BookService.svc or BookService.svc.cs at the Solution Explorer and start debugging.
    public class BookService : IBookService
    {
        private static long id = 1;
        private static string[] enumNames = Enum.GetNames(typeof(BookCategoryEnum));
        private static int enumNamesCnt = enumNames.Length;
        private static Random rnd = new Random();
        private static (long, long) GetStartEnd(int cnt)
        {
            var end = Interlocked.Add(ref id, cnt);
            var start = end - cnt;
            return (start, end);
        }

        public List<Book> GetBooks(int cnt = 1000)
        {
            List<Book> bksList = new List<Book>();
            var (start, end) = GetStartEnd(cnt);
            for (long i = start; i < end; i++)
            {
                bksList.Add(new Book()
                {
                    Id = i,
                    Name = $"Name_{i}",
                    ISBN = $"ISBN_{i}_{Guid.NewGuid():N}",
                    Author = $"Author_{i}",
                    Comment = $"Comment_{i}",
                    CategoryName = $"{enumNames[rnd.Next(0, enumNamesCnt)]}"
                });
            }
            return bksList;
        }
    }
}


//D:\C\WcfService4\WcfService4\Web.config
<?xml version="1.0"?>
<configuration>
  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true"/>
  </appSettings>
  <!--
    For a description of web.config changes see http://go.microsoft.com/fwlink/?LinkId=235367.

    The following attributes can be set on the <httpRuntime> tag.
      <system.Web>
        <httpRuntime targetFramework="4.8.1" />
      </system.Web>
  -->
  <system.web>
    <compilation debug="true" targetFramework="4.8.1"/>
    <httpRuntime targetFramework="4.7.2"/>
  </system.web>
  <system.serviceModel>
      <bindings>
          <webHttpBinding>
              <binding name="BookServiceWebHttpBinding"
                       openTimeout="01:00:00"
                       closeTimeout="01:00:00"
                       sendTimeout="01:00:00"
                       receiveTimeout="01:00:00"
                       maxBufferPoolSize="2147483647"
                       maxBufferSize="2147483647"
                       maxReceivedMessageSize="2147483647">
                  <readerQuotas maxArrayLength="2147483647"
                                maxBytesPerRead="214748347"
                                maxDepth="2147483647"
                                maxNameTableCharCount="2147483647"
                                maxStringContentLength="2147483647"/>
                  <security mode="None"/>
              </binding>                        
          </webHttpBinding>
      </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior name="BookServiceBehavior">
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
        <endpointBehaviors>
            <behavior name="BookServiceEndPointBehavior">
                <webHttp/>
            </behavior>
        </endpointBehaviors>
    </behaviors>
    <protocolMapping>
      <add binding="basicHttpsBinding" scheme="https"/>
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
      <services>
          <service name="WcfService4.BookService"
                   behaviorConfiguration="BookServiceBehavior">
              <endpoint address=""
                        binding="webHttpBinding"
                        contract="WcfService4.IBookService"
                        behaviorConfiguration="BookServiceEndPointBehavior"
                        bindingConfiguration="BookServiceWebHttpBinding"/>
          </service>                   
      </services>
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true"/>
  </system.webServer>
</configuration>

image