wpf 实现QQ分组效果 代码简单

效果

wpf XMAL 重要的是样式

<Window x:Class="TestWPF.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:TestWPF"
        mc:Ignorable="d"
        Background="#d3dee8"
        Title="MainWindow" Height="450" Width="206">
    <Window.Resources>
        <Style x:Key="ExpanderDownHeaderStyle" TargetType="{x:Type ToggleButton}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ToggleButton}">
                        <Border Padding="{TemplateBinding Padding}">
                            <Grid x:Name="bk" Background="Transparent" Height="35" SnapsToDevicePixels="False">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="35"/>
                                    <ColumnDefinition Width="*"/>
                                </Grid.ColumnDefinitions>
                                <Path x:Name="arrow" Data="M 3,3 L 8,8 L 3,13" 
                                      HorizontalAlignment="Center" SnapsToDevicePixels="false" Stroke="DarkGray" 
                                      StrokeThickness="2" VerticalAlignment="Center"/>
                                <ContentPresenter Grid.Column="1" HorizontalAlignment="Left" Margin="4,0,0,0" RecognizesAccessKey="True" SnapsToDevicePixels="True" VerticalAlignment="Center"/>
                            </Grid>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsChecked" Value="true">
                                <Setter Property="Data" TargetName="arrow" Value="M 3,3  L 8,8  L 13,3"/>
                            </Trigger>
                            <Trigger Property="IsMouseOver" Value="true">
                                <Setter Property="Background" TargetName="bk" Value="#20333333"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <Style x:Key="ExpanderStyle1" TargetType="{x:Type Expander}">
            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
            <Setter Property="Background" Value="Transparent"/>
            <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
            <Setter Property="VerticalContentAlignment" Value="Stretch"/>
            <Setter Property="BorderBrush" Value="Transparent"/>
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Expander}">
                        <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" CornerRadius="3" SnapsToDevicePixels="true">
                            <DockPanel>
                                <ToggleButton x:Name="HeaderSite" ContentTemplate="{TemplateBinding HeaderTemplate}" 
                                              ContentTemplateSelector="{TemplateBinding HeaderTemplateSelector}" 
                                              DockPanel.Dock="Top" 
                                              Content="{TemplateBinding Header}" 
                                              Foreground="{TemplateBinding Foreground}" 
                                              FontWeight="{TemplateBinding FontWeight}" 
                                              FontStyle="{TemplateBinding FontStyle}" 
                                              FontStretch="{TemplateBinding FontStretch}" 
                                              FontSize="{TemplateBinding FontSize}" 
                                              FontFamily="{TemplateBinding FontFamily}" 
                                              HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" 
                                              IsChecked="{Binding IsExpanded, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" 
                                              Margin="1" MinWidth="0" MinHeight="0" Padding="{TemplateBinding Padding}" 
                                              Style="{StaticResource ExpanderDownHeaderStyle}" 
                                              VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/>
                                <ContentPresenter x:Name="ExpandSite" DockPanel.Dock="Bottom" Focusable="false" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" Visibility="Collapsed" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                            </DockPanel>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsExpanded" Value="true">
                                <Setter Property="Visibility" TargetName="ExpandSite" Value="Visible"/>
                            </Trigger>
                            <Trigger Property="IsEnabled" Value="false">
                                <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <Style TargetType="{x:Type ScrollViewer}">
            <Setter Property="OverridesDefaultStyle" Value="True"/>
            <Setter Property="CanContentScroll" Value="True"></Setter>
            <Setter Property="HorizontalScrollBarVisibility" Value="Hidden"/>

            <Style.Resources>
                <SolidColorBrush x:Key="NormalBorderBrush" Color="#FFFF0000" />
                <SolidColorBrush x:Key="HorizontalNormalBrush" Color="#FFFF0000" />
                <SolidColorBrush x:Key="HorizontalNormalBorderBrush" Color="#FFFF0000" />
                <SolidColorBrush x:Key="DisabledForegroundBrush" Color="#FFFF0000" />
                <SolidColorBrush x:Key="NormalBrush" Color="Transparent" />
                <!--滚动条样式-->
                <Style x:Key="ScrollBarThumb" TargetType="{x:Type Thumb}">
                    <Setter Property="SnapsToDevicePixels" Value="True"/>
                    <Setter Property="OverridesDefaultStyle" Value="true"/>
                    <Setter Property="IsTabStop" Value="false"/>
                    <Setter Property="Width" Value="8"/>
                    <Setter Property="Focusable" Value="false"/>
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type Thumb}">
                                <!-- 滑块正常颜色  FF428BCA  FF00FFFF -->
                                <Border x:Name="border"  Background="#FF428BCA" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="0" />
                                <ControlTemplate.Triggers>
                                    <Trigger Property="IsMouseOver" Value="True">
                                        <!-- 滑块鼠标移上去颜色80FFFFFF  -->
                                        <Setter Property="Background" TargetName="border" Value="#FF1F7AC9"></Setter>
                                    </Trigger>
                                </ControlTemplate.Triggers>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>


                <ControlTemplate x:Key="VerticalScrollBar" TargetType="{x:Type ScrollBar}">
                    <Grid >
                        <!-- 滑块背景颜色-->
                        <Border CornerRadius="2"  Background="Transparent"  BorderThickness="0" BorderBrush="#D6D4D4" />
                        <Track Name="PART_Track" IsDirectionReversed="true">
                            <Track.Thumb>
                                <Thumb Style="{StaticResource ScrollBarThumb}" 
                                   Margin="1,0,1,0" Background="{StaticResource HorizontalNormalBrush}" 
                                   BorderBrush="{StaticResource HorizontalNormalBorderBrush}" />
                            </Track.Thumb>
                        </Track>

                    </Grid>
                </ControlTemplate>

                <Style x:Key="HScrollBarThumb" TargetType="{x:Type Thumb}">
                    <Setter Property="SnapsToDevicePixels" Value="True"/>
                    <Setter Property="OverridesDefaultStyle" Value="true"/>
                    <Setter Property="IsTabStop" Value="false"/>
                    <Setter Property="Height" Value="9"/>
                    <Setter Property="HorizontalAlignment" Value="Stretch"/>
                    <Setter Property="Focusable" Value="false"/>
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type Thumb}">
                                <!-- 滑块正常颜色-->
                                <Border  x:Name="border" CornerRadius="0" Background="#FF428BCA" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="0" />
                                <ControlTemplate.Triggers>
                                    <Trigger Property="IsMouseOver" Value="True">
                                        <!-- 滑块鼠标移上去颜色-->
                                        <Setter Property="Background" TargetName="border" Value="#FF1F7AC9"></Setter>
                                    </Trigger>
                                </ControlTemplate.Triggers>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>

                <ControlTemplate x:Key="HorizontalScrollBar" TargetType="{x:Type ScrollBar}">
                    <Grid >
                        <!-- 滑块背景颜色-->
                        <Border  Grid.ColumnSpan="3"  CornerRadius="2" Background="Transparent"  BorderThickness="0" BorderBrush="#D6D4D4" />
                        <Track Name="PART_Track" IsDirectionReversed="False">
                            <Track.Thumb>
                                <Thumb Style="{StaticResource HScrollBarThumb}" Margin="0,1,0,1" Background="{StaticResource NormalBrush}" BorderBrush="{StaticResource NormalBorderBrush}" />
                            </Track.Thumb>
                        </Track>
                    </Grid>
                </ControlTemplate>

                <!--滚动条整体样式-->
                <Style x:Key="{x:Type ScrollBar}" TargetType="{x:Type ScrollBar}">
                    <Setter Property="SnapsToDevicePixels" Value="True"/>
                    <Setter Property="Background" Value="Transparent"/>
                    <Setter Property="OverridesDefaultStyle" Value="true"/>
                    <Style.Triggers>
                        <Trigger Property="Orientation" Value="Horizontal">
                            <Setter Property="Width" Value="Auto"/>
                            <Setter Property="Height" Value="8" />
                            <Setter Property="Template" Value="{StaticResource HorizontalScrollBar}" />
                        </Trigger>
                        <Trigger Property="Orientation" Value="Vertical">
                            <Setter Property="Width" Value="8"/>
                            <Setter Property="Height" Value="Auto" />
                            <Setter Property="Template" Value="{StaticResource VerticalScrollBar}" />
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </Style.Resources>
        </Style>
    </Window.Resources>
    <Grid>
        <ScrollViewer VerticalScrollBarVisibility="Visible">
            <ItemsControl ItemsSource="{Binding friendGroups}">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel></StackPanel>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Expander Header="{Binding Name, UpdateSourceTrigger=PropertyChanged}"
                                  Style="{StaticResource ExpanderStyle1}"
                                  HorizontalAlignment="Stretch"  VerticalAlignment="Top" >
                            <Grid Background="#00000000">
                                <ItemsControl ItemsSource="{Binding Friends}">
                                    <ItemsControl.ItemsPanel>
                                        <ItemsPanelTemplate>
                                            <StackPanel></StackPanel>
                                        </ItemsPanelTemplate>
                                    </ItemsControl.ItemsPanel>
                                    <ItemsControl.ItemTemplate>
                                        <DataTemplate>
                                            <Grid x:Name="bk" Height="60" HorizontalAlignment="Stretch" Margin="5,5,5,5">
                                                <Grid.ColumnDefinitions>
                                                    <ColumnDefinition Width="68"/>
                                                    <ColumnDefinition Width="80*"/>
                                                </Grid.ColumnDefinitions>
                                                <Rectangle Fill="Chocolate" HorizontalAlignment="Center" 
                                                           VerticalAlignment="Center" Width="40" Height="40" Grid.Column="0"></Rectangle>
                                                <Label Content="{Binding Name}" Margin="0,5,0,0" Grid.Column="1" />
                                            </Grid>
                                            <DataTemplate.Triggers>
                                                <Trigger Property="IsMouseOver" Value="true">
                                                    <Setter Property="Background" TargetName="bk" Value="#15333333"/>
                                                </Trigger>
                                            </DataTemplate.Triggers>
                                        </DataTemplate>
                                    </ItemsControl.ItemTemplate>
                                </ItemsControl>
                            </Grid>
                        </Expander>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </ScrollViewer>
    </Grid>
</Window>

wpf cs后台

using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;

namespace TestWPF
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = this;
            friendGroups = new ObservableCollection<FriendGroup>();
            friendGroups.Add(new FriendGroup() { Name = "不认识的" });
            friendGroups.Add(new FriendGroup() { Name = "同学" });
            friendGroups.Add(new FriendGroup() { Name = "同事" });
            friendGroups.Add(new FriendGroup() { Name = "室友" });
            friendGroups.Add(new FriendGroup() { Name = "老外" });
            friendGroups.Add(new FriendGroup() { Name = "陌生人" });
        }

        public ObservableCollection<FriendGroup> friendGroups
        {
            get
            {
                return _friends;
            }
            set
            {
                _friends = value;
                RaisePropertyChanged("FriendGroup");
            }
        }
        private ObservableCollection<FriendGroup> _friends { get; set; }
        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
        #region Methods
        private void RaisePropertyChanged(string propertyName)
        {
            // take a copy to prevent thread issues
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion
    }


    public class FriendEntity : INotifyPropertyChanged
    {
        public String Name { get; set; }
        public String Nick { get; set; }


        #region INotifyPropertyChanged Members
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
        #region Methods
        private void RaisePropertyChanged(string propertyName)
        {
            // take a copy to prevent thread issues
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion
    }




    public class FriendGroup : INotifyPropertyChanged
    {
        public String Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
                RaisePropertyChanged("Name");
            }
        }
        private String _name { get; set; }

        public FriendGroup()
        {
            Friends = new ObservableCollection<FriendEntity>();
            Friends.Add(new FriendEntity() { Name = "小明" });
            Friends.Add(new FriendEntity() { Name = "小红" });
            Friends.Add(new FriendEntity() { Name = "小兰" });
            Friends.Add(new FriendEntity() { Name = "柱子" });

        }


        public ObservableCollection<FriendEntity> Friends
        {
            get
            {
                return _friends;
            }
            set
            {
                _friends = value;
                RaisePropertyChanged("Friends");
            }
        }
        private ObservableCollection<FriendEntity> _friends { get; set; }


        #region INotifyPropertyChanged Members



        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
        #region Methods
        private void RaisePropertyChanged(string propertyName)
        {
            // take a copy to prevent thread issues
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值