首页 > 其他分享 >WPF DataTemplate DataTemplateSelector

WPF DataTemplate DataTemplateSelector

时间:2024-04-30 19:55:44浏览次数:23  
标签:Windows System MainWindow public using var DataTemplateSelector WPF DataTemplate

//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;
        }
    }
}

 

标签:Windows,System,MainWindow,public,using,var,DataTemplateSelector,WPF,DataTemplate
From: https://www.cnblogs.com/Fred1987/p/18168606

相关文章

  • WPF DataTemplate DataType
    //xaml<Windowx:Class="WpfApp77.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.mic......
  • WPF SetProperty to implement compare,assign and notify
    protectedvoidSetProperty<T>(refTfield,Tvalue,[CallerMemberName]stringpropName=null){if(!EqualityComparer<T>.Default.Equals(field,value)){field=value;varhandler=PropertyChanged;if(handler!=nul......
  • WPF MVVM Datagrid Selected Multiple items via behavior interaction.trigger,event
    1.Install Microsoft.Xaml.Behaviors.WpffromNuget;2.Addbehaviorreferenceinxamlxmlns:behavior="http://schemas.microsoft.com/xaml/behaviors"3.Passmethodtomvvmviabehavior,interaction,trigger,eventname,TargetObject,MethodNameinxaml......
  • WPF 后台设置DataGrid选中多行
    1privatevoidSetSelectIndex(List<int>listIndex)2{3try4{5foreach(variinlistIndex)6{7if(i>=datagridSig.Items.Count)8......
  • WPF pass event method to viewmodel via Interaction:CallMethodAction,TargetObject
    <Windowx:Class="WpfApp71.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.......
  • 【转】[WPF] 复制文本到剪贴板
    来自:阿里的通义灵码以下是几种常见的复制数据类型到剪切板的方法:复制文本到剪切板usingSystem.Windows.Forms;//对于WindowsForms应用//或者usingSystem.Windows;//对于WPF应用publicvoidCopyTextToClipboard(stringtext){//确保在UI线程中操作剪切板......
  • WPF & Prism
    WPF编程-Prism世有伯乐,然后有千里马。千里马常有,而伯乐不常有。一、背景Winform和WPF1.WinForms和WPF技术架构:WinForms是基于传统的窗体和控件的技术,使用的是类似于VB6时代的设计理念。WPF是基于XAML(可扩展应用程序标记语言)的技术,允许更灵活、高度可定制化......
  • WPF所有原生空间使用demo
    //前台窗体<Windowx:Class="WpfTestDemo.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.......
  • SignalR服务端嵌入到WPF
    用的是.netframework4.7.2的WPF。<Windowx:Class="EBServerTry.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:......
  • dotnet 简单方法在一个进程内同时跑起 WPF 和 ASP.NET Core 框架
    从设计架构上,无论是WPF还是ASP.NETCore框架,都是在dotnet运行时上层的应用,两个框架处于平级的结构。理论上讲,两个平级的框架只要不存在特殊的情况,都是能够相容存在的。本文将和大家介绍一个非常简单的方法,在一个进程内同时跑起WPF和ASP.NETCore框架在一个进程内同时跑......