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

推荐订阅源

奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园_首页
大猫的无限游戏
大猫的无限游戏
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Apple Machine Learning Research
Apple Machine Learning Research
B
Blog
B
Blog RSS Feed
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
P
Proofpoint News Feed
MyScale Blog
MyScale Blog
Engineering at Meta
Engineering at Meta
量子位
H
Hackread – Cybersecurity News, Data Breaches, AI and More
T
Tailwind CSS Blog
Stack Overflow Blog
Stack Overflow Blog
N
Netflix TechBlog - Medium
T
The Blog of Author Tim Ferriss
U
Unit 42
aimingoo的专栏
aimingoo的专栏
博客园 - 叶小钗
博客园 - 【当耐特】
云风的 BLOG
云风的 BLOG
博客园 - Franky
博客园 - 聂微东

博客园 - 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 Microsoft.Xaml.Behaviors.WPF, EventTrigger EventName=...
FredGrit · 2026-06-08 · via 博客园 - FredGrit
    <behavior:Interaction.Triggers>
        <behavior:EventTrigger EventName="PreviewMouseDown">
            <behavior:InvokeCommandAction Command="{Binding PreviewMouseDownCmd}"/>
        </behavior:EventTrigger>
    </behavior:Interaction.Triggers>
private DelCmd previewMouseDownCmd;
public DelCmd PreviewMouseDownCmd
{
    get
    {
        if(previewMouseDownCmd==null)
        {
            previewMouseDownCmd = new DelCmd(PreviewMouseDownCmdExecuted, PreviewMouseDownCmdCanExecute);
        }
        return previewMouseDownCmd;
    }
}

private bool PreviewMouseDownCmdCanExecute(object? arg)
{
    return IsLoading == true ? false : true;
}

private void PreviewMouseDownCmdExecuted(object? obj)
{
    _ = InitBooksCollectionAsync(20000000);
}

private async Task InitBooksCollectionAsync(int cnt = 10000000)
{
    if (IsLoading)
    {
        return;
    }
    IsLoading = true;
    if (cnt <= 0)
    {
        return;
    }

    try
    {
        BooksCollection = new ObservableCollection<Book>();
        
        int batchCount = (cnt - 1 + batchSize) / batchSize;
        for (int batch = 0; batch < batchCount; batch++)
        {
            int actualSize = Math.Min(batchSize, cnt - batch * batchSize);
            List<Book> bksList = new List<Book>(actualSize);
            await Task.Run(() =>
            {
                for (int i = 0; i < actualSize; i++)
                {
                    var a = GetIncrementId();
                    bksList.Add(new Book()
                    {
                        Id = a,
                        Name = $"Name_{a}",
                        ISBN = $"ISBN_{a}",
                        Abstract = $"Abstract_{a}",
                        Author = $"Author_{a}",
                        Comment = $"Comment_{a}",
                        Content = $"Content_{a}",
                        Summary = $"Summary_{a}",
                        Title = $"Title_{a}",
                        Topic = $"Topic_{a}"
                    });
                }
            }).ConfigureAwait(false);
            await PopulateBooksCollectionAsync(batch, bksList);
        }
    }
    finally
    {
        IsLoading = false;
    }
}

private async Task PopulateBooksCollectionAsync(int batch, List<Book> bksList)
{
    await Application.Current.Dispatcher.InvokeAsync(() =>
    {
        foreach (var bk in bksList)
        {
            BooksCollection.Add(bk);
        }
        bksList.Clear();
        MainTitle = $"{DateTime.Now}," +
        $"batch:{batch + 1}," +
        $"loaded {BooksCollection.Count} items," +
        $"First Id:{BooksCollection[0]?.Id}," +
        $"Last Id:{BooksCollection[^1]?.Id}," +
        $"{GetMem()}";
    }, System.Windows.Threading.DispatcherPriority.Background);
}
<Window x:Class="WpfApp2.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:behavior="http://schemas.microsoft.com/xaml/behaviors"
        xmlns:local="clr-namespace:WpfApp2"
        mc:Ignorable="d"
        Background="White"
        Title="{Binding MainTitle}" WindowState="Maximized">
    <Window.InputBindings>
        <MouseBinding Command="{Binding LeftMouseDownCommand}"
                      Gesture="LeftClick" />
    </Window.InputBindings>
    <Window.DataContext>
        <local:MainVM/>
    </Window.DataContext>
    <!--<behavior:Interaction.Triggers>
        <behavior:EventTrigger EventName="PreviewMouseDown">
            <behavior:InvokeCommandAction Command="{Binding PreviewMouseDownCmd}"/>
        </behavior:EventTrigger>
    </behavior:Interaction.Triggers>-->
    <Grid Background="White"
          IsHitTestVisible="True">
        <!--<Grid.InputBindings>
            <MouseBinding Command="{Binding LeftMouseDownCommand}" Gesture="LeftClick" />
        </Grid.InputBindings>-->
        <ItemsControl ItemsSource="{Binding BooksCollection}">
            <ItemsControl.Resources>
                <Style TargetType="MenuItem">
                    <Setter Property="FontSize" Value="30"/>
                    <Style.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="FontSize" Value="50"/>
                            <Setter Property="Foreground" Value="Red"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </ItemsControl.Resources>
            <ItemsControl.Template>
                <ControlTemplate>
                    <ScrollViewer CanContentScroll="True">
                        <VirtualizingStackPanel IsItemsHost="True"
                                                VirtualizingPanel.IsVirtualizing="True"
                                                VirtualizingPanel.VirtualizationMode="Recycling"
                                                VirtualizingPanel.CacheLengthUnit="Item"
                                                VirtualizingPanel.CacheLength="2,2"
                                                ScrollViewer.CanContentScroll="True"
                                                ScrollViewer.IsDeferredScrollingEnabled="True"
                                                UseLayoutRounding="True"
                                                SnapsToDevicePixels="True">                            
                        </VirtualizingStackPanel>
                    </ScrollViewer>
                </ControlTemplate>
            </ItemsControl.Template>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Grid Margin="20">
                        <Grid.InputBindings>
                            <MouseBinding Command="{Binding DataContext.LeftMouseDownCommand, 
                            RelativeSource={RelativeSource AncestorType=Window}}" 
                                      Gesture="LeftClick" />
                        </Grid.InputBindings>
                        <Grid.Resources>
                            <Style TargetType="TextBlock">
                                <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>
                        </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 ISBN}" Grid.Row="0" Grid.Column="2"/>
                        <TextBlock Text="{Binding Author}" Grid.Row="1" Grid.Column="0"/>
                        <TextBlock Text="{Binding Abstract}" Grid.Row="1" Grid.Column="1"/>
                        <TextBlock Text="{Binding Comment}" Grid.Row="1" Grid.Column="2"/>
                        <TextBlock Text="{Binding Summary}" 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>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
            <ItemsControl.ContextMenu>
                <ContextMenu>
                    <MenuItem Header="Load Data"
                              Command="{Binding LoadDataCmd}"/>
                </ContextMenu>
            </ItemsControl.ContextMenu>
        </ItemsControl>
    </Grid>
</Window>