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

推荐订阅源

The GitHub Blog
The GitHub Blog
Engineering at Meta
Engineering at Meta
博客园 - 聂微东
博客园 - Franky
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
雷峰网
雷峰网
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
L
LangChain Blog
WordPress大学
WordPress大学
H
Help Net Security
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Y
Y Combinator Blog
Blog — PlanetScale
Blog — PlanetScale
MyScale Blog
MyScale Blog
IT之家
IT之家
酷 壳 – CoolShell
酷 壳 – CoolShell
罗磊的独立博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
有赞技术团队
有赞技术团队
Apple Machine Learning Research
Apple Machine Learning Research
云风的 BLOG
云风的 BLOG
博客园 - 【当耐特】
P
Proofpoint News Feed
D
DataBreaches.Net

博客园 - 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 DataTemplateSelector
FredGrit · 2026-04-27 · via 博客园 - FredGrit
Install-Package Microsoft.Xaml.Behaviors.WPF
public class IdDataTemplate : DataTemplateSelector
{
    public DataTemplate ZeroTemplate { get; set; }
    public DataTemplate FirstTemplate { get; set; }
    public DataTemplate SecondTemplate { get; set; }
    public DataTemplate ThirdTemplate { get; set; }
    public DataTemplate ForthTemplate { get; set; }
    public DataTemplate FifthTemplate { get; set; }

    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {
        if (item is Book bk)
        {
            switch (bk.Id % 6)
            {
                case 0:
                    return ZeroTemplate;
                case 1:
                    return FirstTemplate;
                case 2:
                    return SecondTemplate;
                case 3:
                    return ThirdTemplate;
                case 4:
                    return ForthTemplate;
                case 5:
                    return FifthTemplate;
            }
        }

        return base.SelectTemplate(item, container);
    }
}
using System.Collections.ObjectModel;
using System.ComponentModel;
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.Windows.Threading;

namespace WpfApp23
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }
    public class MainVM : INotifyPropertyChanged
    {
        public MainVM()
        {
            MainTitle = $"loading...";
            InitTimer();
            BooksCollection = new ObservableCollection<Book>();
            for (int i = 1; i < 10000001; i++)
            {
                BooksCollection.Add(new Book()
                {
                    Id = i,
                    Name = $"Name_{i}",
                    ISBN = $"ISBN_{i}",
                    Author = $"Author_{i}"
                });
            }
        }

        private void InitTimer()
        {
            DispatcherTimer tmr = new DispatcherTimer();
            tmr.Interval = TimeSpan.FromSeconds(1);
            tmr.Tick += Tmr_Tick;
            tmr.Start();            
        }

        private void Tmr_Tick(object? sender, EventArgs e)
        {
            MainTitle = $"{DateTime.Now}";
        }
        
        private string mainTitle;
        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 = PropertyChanged;
            handler?.Invoke(this, new PropertyChangedEventArgs(propName));
        }
    }

    public class IdDataTemplate : DataTemplateSelector
    {
        public DataTemplate ZeroTemplate { get; set; }
        public DataTemplate FirstTemplate { get; set; }
        public DataTemplate SecondTemplate { get; set; }
        public DataTemplate ThirdTemplate { get; set; }
        public DataTemplate ForthTemplate { get; set; }
        public DataTemplate FifthTemplate { get; set; }

        public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {
            if (item is Book bk)
            {
                switch (bk.Id % 6)
                {
                    case 0:
                        return ZeroTemplate;
                    case 1:
                        return FirstTemplate;
                    case 2:
                        return SecondTemplate;
                    case 3:
                        return ThirdTemplate;
                    case 4:
                        return ForthTemplate;
                    case 5:
                        return FifthTemplate;
                }
            }

            return base.SelectTemplate(item, container);
        }
    }

    public class Book
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string ISBN { get; set; }
        public string Author { get; set; }

        public override string ToString()
        {
            return $"Id:{Id},Name:{Name},ISBN:{ISBN},Author:{Author}";
        }
    }
}


<Window x:Class="WpfApp23.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:WpfApp23"
        mc:Ignorable="d"
        Title="{Binding MainTitle}" WindowState="Maximized">
    <Window.Resources>
        <DataTemplate x:Key="ZeroTemplate">
            <TextBlock Text="{Binding}" Foreground="Red"  />
        </DataTemplate>

        <DataTemplate x:Key="FirstTemplate">
            <TextBlock Text="{Binding}"  Foreground="Orange"  />
        </DataTemplate>

        <DataTemplate x:Key="SecondTemplate">
            <TextBlock Text="{Binding}"  Foreground="Yellow"  />
        </DataTemplate>

        <DataTemplate x:Key="ThirdTemplate">
            <TextBlock Text="{Binding}"  Foreground="Green"  />
        </DataTemplate>

        <DataTemplate x:Key="ForthTemplate">
            <TextBlock Text="{Binding}"  Foreground="Blue"  />
        </DataTemplate>

        <DataTemplate x:Key="FifthTemplate">
            <TextBlock Text="{Binding}"  Foreground="Cyan"  />
        </DataTemplate>

        <local:IdDataTemplate x:Key="IdDataTemplate"
                              ZeroTemplate="{StaticResource ZeroTemplate}"
                              FirstTemplate="{StaticResource FirstTemplate}"
                              SecondTemplate="{StaticResource SecondTemplate}"
                              ThirdTemplate="{StaticResource ThirdTemplate}"
                              ForthTemplate="{StaticResource ForthTemplate}"
                              FifthTemplate="{StaticResource FifthTemplate}"/>
    </Window.Resources>
    <Window.DataContext>
        <local:MainVM/>
    </Window.DataContext>
    <Grid>
        <ListBox ItemsSource="{Binding BooksCollection}"
                 ItemTemplateSelector="{StaticResource IdDataTemplate}">
            <ListBox.Resources>
                <Style TargetType="ListBoxItem">
                    <Setter Property="FontSize" Value="50"/>
                    <Style.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="FontSize" Value="80"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </ListBox.Resources>
        </ListBox>
    </Grid>
</Window>

image

image

image

posted @ 2026-04-27 19:19  FredGrit  阅读(0)  评论()    收藏  举报