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

推荐订阅源

MongoDB | Blog
MongoDB | Blog
B
Blog
Y
Y Combinator Blog
大猫的无限游戏
大猫的无限游戏
aimingoo的专栏
aimingoo的专栏
B
Blog RSS Feed
博客园 - Franky
V
V2EX
IT之家
IT之家
WordPress大学
WordPress大学
博客园 - 三生石上(FineUI控件)
J
Java Code Geeks
F
Fortinet All Blogs
I
InfoQ
云风的 BLOG
云风的 BLOG
腾讯CDC
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
月光博客
月光博客
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
N
Netflix TechBlog - Medium
宝玉的分享
宝玉的分享
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
P
Proofpoint News Feed
Microsoft Security Blog
Microsoft Security Blog

博客园 - 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)  评论()    收藏  举报