首页 > 其他分享 >WPF GroupStyle GroupStyle.HeaderTemplate

WPF GroupStyle GroupStyle.HeaderTemplate

时间:2024-06-08 22:34:32浏览次数:31  
标签:HeaderTemplate Windows System GroupStyle var using WPF MainWindow

//xaml
<Window x:Class="WpfApp148.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:WpfApp148"
        mc:Ignorable="d" WindowState="Maximized"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <ListBox ItemsSource="{Binding}" HorizontalContentAlignment="Stretch">
            <ListBox.GroupStyle>
                <GroupStyle>
                    <GroupStyle.HeaderTemplate>
                        <DataTemplate>
                            <Border BorderBrush="Red" BorderThickness="2" 
                                    Background="White" Margin="2">
                                <TextBlock Text="{Binding Name,StringFormat=Priority:{0}}"
                                           FontSize="20" FontWeight="Bold"
                                           Margin="4"/>
                            </Border>
                        </DataTemplate>
                    </GroupStyle.HeaderTemplate>
                </GroupStyle>
            </ListBox.GroupStyle>
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <UniformGrid Rows="1" Columns="3">
                        <TextBlock Text="{Binding ProcessName,StringFormat='Name:{0}'}"/>
                        <TextBlock Text="{Binding Id,StringFormat='Id:{0}'}"/>
                        <TextBlock Text="{Binding PriorityClass,StringFormat='Priority:{0}'}"/>
                    </UniformGrid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</Window>


//cs
using System;
using System.Collections.Generic;
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;
using System.Diagnostics;

namespace WpfApp148
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent(); 
            var procs = Process.GetProcesses().Where(CanAccess).OrderByDescending(x=>x.PriorityClass);
            var view = CollectionViewSource.GetDefaultView(procs);
            view.GroupDescriptions.Add(new PropertyGroupDescription("PriorityClass"));
            DataContext = procs;
        }

        public static bool CanAccess(Process proc)
        {
            try
            {
                var handler = proc.Handle;
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}

 

 

 

 

 

 <ListBox ItemsSource="{Binding}" HorizontalContentAlignment="Stretch">
     <ListBox.GroupStyle>
         <GroupStyle>
             <GroupStyle.HeaderTemplate>
                 <DataTemplate>
                     <Border BorderBrush="Red" BorderThickness="2" 
                             Background="White" Margin="2">
                         <TextBlock Text="{Binding Name,StringFormat=Priority:{0}}"
                                    FontSize="20" FontWeight="Bold"
                                    Margin="4"/>
                     </Border>
                 </DataTemplate>
             </GroupStyle.HeaderTemplate>
         </GroupStyle>
     </ListBox.GroupStyle>
</ListBox>

 

标签:HeaderTemplate,Windows,System,GroupStyle,var,using,WPF,MainWindow
From: https://www.cnblogs.com/Fred1987/p/18239035

相关文章

  • WPF DataContext order and filter via CollectionViewSource.GetDefaultView(DataCon
    //xaml<Windowx:Class="WpfApp146.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.mi......
  • WPF CollectionViewSource.GetDefaultView ICollectionViewLiveShaping IsLiveSorting
    <Windowx:Class="WpfApp147.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 Image zoomin zoomout move
    //xaml<Windowx:Class="WpfApp145.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.mi......
  • WPF ListBox ListBox.ItemTemplate DataTemplate
    <Windowx:Class="WpfApp144.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 master detail view
    <Windowx:Class="WpfApp143.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 RepeatButton
    是一个特殊的按钮,用于在用户长按或连续点击时重复执行特定动作。它通常用于需要重复执行某个操作的场景。常用属性描述Delay用于获取或设置RepeatButton在开始重复之前被按下时等待的时间(以毫秒为单位)。该值必须为非负数。Interval用于获取或设置开始重复后重复之......
  • WPF button mouseover background change color
    <Applicationx:Class="WpfApp142.App"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:local="clr-nam......
  • 在WPF程序中如何实现主题,字体的切换
    在wpf程序中如何实现主题变换,首先我们需要先思考主题切换都更改了一些什么,无非就是字体颜色,背景颜色等一些资源当我们了解了主题变换的本质后就能够进一步去实现它,众所周知,在wpf中我们可以通过资源绑定的形式去实现颜色,字体,样式等一些资源绑定,在wpf中如果有相同的key通常会绑......
  • 【WPF】Dispatcher 与消息循环
    这一期的话题有点深奥,不过按照老周一向的作风,尽量讲一些人鬼都能懂的知识。咱们先来整个小活开开胃,这个小活其实老周在N年前写过水文的,常阅读老周水文的伙伴可能还记得。通常,咱们按照正常思路构建的应用程序,第一个启动的线程为主线程,而且还是UI线程(当然,WPF默认会创建辅助线......
  • WPF grid column resize via GridSpitter, when you can drag to enlarge or shrink t
    <Windowx:Class="WpfApp137.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft......