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

推荐订阅源

C
Check Point Blog
罗磊的独立博客
量子位
Microsoft Azure Blog
Microsoft Azure Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
J
Java Code Geeks
M
MIT News - Artificial intelligence
月光博客
月光博客
IT之家
IT之家
D
DataBreaches.Net
A
About on SuperTechFans
博客园 - 三生石上(FineUI控件)
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Last Week in AI
Last Week in AI
D
Docker
The GitHub Blog
The GitHub Blog
B
Blog
V
Visual Studio Blog
博客园 - Franky
N
Netflix TechBlog - Medium
博客园 - 【当耐特】
Martin Fowler
Martin Fowler
博客园 - 聂微东
U
Unit 42

博客园 - 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 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 ContextMenu,MenuItem command can binding to dataconte...
FredGrit · 2026-06-20 · via 博客园 - FredGrit
 <DataGrid.ContextMenu>
     <ContextMenu>
         <MenuItem Header="{Binding FirstMIHeader}"
                   FontSize="50"
                   Width="350"
                   Height="150"
                   Command="{Binding FirstCmd}"
                   CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=ContextMenu},
             Path=PlacementTarget.SelectedItems}"/>
     </ContextMenu>
 </DataGrid.ContextMenu>

private DelCmd firstCmd;
public DelCmd FirstCmd
{
    get
    {
        if (firstCmd == null)
        {
            firstCmd = new DelCmd(FirstCmdExecuted);
        }
        return firstCmd;
    }
}

private void FirstCmdExecuted(object? obj)
{
    _ = Task.Run(async() =>
    {
        var items = ((System.Collections.IList)obj).Cast<Book>()?.ToList();
        if (items != null && items.Any())
        {
            string jsonStr = JsonConvert.SerializeObject(items, Formatting.Indented);
            string jsonFile = string.Empty;

            await Application.Current?.Dispatcher?.InvokeAsync(() =>
            {
                SaveFileDialog dlg = new SaveFileDialog();
                dlg.Filter = $"Json Files|*.json";
                dlg.FileName = $"Json_{DateTime.Now.ToString("yyyyMMddHHmmssffff")}.json";
                if (dlg.ShowDialog() == true)
                {
                    jsonFile = dlg.FileName;
                }
            });

            using(StreamWriter jsonWriter=new StreamWriter(jsonFile,false,Encoding.UTF8))
            {
                jsonWriter.WriteLine(jsonStr);
            }

            await Application.Current?.Dispatcher?.InvokeAsync(() =>
            {
                MessageBox.Show($"Saved {items.Count} items in {jsonFile}");
            },System.Windows.Threading.DispatcherPriority.Background);
        }
    });            
}

private string firstMIHeader="Save In Json";
public string FirstMIHeader
{
    get
    {
        return firstMIHeader;
    }
    set
    {
        if (value != firstMIHeader)
        {
            firstMIHeader = value;
            OnPropertyChanged();
        }
    }
}
Install-Package Newtonsoft.json
<Window x:Class="WpfApp10.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:WpfApp10"
        mc:Ignorable="d"
        Title="{Binding MainTitle}" WindowState="Maximized">
    <Window.DataContext>
        <local:MainVM/>
    </Window.DataContext>
    <Grid>
        <DataGrid ItemsSource="{Binding BooksCollection}"
                  VirtualizingPanel.IsVirtualizing="True"
                  VirtualizingPanel.VirtualizationMode="Recycling"
                  VirtualizingPanel.CacheLength="5,5"
                  VirtualizingPanel.CacheLengthUnit="Item"
                  ScrollViewer.IsDeferredScrollingEnabled="True"
                  ScrollViewer.CanContentScroll="True"
                  UseLayoutRounding="True"
                  SnapsToDevicePixels="True"
                  CanUserAddRows="False"
                  AutoGenerateColumns="True"
                  EnableRowVirtualization="True"
                  EnableColumnVirtualization="True">
            <DataGrid.Resources>
                <Style TargetType="DataGridRow">
                    <Setter Property="FontSize" Value="30"/>
                    <Style.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="FontSize" Value="40"/>
                            <Setter Property="Foreground" Value="Red"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </DataGrid.Resources>
            <DataGrid.ContextMenu>
                <ContextMenu>
                    <MenuItem Header="{Binding FirstMIHeader}"
                              FontSize="50"
                              Width="350"
                              Height="150"
                              Command="{Binding FirstCmd}"
                              CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=ContextMenu},
                        Path=PlacementTarget.SelectedItems}"/>
                </ContextMenu>
            </DataGrid.ContextMenu>
        </DataGrid>
    </Grid>
</Window>


using Microsoft.Win32;
using Newtonsoft.Json;
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;

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

    public class MainVM : INotifyPropertyChanged
    {
        string originUrl = "http://localhost:51537/BookService.svc/getbookslist?cnt=";
        static readonly HttpClient client = new HttpClient();
        private readonly object isLoadingLock = new object();
        private readonly SemaphoreSlim semaphoreThrottle = new SemaphoreSlim(10, 10);

        public MainVM()
        {
            if (DesignerProperties.GetIsInDesignMode(new DependencyObject()))
            {
                return;
            }
            _ = Task.Run(async () =>
            {
                while (true)
                {
                    await InitBooksCollectionAsync();
                    await Task.Delay(20000);
                }
            });

        }
        private async Task InitBooksCollectionAsync(int cnt = 1000000)
        {
            if (!await semaphoreThrottle.WaitAsync(20000))
            {
                return;
            }
            if (IsLoading)
            {
                return;
            }

            IsLoading = true;

            try
            {
                await Application.Current?.Dispatcher?.InvokeAsync(() =>
                {
                    MainTitle = $"{DateTime.Now},loading...";
                }, System.Windows.Threading.DispatcherPriority.Background);

                string requestUrl = $"{originUrl}{cnt}";
                await Task.Run(async () =>
                {
                    string jsonStr = await client.GetStringAsync(requestUrl);
                    if (string.IsNullOrWhiteSpace(jsonStr))
                    {
                        return;
                    }

                    List<Book>? bks = JsonConvert.DeserializeObject<List<Book>>(jsonStr);
                    if (bks != null && bks.Any())
                    {
                        await Application.Current?.Dispatcher?.InvokeAsync(() =>
                        {
                            BooksCollection = new ObservableCollection<Book>(bks);
                            MainTitle = $"{DateTime.Now},FirstID:{BooksCollection.FirstOrDefault()?.Id},LastID:{BooksCollection.LastOrDefault()?.Id}";
                        }, System.Windows.Threading.DispatcherPriority.Background);
                    }
                });
            }
            catch (Exception ex)
            {
                MessageBox.Show($"{DateTime.Now},exception message {ex?.Message}");
            }
            finally
            {
                IsLoading = false;
                semaphoreThrottle.Release();
            }
        }

        private DelCmd firstCmd;
        public DelCmd FirstCmd
        {
            get
            {
                if (firstCmd == null)
                {
                    firstCmd = new DelCmd(FirstCmdExecuted);
                }
                return firstCmd;
            }
        }

        private void FirstCmdExecuted(object? obj)
        {
            _ = Task.Run(async() =>
            {
                var items = ((System.Collections.IList)obj).Cast<Book>()?.ToList();
                if (items != null && items.Any())
                {
                    string jsonStr = JsonConvert.SerializeObject(items, Formatting.Indented);
                    string jsonFile = string.Empty;

                    await Application.Current?.Dispatcher?.InvokeAsync(() =>
                    {
                        SaveFileDialog dlg = new SaveFileDialog();
                        dlg.Filter = $"Json Files|*.json";
                        dlg.FileName = $"Json_{DateTime.Now.ToString("yyyyMMddHHmmssffff")}.json";
                        if (dlg.ShowDialog() == true)
                        {
                            jsonFile = dlg.FileName;
                        }
                    });

                    using(StreamWriter jsonWriter=new StreamWriter(jsonFile,false,Encoding.UTF8))
                    {
                        jsonWriter.WriteLine(jsonStr);
                    }

                    await Application.Current?.Dispatcher?.InvokeAsync(() =>
                    {
                        MessageBox.Show($"Saved {items.Count} items in {jsonFile}");
                    },System.Windows.Threading.DispatcherPriority.Background);
                }
            });            
        }

        private string firstMIHeader="Save In Json";
        public string FirstMIHeader
        {
            get
            {
                return firstMIHeader;
            }
            set
            {
                if (value != firstMIHeader)
                {
                    firstMIHeader = value;
                    OnPropertyChanged();
                }
            }
        }

        private bool isLoading = false;
        public bool IsLoading
        {
            get
            {
                lock (isLoadingLock)
                {
                    return isLoading;
                }
            }
            set
            {
                lock (isLoadingLock)
                {
                    if (value != isLoading)
                    {
                        isLoading = value;
                        OnPropertyChanged();
                    }
                }
            }
        }

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

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

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

    public class DelCmd : ICommand
    {
        private readonly Action<object?> execute;
        private readonly Func<object?, bool>? canExecute;
        public DelCmd(Action<object?> executeValue, Func<object?, bool>? canExecuteValue = null)
        {
            execute = executeValue;
            canExecute = canExecuteValue;
        }

        public event EventHandler? CanExecuteChanged;

        public bool CanExecute(object? parameter)
        {
            return canExecute == null ? true : canExecute(parameter);
        }

        public void Execute(object? parameter)
        {
            if (!CanExecute(parameter))
            {
                return;
            }
            execute?.Invoke(parameter);
        }

        public void RaiseCanExecuteChanged(object? parameter)
        {
            var handler = Volatile.Read(ref CanExecuteChanged);
            if (handler == null)
            {
                return;
            }

            if (Application.Current?.Dispatcher?.CheckAccess() == true)
            {
                handler?.Invoke(parameter, EventArgs.Empty);
            }
            else
            {
                Application.Current?.Dispatcher?.Invoke(() =>
                {
                    handler?.Invoke(parameter, EventArgs.Empty);
                });
            }
        }
    }

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

}

image

image