WPF DataTemplate DataTemplateSelector

FredGrit發表於2024-04-30
//xaml
<Window x:Class="WpfApp78.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:WpfApp78"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <DataTemplate x:Key="systemTemplate">
            <Border Background="Red" BorderBrush="Black"
                    BorderThickness="11" Margin="2" Padding="4">
                <TextBlock HorizontalAlignment="Center"
                           VerticalAlignment="Center"
                           FontSize="16"
                           Text="{Binding ProcessName}"/>
            </Border>
        </DataTemplate>
        <DataTemplate x:Key="userTemplate">
            <Border Background="White" BorderBrush="Blue" BorderThickness="3" Margin="2">
                <StackPanel Orientation="Horizontal" 
                            TextBlock.FontSize="15" Margin="3">
                    <TextBlock Text="{Binding ProcessName}"/>
                    <TextBlock Margin="10,0,0,0"/>
                    <TextBlock Margin="10,0,0,0" Text="{Binding Id,StringFormat='ID:0'}"/>
                    <TextBlock Margin="10,0,0,0" Text="{Binding Threads.Count,StringFormat='Threads:0'}"/>
                </StackPanel>
            </Border>
        </DataTemplate>
        <local:ProcessTemplateSelector x:Key="_selector"
                                       SystemProcessTemplate="systemTemplate"
                                       UserProcessTemplate="userTemplate"/>
    </Window.Resources>
    <Grid MouseDown="Grid_MouseDown"> 
        <ListBox HorizontalAlignment="Stretch"
                 ItemsSource="{Binding}" SelectionChanged="ListBox_SelectionChanged"
                 ItemTemplateSelector="{StaticResource _selector}"/>
    </Grid>
</Window>


//xaml.cs
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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 WpfApp78
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = Process.GetProcesses();
            this.Loaded += MainWindow_Loaded;
        }

        public static List<Process> usersProcs = new List<Process>();
        public static List<Process> systemProcs = new List<Process>();
        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            var sysProcs = systemProcs;
            var uProcs = usersProcs;
        }

        private void Grid_MouseDown(object sender, MouseButtonEventArgs e)
        {
            var sysProcs = systemProcs;
            var uProcs = usersProcs;
        }

        private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var sysProcs = systemProcs.OrderBy(x=>x.ProcessName).ToList();
            var uProcs = usersProcs.OrderBy(x => x.ProcessName).ToList();
        }
    }

    class ProcessTemplateSelector : DataTemplateSelector
    {
       
        public string SystemProcessTemplate { get; set; }
        public string UserProcessTemplate { get; set; }
        public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {
            Process proc = item as Process;

            if (proc != null)
            {
                if (proc.SessionId == 0)
                {
                    MainWindow.systemProcs.Add(proc);
                    return ((FrameworkElement)container).FindResource(SystemProcessTemplate) as DataTemplate;
                }
                else
                {
                    MainWindow.usersProcs.Add(proc);
                    return ((FrameworkElement)container).FindResource(UserProcessTemplate) as DataTemplate;
                }
            }
            return null;
        }
    }
}

相關文章