先看效果圖:這個是摺疊特效。
express
代碼結構:ide
model是咱們的數據模型,定義了在列表顯示的人物名稱 圖片 簡介 。spa
Resource是咱們的圖片資源 和 存儲圖片資源路徑、名稱的資源文件。3d
GroupListCreate是咱們的主要建立列表的模塊。code
代碼以下:orm
public StackPanel CreateModuleEntry(List<GroupPerson> hrvGroups) { if (hrvGroups != null) { StackPanel spl = new StackPanel(); //建立一個最外圍的框 List<GroupPerson> groups = hrvGroups; foreach (GroupPerson g in groups) //根據分組去遍歷 { int count = 1; Expander ex = new Expander(); //每個分組 ex.Header = g.Name; List<Person> modules = new List<Person>(); modules = g.Persons; StackPanel sp = new StackPanel(); //每個分組的框 foreach (Person m in modules) { DockPanel dp = new DockPanel(); if (count >= 2) { Thickness dpMargin = new Thickness(0, 10, 0, 0); dp.Margin = dpMargin; } dp.MouseLeave += new System.Windows.Input.MouseEventHandler(dp_MouseLeave); dp.MouseEnter += new System.Windows.Input.MouseEventHandler(dp_MouseEnter); dp.MouseLeftButtonDown += new System.Windows.Input.MouseButtonEventHandler(dp_MouseLeftButtonDown); dp.MouseLeftButtonUp += new System.Windows.Input.MouseButtonEventHandler(dp_MouseLeftButtonUp); //人物頭像 Bitmap bm = new Bitmap(ImagePathResource.ImageSource+m.Ico); MemoryStream ms = new MemoryStream(); bm.Save(ms, System.Drawing.Imaging.ImageFormat.Png); BitmapImage bi = new BitmapImage(); bi.BeginInit(); bi.StreamSource = new MemoryStream(ms.ToArray()); bi.EndInit(); ms.Dispose(); System.Windows.Controls.Image image = new System.Windows.Controls.Image(); Thickness imageMargin = new Thickness(0, 0, 0, 0); image.Margin = imageMargin; image.Source = bi; image.Width = 50; image.Height = 50; image.Stretch = Stretch.Fill; //人物名稱 Label lblName = new Label(); Thickness lblNameMargin = new Thickness(15, 0, 0, 0); lblName.Margin = lblNameMargin; lblName.Height = 25; lblName.Content = m.Name; lblName.FontSize = 13; lblName.FontWeight = FontWeights.Bold; //人物說明 WrapPanel dpl = new WrapPanel(); Thickness t3 = new Thickness(15, 0, 0, 0); dpl.Margin = t3; //說明文字的自動換行 for (int i = 0; i < m.Introduction.Length; i++) { string s = m.Introduction.Substring(i, 1); Label lblResume = new Label(); lblResume.Content = s; dpl.Children.Add(lblResume); } dpl.ItemHeight = 18; dp.Children.Add(image); dp.Children.Add(lblName); dp.Children.Add(dpl); DockPanel.SetDock(image, Dock.Left); DockPanel.SetDock(lblName, Dock.Top); sp.Children.Add(dp); count++; } ex.Content = sp; spl.Children.Add(ex); } return spl; } return null; }
這裏咱們直接返回一個stackpanel用來在界面展現,這樣自動創捷列表基本上就完成了。
裏面的四個事件是用來作鼠標浮動其上和按下的效果xml
void dp_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e) { dp = sender as DockPanel; dp.Background = System.Windows.Media.Brushes.LightGray; } DockPanel dp = null; void dp_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e) { dp = sender as DockPanel; dp.Background = System.Windows.Media.Brushes.LightGray; } void dp_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e) { dp = sender as DockPanel; dp.Background = System.Windows.Media.Brushes.Gray; } void dp_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e) { dp = sender as DockPanel; dp.Background = System.Windows.Media.Brushes.Transparent; }
剩下的工做就是怎樣去顯示 和 顯示出比較炫的效果了。blog
先說說顯示,顯示主要就在PersonView.xaml文件裏面,這是一個UserControl;事件
這個很簡單 首先在PersonView.xaml裏面用一個Grid,裏面再放一個ScrollViewer再加上咱們的 StackPanel就大功告成了!圖片
代碼以下:
<UserControl x:Class="WpfApplication2.PersonView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" Background="Black" d:DesignHeight="300" d:DesignWidth="250" Loaded="UserControl_Loaded"> <Grid Name="gd"> <ScrollViewer Name="sv" Width="250" Height="300" VerticalScrollBarVisibility="Auto"> <StackPanel Name="sp"> </StackPanel> </ScrollViewer> </Grid> </UserControl>
PersonView.xaml.cs文件代碼:
private void UserControl_Loaded(object sender, RoutedEventArgs e) { List<GroupPerson> gps = GetListGroup(); GroupListCreate glc = new GroupListCreate(); sp = glc.CreateModuleEntry(gps); sv.Content = sp; }
關於GetListGroup 我就不寫了這個就是根據咱們定義的數據模型去給初始化一下。
數據模型和Style我就不貼了 數據模型看我建立的列表就能看出來, Style代碼實在太多。
須要完整代碼的朋友請加羣
C#/WPF技術交流羣: 94234450
無論你遇到了什麼問題,咱們都不會讓你獨自去面對!