

- 根据页面布局,可以将页面设计成3行,每行中分多少列,看需求而定
- 根据页面内容,设计Model 实体类,以及View Model
FontSize 属性,控件显示的字体大小
- 在的Grid 控件中,每定义的每行(Row)中还可以划分成更细的行和列。根据需求自由灵活定义使用。
- ItemsControl 控件:它允许您将任何类型的数据绑定到其中,并为每个数据项显示一个模板
ItemsControl 控件固定的写法。比如,要分4列
- <ItemsControl>
- <ItemsControl.ItemsPanel>
- <ItemsPanelTemplate>
- <UniformGrid Columns="4"/>
- ItemsPanelTemplate>
- ItemsControl.ItemsPanel>
- ItemsControl>
ItemsControl 列固定完后,接着进行固定内容模板的设计。内容设计固定的写法
- <ItemsControl.ItemTemplate>
- <DataTemplate>
-
- DataTemplate>
- ItemsControl.ItemTemplate>
- 进行模板内容设计的时候,首先要清楚模板中都要有那些内容
- 例如,当前的模板:有图标,标题文本,内容,背景颜色,还有2个白色的图标
- <ItemsControl Grid.Row="1">
- <ItemsControl.ItemsPanel>
- <ItemsPanelTemplate>
- <UniformGrid Columns="4"/>
- ItemsPanelTemplate>
- ItemsControl.ItemsPanel>
-
- <ItemsControl.ItemTemplate>
- <DataTemplate>
- <Border>
- <Grid>
- <StackPanel>
-
- <materialDesign:PackIcon Kind="Abacus" />
-
- <TextBlock Text="jj"/>
-
- <TextBlock Text="888" FontWeight="Bold"/>
- StackPanel>
-
- <Canvas>
- <Border Canvas.Top="10" Canvas.Right="-50" Width="120" Height="120" Background="#ffffff" Opacity="0.1"/>
- <Border Canvas.Top="80" Canvas.Right="-30" Width="120" Height="120" Background="#ffffff" Opacity="0.1"/>
- Canvas>
- Grid>
- Border>
- DataTemplate>
- ItemsControl.ItemTemplate>
- ItemsControl>
- ///
- /// 首页任务栏
- ///
- public class TaskBar: BindableBase
- {
- private string icon;
-
- ///
- /// 图标
- ///
- public string Icon
- {
- get { return icon; }
- set { icon = value; }
- }
- private string title;
- ///
- /// 标题
- ///
- public string Title
- {
- get { return title; }
- set { title = value; }
- }
- private string content;
- ///
- /// 内容
- ///
- public string Content
- {
- get { return content; }
- set { content = value; }
- }
- private string color;
- ///
- /// 背景颜色
- ///
- public string Color
- {
- get { return color; }
- set { color = value; }
- }
- private string target;
- ///
- /// 触发目标
- ///
- public string Target
- {
- get { return target; }
- set { target = value; }
- }
-
- }
- public class IndexViewModel:BindableBase
- {
- public IndexViewModel()
- {
- TaskBars=new ObservableCollection
(); - CreateTaskBars();
- }
- private ObservableCollection
taskBars; -
- public ObservableCollection
TaskBars - {
- get { return taskBars; }
- set { taskBars = value; RaisePropertyChanged(); }
- }
- void CreateTaskBars()
- {
- TaskBars.Add(new TaskBar() { Icon="ClockFast",Title="汇总",Content="9",Color="#FF0CA0FF",Target=""});
- TaskBars.Add(new TaskBar() { Icon = "ClockCheckOutline", Title = "已完成", Content = "9", Color = "#FF1ECA3A", Target = "" });
- TaskBars.Add(new TaskBar() { Icon = "ChartLineVariant", Title = "完成比例", Content = "9%", Color = "#FF02C6DC", Target = "" });
- TaskBars.Add(new TaskBar() { Icon = "PlaylistStar", Title = "备忘录", Content = "18", Color = "#FFFFA000", Target = "" });
- }
- }
- <ItemsControl Grid.Row="1" ItemsSource="{Binding TaskBars}">
- <ItemsControl.ItemsPanel>
- <ItemsPanelTemplate>
- <UniformGrid Columns="4"/>
- ItemsPanelTemplate>
- ItemsControl.ItemsPanel>
-
- <ItemsControl.ItemTemplate>
- <DataTemplate>
- <Border Background="{Binding Color}" CornerRadius="5" Margin="10">
- <Grid>
- <StackPanel Margin="20,10">
-
- <materialDesign:PackIcon Kind="{Binding Icon}" Width="30" Height="30" />
-
- <TextBlock Text="{Binding Title}" Margin="0,15" FontSize="15"/>
-
- <TextBlock Text="{Binding Content}" FontWeight="Bold" FontSize="40"/>
- StackPanel>
-
- <Canvas ClipToBounds="True">
- <Border Canvas.Top="10" CornerRadius="100" Canvas.Right="-50" Width="120" Height="120" Background="#ffffff" Opacity="0.1"/>
- <Border Canvas.Top="80" CornerRadius="100" Canvas.Right="-30" Width="120" Height="120" Background="#ffffff" Opacity="0.1"/>
- Canvas>
- Grid>
- Border>
- DataTemplate>
- ItemsControl.ItemTemplate>
- ItemsControl>
- LastChildFill 属性,设置成False,不填允最后一列
- Button 按钮,样式希望变成图角,可以使用组件带的默认样式来实现,如下
Style="{StaticResource MaterialDesignFloatingActionAccentButton}"
- <Grid Grid.Row="2" Margin="0,10">
- <Grid.ColumnDefinitions>
- <ColumnDefinition/>
- <ColumnDefinition/>
- Grid.ColumnDefinitions>
-
-
- <Border Margin="10,0" Background="#BEBEBE" CornerRadius="5" Opacity="0.1"/>
- <Border Grid.Column="1" Margin="10,0" Background="#BEBEBE" CornerRadius="5" Opacity="0.1"/>
-
-
- <DockPanel Margin="10,0">
- <DockPanel Margin="10,5" LastChildFill="False" DockPanel.Dock="Top">
- <TextBlock Text="待办事项" FontSize="20" FontWeight="Bold"/>
- <Button Width="30" Height="30" VerticalAlignment="Top" DockPanel.Dock="Right" Style="{StaticResource MaterialDesignFloatingActionAccentButton}" >
- <materialDesign:PackIcon Kind="Add" />
- Button>
- DockPanel>
-
- <ListBox />
- DockPanel>
-
-
- <DockPanel Grid.Column="1" Margin="10,0">
- <DockPanel Margin="10,5" LastChildFill="False" DockPanel.Dock="Top">
- <TextBlock Text="待办事项" FontSize="20" FontWeight="Bold"/>
- <Button Width="30" Height="30" VerticalAlignment="Top" DockPanel.Dock="Right" Style="{StaticResource MaterialDesignFloatingActionAccentButton}" >
- <materialDesign:PackIcon Kind="Add" />
- Button>
- DockPanel>
-
- <ListBox />
- DockPanel>
- Grid>
- <UserControl x:Class="MyToDo.Views.IndexView"
- 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"
- xmlns:local="clr-namespace:MyToDo.Views"
- mc:Ignorable="d"
- xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
- d:DesignHeight="450" d:DesignWidth="800">
- <Grid>
- <Grid.RowDefinitions>
- <RowDefinition Height="auto"/>
- <RowDefinition Height="auto"/>
- <RowDefinition/>
- Grid.RowDefinitions>
- <TextBlock Margin="15,10" FontSize="22" Text="你好,WPF! 今天是2023-11-12 星期天"/>
-
- <ItemsControl Grid.Row="1" ItemsSource="{Binding TaskBars}">
- <ItemsControl.ItemsPanel>
- <ItemsPanelTemplate>
- <UniformGrid Columns="4"/>
- ItemsPanelTemplate>
- ItemsControl.ItemsPanel>
-
- <ItemsControl.ItemTemplate>
- <DataTemplate>
- <Border Background="{Binding Color}" CornerRadius="5" Margin="10">
- <Grid>
- <StackPanel Margin="20,10">
-
- <materialDesign:PackIcon Kind="{Binding Icon}" Width="30" Height="30" />
-
- <TextBlock Text="{Binding Title}" Margin="0,15" FontSize="15"/>
-
- <TextBlock Text="{Binding Content}" FontWeight="Bold" FontSize="40"/>
- StackPanel>
-
- <Canvas ClipToBounds="True">
- <Border Canvas.Top="10" CornerRadius="100" Canvas.Right="-50" Width="120" Height="120" Background="#ffffff" Opacity="0.1"/>
- <Border Canvas.Top="80" CornerRadius="100" Canvas.Right="-30" Width="120" Height="120" Background="#ffffff" Opacity="0.1"/>
- Canvas>
- Grid>
- Border>
- DataTemplate>
- ItemsControl.ItemTemplate>
- ItemsControl>
-
- <Grid Grid.Row="2" Margin="0,10">
- <Grid.ColumnDefinitions>
- <ColumnDefinition/>
- <ColumnDefinition/>
- Grid.ColumnDefinitions>
-
-
- <Border Margin="10,0" Background="#BEBEBE" CornerRadius="5" Opacity="0.1"/>
- <Border Grid.Column="1" Margin="10,0" Background="#BEBEBE" CornerRadius="5" Opacity="0.1"/>
-
-
- <DockPanel Margin="10,0">
- <DockPanel Margin="10,5" LastChildFill="False" DockPanel.Dock="Top">
- <TextBlock Text="待办事项" FontSize="20" FontWeight="Bold"/>
- <Button Width="30" Height="30" VerticalAlignment="Top" DockPanel.Dock="Right" Style="{StaticResource MaterialDesignFloatingActionAccentButton}" >
- <materialDesign:PackIcon Kind="Add" />
- Button>
- DockPanel>
-
- <ListBox />
- DockPanel>
-
-
- <DockPanel Grid.Column="1" Margin="10,0">
- <DockPanel Margin="10,5" LastChildFill="False" DockPanel.Dock="Top">
- <TextBlock Text="待办事项" FontSize="20" FontWeight="Bold"/>
- <Button Width="30" Height="30" VerticalAlignment="Top" DockPanel.Dock="Right" Style="{StaticResource MaterialDesignFloatingActionAccentButton}" >
- <materialDesign:PackIcon Kind="Add" />
- Button>
- DockPanel>
-
- <ListBox />
- DockPanel>
- Grid>
- Grid>
- UserControl>