• Wpf 使用 Prism 实战开发Day05


    首页设计

    1.效果图

    一.代码现实

    • 根据页面布局,可以将页面设计成3行,每行中分多少列,看需求而定
    • 根据页面内容,设计Model 实体类,以及View Model

     1.Index.xaml 页面布局设计

    •  RowDefinition 分行(Row)属性,分几行就写几个
    • ColumnDefinition 分列(Column )属性,分几列就写几个
    •  Height 属性,Row (行)属性只有Height (高度),没有宽度。并且Height 设置成 auto 时,根据内容适应高度。
    • Width 属性,Column (列) 只有Width (宽度),没有Height (高度
    • Grid.Row 属性,将控件放置在第几行
    • Grid.Column 属性,将控件放置在第几列
    • FontWeight 属性,设置成 Bold (加粗)
    • Opacity 属性,透明度
    • ItemsSource 属性,数据源绑定
    • ClipToBounds 裁剪属性,在Canvas 控件中使用,ClipToBounds 设置成True后,超出的内容会被裁剪掉

    • 第一行设计,放置显示的文本控件 TextBlock。

    FontSize 属性,控件显示的字体大小


    • 第二行设计,使用 ItemsControl 控件,并且固定4列。

    1. 在的Grid 控件中,每定义的每行(Row)中还可以划分成更细的行和列。根据需求自由灵活定义使用。
    2. ItemsControl 控件:它允许您将任何类型的数据绑定到其中,并为每个数据项显示一个模板

     ItemsControl 控件固定的写法。比如,要分4列

    1. <ItemsControl>
    2. <ItemsControl.ItemsPanel>
    3. <ItemsPanelTemplate>
    4. <UniformGrid Columns="4"/>
    5. ItemsPanelTemplate>
    6. ItemsControl.ItemsPanel>
    7. ItemsControl>

    ItemsControl  列固定完后,接着进行固定内容模板的设计。内容设计固定的写法

    1. <ItemsControl.ItemTemplate>
    2. <DataTemplate>
    3. DataTemplate>
    4. ItemsControl.ItemTemplate>
    1. 进行模板内容设计的时候,首先要清楚模板中都要有那些内容
    2. 例如,当前的模板:有图标,标题文本,内容,背景颜色,还有2个白色的图标


    1.  外层使用Border ,要设计圆角
    2. Border 里面放置Grid,方便设计。因为Grid 中可以放置多个控件
    3. 右边白色的圆图标,使用Canvas 控件
    1. <ItemsControl Grid.Row="1">
    2. <ItemsControl.ItemsPanel>
    3. <ItemsPanelTemplate>
    4. <UniformGrid Columns="4"/>
    5. ItemsPanelTemplate>
    6. ItemsControl.ItemsPanel>
    7. <ItemsControl.ItemTemplate>
    8. <DataTemplate>
    9. <Border>
    10. <Grid>
    11. <StackPanel>
    12. <materialDesign:PackIcon Kind="Abacus" />
    13. <TextBlock Text="jj"/>
    14. <TextBlock Text="888" FontWeight="Bold"/>
    15. StackPanel>
    16. <Canvas>
    17. <Border Canvas.Top="10" Canvas.Right="-50" Width="120" Height="120" Background="#ffffff" Opacity="0.1"/>
    18. <Border Canvas.Top="80" Canvas.Right="-30" Width="120" Height="120" Background="#ffffff" Opacity="0.1"/>
    19. Canvas>
    20. Grid>
    21. Border>
    22. DataTemplate>
    23. ItemsControl.ItemTemplate>
    24. ItemsControl>
     1.1 模板设计完成后,进行数据内容动态渲染。
    • 创建首页内容数据集合实体类模型 TaskBar
    1. ///
    2. /// 首页任务栏
    3. ///
    4. public class TaskBar: BindableBase
    5. {
    6. private string icon;
    7. ///
    8. /// 图标
    9. ///
    10. public string Icon
    11. {
    12. get { return icon; }
    13. set { icon = value; }
    14. }
    15. private string title;
    16. ///
    17. /// 标题
    18. ///
    19. public string Title
    20. {
    21. get { return title; }
    22. set { title = value; }
    23. }
    24. private string content;
    25. ///
    26. /// 内容
    27. ///
    28. public string Content
    29. {
    30. get { return content; }
    31. set { content = value; }
    32. }
    33. private string color;
    34. ///
    35. /// 背景颜色
    36. ///
    37. public string Color
    38. {
    39. get { return color; }
    40. set { color = value; }
    41. }
    42. private string target;
    43. ///
    44. /// 触发目标
    45. ///
    46. public string Target
    47. {
    48. get { return target; }
    49. set { target = value; }
    50. }
    51. }
    • 接着,在ViewModel 中,创建出TaskBar数据的动态集合,给页面展示数据
    1. public class IndexViewModel:BindableBase
    2. {
    3. public IndexViewModel()
    4. {
    5. TaskBars=new ObservableCollection();
    6. CreateTaskBars();
    7. }
    8. private ObservableCollection taskBars;
    9. public ObservableCollection TaskBars
    10. {
    11. get { return taskBars; }
    12. set { taskBars = value; RaisePropertyChanged(); }
    13. }
    14. void CreateTaskBars()
    15. {
    16. TaskBars.Add(new TaskBar() { Icon="ClockFast",Title="汇总",Content="9",Color="#FF0CA0FF",Target=""});
    17. TaskBars.Add(new TaskBar() { Icon = "ClockCheckOutline", Title = "已完成", Content = "9", Color = "#FF1ECA3A", Target = "" });
    18. TaskBars.Add(new TaskBar() { Icon = "ChartLineVariant", Title = "完成比例", Content = "9%", Color = "#FF02C6DC", Target = "" });
    19. TaskBars.Add(new TaskBar() { Icon = "PlaylistStar", Title = "备忘录", Content = "18", Color = "#FFFFA000", Target = "" });
    20. }
    21. }
    •  最后,在ItemsControl 中,进行数据绑定
    1. <ItemsControl Grid.Row="1" ItemsSource="{Binding TaskBars}">
    2. <ItemsControl.ItemsPanel>
    3. <ItemsPanelTemplate>
    4. <UniformGrid Columns="4"/>
    5. ItemsPanelTemplate>
    6. ItemsControl.ItemsPanel>
    7. <ItemsControl.ItemTemplate>
    8. <DataTemplate>
    9. <Border Background="{Binding Color}" CornerRadius="5" Margin="10">
    10. <Grid>
    11. <StackPanel Margin="20,10">
    12. <materialDesign:PackIcon Kind="{Binding Icon}" Width="30" Height="30" />
    13. <TextBlock Text="{Binding Title}" Margin="0,15" FontSize="15"/>
    14. <TextBlock Text="{Binding Content}" FontWeight="Bold" FontSize="40"/>
    15. StackPanel>
    16. <Canvas ClipToBounds="True">
    17. <Border Canvas.Top="10" CornerRadius="100" Canvas.Right="-50" Width="120" Height="120" Background="#ffffff" Opacity="0.1"/>
    18. <Border Canvas.Top="80" CornerRadius="100" Canvas.Right="-30" Width="120" Height="120" Background="#ffffff" Opacity="0.1"/>
    19. Canvas>
    20. Grid>
    21. Border>
    22. DataTemplate>
    23. ItemsControl.ItemTemplate>
    24. ItemsControl>

    • 第三行设计,当前行,分两列放置内容

    1. LastChildFill 属性,设置成False,不填允最后一列
    2. Button 按钮,样式希望变成图角,可以使用组件带的默认样式来实现,如下
    Style="{StaticResource MaterialDesignFloatingActionAccentButton}"
    1. <Grid Grid.Row="2" Margin="0,10">
    2. <Grid.ColumnDefinitions>
    3. <ColumnDefinition/>
    4. <ColumnDefinition/>
    5. Grid.ColumnDefinitions>
    6. <Border Margin="10,0" Background="#BEBEBE" CornerRadius="5" Opacity="0.1"/>
    7. <Border Grid.Column="1" Margin="10,0" Background="#BEBEBE" CornerRadius="5" Opacity="0.1"/>
    8. <DockPanel Margin="10,0">
    9. <DockPanel Margin="10,5" LastChildFill="False" DockPanel.Dock="Top">
    10. <TextBlock Text="待办事项" FontSize="20" FontWeight="Bold"/>
    11. <Button Width="30" Height="30" VerticalAlignment="Top" DockPanel.Dock="Right" Style="{StaticResource MaterialDesignFloatingActionAccentButton}" >
    12. <materialDesign:PackIcon Kind="Add" />
    13. Button>
    14. DockPanel>
    15. <ListBox />
    16. DockPanel>
    17. <DockPanel Grid.Column="1" Margin="10,0">
    18. <DockPanel Margin="10,5" LastChildFill="False" DockPanel.Dock="Top">
    19. <TextBlock Text="待办事项" FontSize="20" FontWeight="Bold"/>
    20. <Button Width="30" Height="30" VerticalAlignment="Top" DockPanel.Dock="Right" Style="{StaticResource MaterialDesignFloatingActionAccentButton}" >
    21. <materialDesign:PackIcon Kind="Add" />
    22. Button>
    23. DockPanel>
    24. <ListBox />
    25. DockPanel>
    26. Grid>

     二.Index.xaml 完整源码

    1. <UserControl x:Class="MyToDo.Views.IndexView"
    2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    4. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    5. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    6. xmlns:local="clr-namespace:MyToDo.Views"
    7. mc:Ignorable="d"
    8. xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
    9. d:DesignHeight="450" d:DesignWidth="800">
    10. <Grid>
    11. <Grid.RowDefinitions>
    12. <RowDefinition Height="auto"/>
    13. <RowDefinition Height="auto"/>
    14. <RowDefinition/>
    15. Grid.RowDefinitions>
    16. <TextBlock Margin="15,10" FontSize="22" Text="你好,WPF! 今天是2023-11-12 星期天"/>
    17. <ItemsControl Grid.Row="1" ItemsSource="{Binding TaskBars}">
    18. <ItemsControl.ItemsPanel>
    19. <ItemsPanelTemplate>
    20. <UniformGrid Columns="4"/>
    21. ItemsPanelTemplate>
    22. ItemsControl.ItemsPanel>
    23. <ItemsControl.ItemTemplate>
    24. <DataTemplate>
    25. <Border Background="{Binding Color}" CornerRadius="5" Margin="10">
    26. <Grid>
    27. <StackPanel Margin="20,10">
    28. <materialDesign:PackIcon Kind="{Binding Icon}" Width="30" Height="30" />
    29. <TextBlock Text="{Binding Title}" Margin="0,15" FontSize="15"/>
    30. <TextBlock Text="{Binding Content}" FontWeight="Bold" FontSize="40"/>
    31. StackPanel>
    32. <Canvas ClipToBounds="True">
    33. <Border Canvas.Top="10" CornerRadius="100" Canvas.Right="-50" Width="120" Height="120" Background="#ffffff" Opacity="0.1"/>
    34. <Border Canvas.Top="80" CornerRadius="100" Canvas.Right="-30" Width="120" Height="120" Background="#ffffff" Opacity="0.1"/>
    35. Canvas>
    36. Grid>
    37. Border>
    38. DataTemplate>
    39. ItemsControl.ItemTemplate>
    40. ItemsControl>
    41. <Grid Grid.Row="2" Margin="0,10">
    42. <Grid.ColumnDefinitions>
    43. <ColumnDefinition/>
    44. <ColumnDefinition/>
    45. Grid.ColumnDefinitions>
    46. <Border Margin="10,0" Background="#BEBEBE" CornerRadius="5" Opacity="0.1"/>
    47. <Border Grid.Column="1" Margin="10,0" Background="#BEBEBE" CornerRadius="5" Opacity="0.1"/>
    48. <DockPanel Margin="10,0">
    49. <DockPanel Margin="10,5" LastChildFill="False" DockPanel.Dock="Top">
    50. <TextBlock Text="待办事项" FontSize="20" FontWeight="Bold"/>
    51. <Button Width="30" Height="30" VerticalAlignment="Top" DockPanel.Dock="Right" Style="{StaticResource MaterialDesignFloatingActionAccentButton}" >
    52. <materialDesign:PackIcon Kind="Add" />
    53. Button>
    54. DockPanel>
    55. <ListBox />
    56. DockPanel>
    57. <DockPanel Grid.Column="1" Margin="10,0">
    58. <DockPanel Margin="10,5" LastChildFill="False" DockPanel.Dock="Top">
    59. <TextBlock Text="待办事项" FontSize="20" FontWeight="Bold"/>
    60. <Button Width="30" Height="30" VerticalAlignment="Top" DockPanel.Dock="Right" Style="{StaticResource MaterialDesignFloatingActionAccentButton}" >
    61. <materialDesign:PackIcon Kind="Add" />
    62. Button>
    63. DockPanel>
    64. <ListBox />
    65. DockPanel>
    66. Grid>
    67. Grid>
    68. UserControl>

  • 相关阅读:
    基本的Linux命令
    KO88 冲销内部订单
    C# NX二次开发-设置背景颜色
    22年国内最牛的Java面试八股文合集(全彩版),不接受反驳
    二叉树 | 对称二叉树、相同的树、子树相同 | leecode刷题笔记
    go数组后添加元素
    Redis_缓存穿透、击穿、雪崩
    4G LTE教程
    一款剧情特别优秀的ARPG 游戏《FC魔神英雄传》
    JUC - 多线程之Callable;集合类线程不安全(二)
  • 原文地址:https://blog.csdn.net/weixin_39237340/article/details/134361463