WPF案例 (六) 动态切换UI布局

简介: 原文:WPF案例 (六) 动态切换UI布局  这个Wpf示例对同一个界面支持以ListView或者CardView的布局方式呈现界面,使用控件ItemsControl绑定数据源,使用DataTemplate为ItemsControl分别预定义了ListView和CardView的样式,在程序运行时,可在这两种Layout之间互相切换,界面如下。
原文: WPF案例 (六) 动态切换UI布局

  这个Wpf示例对同一个界面支持以ListView或者CardView的布局方式呈现界面,使用控件ItemsControl绑定数据源,使用DataTemplate为ItemsControl分别预定义了ListView和CardView的样式,在程序运行时,可在这两种Layout之间互相切换,界面如下。源代码在这里下载

  

 为ItemsControl定义ListView UI布局的ItemTemplate,并指定MouseOver时DataTemplate的样式

img_405b18b4b6584ae338e0f6ecaf736533.gif ListView布局和样式
 1    < DataTemplate  x:Key ="listViewDataTemplate" >
 2               < Border  x:Name ="listViewBorder"  BorderThickness ="0,0,0,1"  Height ="25"
 3           BorderBrush =" {StaticResource cardViewBackgroundBrush} "  HorizontalAlignment ="Stretch" >
 4                   < StackPanel  Orientation ="Horizontal"  HorizontalAlignment ="Stretch" >
 5                       < TextBlock  VerticalAlignment ="Top"  Text =" {Binding Path=BedNo} "  
 6             TextWrapping ="Wrap"  Margin ="1,0,5,0"  Width ="20"  FontSize ="12"  Foreground ="Black"   />
 7                       < TextBlock  Text =" {Binding Path=Name} "  Grid.Row ="0"  Grid.Column ="1"  
 8             TextWrapping ="Wrap"  Margin ="1,0,5,0"   VerticalAlignment ="Top"  Foreground ="Black"   FontSize ="12"   />
 9                       < TextBlock  VerticalAlignment ="Top"  Text =" {Binding Path=Sex} "  
10              TextWrapping ="Wrap"   Margin ="1,0,5,0"  FontSize ="12"  Foreground ="Black" />
11                       < TextBlock  VerticalAlignment ="Top"  Text =" {Binding Path=BirthDay} "  
12               TextWrapping ="Wrap"  Margin ="1,0,5,0"  FontSize ="12"  Foreground ="Black" />
13                       < TextBlock  Text =" {Binding Path=Address} "  TextWrapping ="Wrap"
14                 Margin ="1,0,5,0"  FontSize ="12"  Foreground ="Black"   VerticalAlignment ="Top" />
15                       < TextBlock   Text =" {Binding Path=City} "  TextWrapping ="Wrap"  
16                 Margin ="1,0,5,0"  FontSize ="12"  Foreground ="Black"   VerticalAlignment ="Top" />
17                       < TextBlock  Text =" {Binding Path=PostCode} "  TextWrapping ="Wrap"
18                  Margin ="1,0,5,0"  FontSize ="12"  Foreground ="Black"   VerticalAlignment ="Top"  HorizontalAlignment ="Stretch" />
19                       < TextBlock   Text =" {Binding Path=HomePhoneNumber} "  TextWrapping ="Wrap"
20                  Margin ="1,0,5,0"  FontSize ="12"  Foreground ="Black"   VerticalAlignment ="Top" />     
21                   </ StackPanel >
22               </ Border >
23               < DataTemplate.Triggers >
24                   < Trigger  Property ="IsMouseOver"  Value ="True" >
25                       < Setter  Property ="Background"   TargetName ="listViewBorder"  Value =" {StaticResource cardViewBackgroundBrush} " />
26                   </ Trigger >
27               </ DataTemplate.Triggers >
28  </ DataTemplate >

  为ItemsControl定义CardView UI布局的ItemTemplate,同时定义MouseOver时DataTemplate的样式

img_405b18b4b6584ae338e0f6ecaf736533.gif CardView布局和样式
 1  < DataTemplate  x:Key ="cardViewDataTemplate"   >
 2               < Border  x:Name ="card1"  Background ="Transparent"  BorderBrush ="Transparent"  
 3             BorderThickness ="1"  CornerRadius ="10"  Margin ="10" >
 4                   < Grid  Margin ="0,0,0,0"  HorizontalAlignment ="Center"  VerticalAlignment ="Center"  
 5                    ShowGridLines ="False"  x:Name ="card"   >
 6                       < Grid.RowDefinitions >
 7                           < RowDefinition  Height ="32" />
 8                           < RowDefinition  Height ="68" />
 9                           < RowDefinition  Height ="20" />
10                           < RowDefinition  Height ="20" />
11                           < RowDefinition  Height ="20" />
12                           < RowDefinition  Height ="20" />
13                           < RowDefinition  Height ="5" />
14                       </ Grid.RowDefinitions >
15                       < Grid.ColumnDefinitions >
16                           < ColumnDefinition  Width ="100"   />
17                           < ColumnDefinition  Width ="*" />
18                       </ Grid.ColumnDefinitions >
19                       < Border  x:Name ="cardBorder"  HorizontalAlignment ="Stretch"  
20                 VerticalAlignment ="Stretch"  Width ="Auto"  Height ="Auto"  Grid.Row ="0"  
21                 Grid.Column ="0"  Grid.ColumnSpan ="2"  Grid.RowSpan ="7"   
22                 Background =" {StaticResource cardViewBackgroundBrush} "  BorderBrush ="LightGray"   
23                 BorderThickness ="1"  Margin ="0,1,0,1"  CornerRadius ="0,0,8,8" />
24                       < Border   x:Name ="cardTitle"  HorizontalAlignment ="Stretch"  VerticalAlignment ="Top"   
25                Grid.Row ="0"  Grid.Column ="0"  Grid.ColumnSpan ="2"  Width ="Auto"  Height ="32"  
26                BorderBrush =" {x:Null} "  BorderThickness ="0,0,0,0"   CornerRadius ="0,0,0,0" >
27                           < Border.Background >
28                                < ImageBrush  ImageSource ="Images\cardTitle.jpg" />
29                           </ Border.Background >
30                       </ Border >
31                       < Border  HorizontalAlignment ="Left"  Grid.Row ="0"  Grid.Column ="0"  Grid.RowSpan ="2"  
32                Width ="80"  Margin ="9"  Height ="80"  VerticalAlignment ="Top"   >
33                           < Border.BitmapEffect >
34                               < DropShadowBitmapEffect  Direction ="316"  Color ="#FFA9A9A9"  ShadowDepth ="7"  Softness ="0.075" />
35                           </ Border.BitmapEffect >
36                           < Image  Width ="Auto"  Height ="Auto"  Source ="Images\Image.gif"  Stretch ="Fill"
37                   StretchDirection ="DownOnly"  HorizontalAlignment ="Stretch"  VerticalAlignment ="Top" />
38                       </ Border >
39                       < StackPanel  Orientation ="Vertical"  Grid.Row ="1"  Grid.Column ="1"  Margin ="1" >
40                           < TextBlock  VerticalAlignment ="Top"  Text =" {Binding Path=BedNo} "  
41                  TextWrapping ="Wrap"  Margin ="1"  FontSize ="14"  Foreground ="Black"   />
42                               < Path  HorizontalAlignment ="Left"  Margin ="1"  VerticalAlignment ="Top"  Width ="176"
43                 Height ="3"  Fill ="Black"  Stretch ="Fill"  Stroke ="Black"  StrokeThickness ="2"  
44                       Data ="M202,79 C412,78 413,78 413,78"   />
45                               < TextBlock  VerticalAlignment ="Top"  Text =" {Binding Path=Sex} "  TextWrapping ="Wrap"
46                    Margin ="1"  FontSize ="12"  Foreground ="Black" />
47                               < TextBlock  VerticalAlignment ="Top"  Text =" {Binding Path=BirthDay} "  TextWrapping ="Wrap"  
48                    Margin ="1"  FontSize ="12"  Foreground ="Black" />
49                           </ StackPanel >
50                            < TextBlock  Text =" {Binding Path=Name} "  Grid.Row ="0"  Grid.Column ="1"  TextWrapping ="Wrap"
51                  Margin ="2,0,0,0"  VerticalAlignment ="Center"  Height ="22"  Foreground ="Black"  FontWeight ="Bold"  FontSize ="16"   />
52                          < StackPanel  Orientation ="Horizontal"  Grid.Row ="2"  Grid.Column ="0"  Grid.ColumnSpan ="2"  Margin ="1" >
53                                < TextBlock  Text ="家庭住址:"  TextWrapping ="Wrap"  Margin ="5,0,0,0"  FontSize ="12"  Foreground ="Black"                                 HorizontalAlignment ="Left"  VerticalAlignment ="Top" />
54                                < TextBlock  Text =" {Binding Path=Address} "  TextWrapping ="Wrap"  Margin ="1"  FontSize ="12"  
55                           Foreground ="Black"   VerticalAlignment ="Top" />
56                           </ StackPanel >
57                           < StackPanel  Orientation ="Horizontal"   Grid.Row ="3"  Grid.Column ="0"  Grid.ColumnSpan ="2"  Margin ="1" >
58                               < TextBlock  Text ="城市: "  TextWrapping ="Wrap"  Margin ="5,0,0,0"  FontSize ="12"  
59                    Foreground ="Black"  HorizontalAlignment ="Left"    VerticalAlignment ="Top" />
60                                < TextBlock   Text =" {Binding Path=City} "  TextWrapping ="Wrap"  Margin ="1"  
61                         FontSize ="12"  Foreground ="Black"   VerticalAlignment ="Top" />
62                           </ StackPanel >
63                           < StackPanel  Orientation ="Horizontal"   Grid.Row ="4"  Grid.Column ="0"  Grid.ColumnSpan ="2"  Margin ="1" >
64                               < TextBlock  Text ="邮编: "  TextWrapping ="Wrap"  Margin ="5,0,0,0"  FontSize ="12"  
65                     Foreground ="Black"  HorizontalAlignment ="Left"    VerticalAlignment ="Top" />
66                                < TextBlock  Text =" {Binding Path=PostCode} "  TextWrapping ="Wrap"  Margin ="1"  FontSize ="12"
67                    Foreground ="Black"   VerticalAlignment ="Top"  HorizontalAlignment ="Stretch" />
68                           </ StackPanel >
69                            < StackPanel  Orientation ="Horizontal"   Grid.Row ="5"  Grid.Column ="0"  Grid.ColumnSpan ="2"  Margin ="1" >
70                               < TextBlock  Text ="家庭电话: "  TextWrapping ="Wrap"  Margin ="5,0,0,0"  FontSize ="12"  
71                 Foreground ="Black"  HorizontalAlignment ="Left"   VerticalAlignment ="Top" />
72                               < TextBlock   Text =" {Binding Path=HomePhoneNumber} "  TextWrapping ="Wrap"  Margin ="1"
73                          FontSize ="12"  Foreground ="Black"   VerticalAlignment ="Top" />     
74                            </ StackPanel >
75                       </ Grid >
76               </ Border >
77               < DataTemplate.Triggers >
78                   < Trigger  Property ="IsMouseOver"  Value ="True" >
79                       < Setter  Property ="Background"   TargetName ="cardBorder"  
80                 Value =" {StaticResource cardViewMouseOverBackgroundBrush} " />
81                   </ Trigger >
82               </ DataTemplate.Triggers >
83    </ DataTemplate >

  定义运行时在ListView和CardView两种布局间切换的事件

img_405b18b4b6584ae338e0f6ecaf736533.gif 切换UI布局
 1    private   void  ViewMode_SelectionChanged( object  sender, SelectionChangedEventArgs e)
 2          {
 3               if  ( ! Selector.GetIsSelectionActive( this .ViewMode))
 4                   return ;
 5               if  (ViewMode.SelectedIndex  ==   0 )
 6              {
 7                   this .itemsControl.ClearValue(ItemsControl.StyleProperty);
 8                   this .itemsControl.ItemTemplate  =  
 9                   this .FindResource( " listViewDataTemplate " as  DataTemplate;
10              }
11               else
12              {
13                   this .itemsControl.ItemTemplate  =  
14                  this .FindResource( " cardViewDataTemplate " as  DataTemplate;
15                   this .itemsControl.Style  =   this .FindResource( " cardViewStyle " as  Style;
16              }
17          }
目录
相关文章
|
2月前
|
C# 开发者 Windows
基于Material Design风格开源、易用、强大的WPF UI控件库
基于Material Design风格开源、易用、强大的WPF UI控件库
227 0
|
19天前
鸿蒙使用 @Builder扩展出来的布局数据更新没法更新UI
鸿蒙使用 @Builder扩展出来的布局数据更新没法更新UI
47 1
|
1月前
|
存储 开发框架 JavaScript
深入探讨Flutter中动态UI构建的原理、方法以及数据驱动视图的实现技巧
【6月更文挑战第11天】Flutter是高效的跨平台移动开发框架,以其热重载、高性能渲染和丰富组件库著称。本文探讨了Flutter中动态UI构建原理与数据驱动视图的实现。动态UI基于Widget树模型,状态变化触发UI更新。状态管理是关键,Flutter提供StatefulWidget、Provider、Redux等方式。使用ListView等可滚动组件和StreamBuilder等流式组件实现数据驱动视图的自动更新。响应式布局确保UI在不同设备上的适应性。Flutter为开发者构建动态、用户友好的界面提供了强大支持。
39 2
|
1月前
|
前端开发 JavaScript Java
SSMP整合案例第六步 在前端页面上利用axios和element-ui与后端交互实现增删改
SSMP整合案例第六步 在前端页面上利用axios和element-ui与后端交互实现增删改
20 1
|
21天前
|
C# Windows
一款开源、免费、现代化风格的WPF UI控件库
一款开源、免费、现代化风格的WPF UI控件库
|
23天前
|
前端开发
ElementPlus卡片如何能够一行呈四,黑马UI前端布局视频资料,element样式具体的细节无法修改,F12找到那个位置,可能在其他组件写了错误,找到那个位置,围绕着位置解决问题最快了,卡片下边
ElementPlus卡片如何能够一行呈四,黑马UI前端布局视频资料,element样式具体的细节无法修改,F12找到那个位置,可能在其他组件写了错误,找到那个位置,围绕着位置解决问题最快了,卡片下边
|
23天前
|
JavaScript BI UED
vue + element UI【实战】打字闯关(含按键监听、按键音效、字符匹配、动态样式、结果判定、数据统计、音效获取和剪辑等实用技巧)
vue + element UI【实战】打字闯关(含按键监听、按键音效、字符匹配、动态样式、结果判定、数据统计、音效获取和剪辑等实用技巧)
17 0
|
25天前
鸿蒙使用 @Builder扩展出来的布局数据更新没法更新UI
采用的方法是在修改数据时,通过`this.dArray.splice(index, 1, this.dArray[index])`替换指定元素,强制数组更新并反映到界面上。
40 0
|
2月前
|
开发框架 前端开发 JavaScript
【Flutter前端技术开发专栏】Flutter中的动态UI构建与数据驱动视图
【4月更文挑战第30天】Flutter是一款高效跨平台移动开发框架,以其热重载、高性能渲染和丰富组件库著称,简化了动态UI和数据驱动视图的实现。本文深入讨论了动态UI构建原理,包括基于Widget树模型的UI更新和状态管理,如使用StatefulWidget和数据流库(如Provider、Redux)。此外,文中还介绍了实现技巧,如使用ListView等可滚动组件、StreamBuilder进行数据流驱动的UI更新,以及应用响应式布局以适应不同设备。Flutter为开发者提供了构建高效动态界面的强大工具。
【Flutter前端技术开发专栏】Flutter中的动态UI构建与数据驱动视图
|
2月前
|
XML API Android开发
构建高效的安卓应用:使用Jetpack Compose实现动态UI
【4月更文挑战第13天】 在移动应用开发领域,随着用户对流畅体验和即时反馈的期待不断上升,开发者面临着构建高效、响应式且具有丰富交互性的用户界面的挑战。传统的Android开发方法,如基于XML的布局,虽然稳定但往往伴随着较高的资源消耗和较低的开发效率。本文将探讨如何使用Jetpack Compose——一种现代声明式UI工具包,来构建动态且高效的安卓应用界面。通过深入分析Jetpack Compose的核心原理及其与传统方法的对比,揭示如何利用其强大的功能集合提升应用性能和开发效率。我们将通过实例演示如何快速构建可重用组件、实现实时数据绑定,以及优化布局渲染过程,从而为开发者提供一种更简洁、