赞
踩
WPF MaterialDesign 初学项目实战(0):github 项目Demo运行
WPF MaterialDesign 初学项目实战(1)首页搭建
WPF MaterialDesign 初学项目实战(2)首页导航栏样式
WPF MaterialDesign 初学项目实战(3)动态侧边栏
WPF MaterialDesign 初学项目实战(4)侧边栏路由管理
我想到WPF的环境搭建比较复杂,我之后的博客会把所有的环境写下来。
//materialDesign,prism材质包 xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes" //点击反馈 xmlns:behaviors="http://schemas.microsoft.com/xaml/behaviors" //引入prism xmlns:prism="http://prismlibrary.com/" //materialDesign 资源样式 TextElement.Foreground="{DynamicResource MaterialDesignBody}" TextElement.FontWeight="Regular" TextElement.FontSize="13" TextOptions.TextFormattingMode="Ideal" TextOptions.TextRenderingMode="Auto" Background="{DynamicResource MaterialDesignPaper}" FontFamily="{DynamicResource MaterialDesignFont}"
预定效果:
准备添加模板文件
<Grid> <Grid.RowDefinitions> <RowDefinition Height="auto" /> <RowDefinition Height="auto" /> <RowDefinition /> </Grid.RowDefinitions> <TextBlock Text="你好,今天是2023年5月17日" FontSize="22"/> <!--添加模板文件--> <ItemsControl> <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <UniformGrid Columns="4"/> </ItemsPanelTemplate> </ItemsControl.ItemsPanel> <!--定义模板文件--> <ItemsControl.ItemTemplate> <DataTemplate> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl> </Grid>
分析需要实现的效果:
实现逻辑
TaskBar声明数据类型
public class TaskBar: BindableBase { /// <summary> /// 图标 /// </summary> public string? Icon { get; set; } /// <summary> /// 标题 /// </summary> public string? Title { get; set; } /// <summary> /// 内容 /// </summary> public string? Content { get; set; } /// <summary> /// 颜色 /// </summary> public string? Color { get; set; } /// <summary> /// 路由地址 /// </summary> public string? Target { get; set; } }
ViewModel实例化元素:
public class IndexViewModel : BindableBase { public IndexViewModel() { taskBars = new ObservableCollection<TaskBar>(); CreateTaskBar(); } private ObservableCollection<TaskBar> taskBars; public ObservableCollection<TaskBar> TaskBars { get { return taskBars; } set { taskBars = value; RaisePropertyChanged(); } } void CreateTaskBar() { TaskBars.Add(new TaskBar (){Icon = "CalendarClock", Color = "",Content = "9",Target = "",Title = "汇总" }); TaskBars.Add(new TaskBar (){Icon = "AlarmCheck", Color = "",Content = "9",Target = "",Title = "已完成" }); TaskBars.Add(new TaskBar (){Icon = "ClipboardTextClock", Color = "",Content = "100%",Target = "",Title = "完成率" }); TaskBars.Add(new TaskBar (){Icon = "ClockEditOutline", Color = "",Content = "19",Target = "",Title = "备忘录" }); } }
View:引用实体数据对象,使用模板生产
<Grid> <Grid.RowDefinitions> <RowDefinition Height="auto" /> <RowDefinition Height="auto" /> <RowDefinition /> </Grid.RowDefinitions> <TextBlock Text="你好,今天是2023年5月17日" FontSize="22" /> <!--添加模板文件--> <ItemsControl Grid.Row="1" ItemsSource="{Binding TaskBars}"> <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <UniformGrid Columns="4" /> </ItemsPanelTemplate> </ItemsControl.ItemsPanel> <!--定义模板文件--> <ItemsControl.ItemTemplate> <DataTemplate> <Border Background="{Binding Color}"> <Grid> <StackPanel> <materialDesign:PackIcon Kind="{Binding Icon}" Margin="15,0" /> <TextBlock Text="{Binding Title}" /> <TextBlock Text="{Binding Content}" FontWeight="Bold" /> </StackPanel> <!--使用Canvas画布添加两个圆--> <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> </Grid>
知识点补充:
我怕大家不知道,这里补充一下知识点:
实现效果:
我们还得修改一下样式
IndexViewModel
void CreateTaskBar()
{
TaskBars.Add(new TaskBar (){Icon = "CalendarClock", Color = "#99CC99", Content = "9",Target = "",Title = "汇总" });
TaskBars.Add(new TaskBar (){Icon = "AlarmCheck", Color = "#99CCFF", Content = "9",Target = "",Title = "已完成" });
TaskBars.Add(new TaskBar (){Icon = "ClipboardTextClock", Color = "#0066CC", Content = "100%",Target = "",Title = "完成率" });
TaskBars.Add(new TaskBar (){Icon = "ClockEditOutline", Color = "#CC0033", Content = "19",Target = "",Title = "备忘录" });
}
样式修改,添加已经用+号标出
<ItemsControl.ItemTemplate> <DataTemplate> <Border Background="{Binding Color}" + Margin="10" CornerRadius="4"> <Grid> <StackPanel + Margin="20"> <materialDesign:PackIcon Kind="{Binding Icon}" + Margin="10,0" + Width="30" + Height="30" /> <TextBlock Text="{Binding Title}" + Margin="0,15" /> <TextBlock Text="{Binding Content}" FontWeight="Bold" + FontSize="40" /> </StackPanel> <!--使用Canvas画布添加两个圆--> <Canvas + ClipToBounds="True" > <Border Canvas.Top="10" Canvas.Right="-50" Width="120" Height="120" Background="#FFFFFF" CornerRadius="100" Opacity="0.1" /> <Border Canvas.Top="80" Canvas.Right="-30" Width="120" Height="120" CornerRadius="100" Background="#FFFFFF" Opacity="0.1" /> </Canvas> </Grid> </Border> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl>
添加样式
<Grid Grid.Row="2"> <Grid.ColumnDefinitions> <ColumnDefinition /> <ColumnDefinition /> </Grid.ColumnDefinitions> <Border Margin="10" CornerRadius="4" Opacity="0.1" Background="#BEBEBE" /> <DockPanel Margin="15"> <DockPanel LastChildFill="False"> <TextBlock Text="代办事项" FontSize='30' FontWeight="Bold" /> <Button DockPanel.Dock="Right" VerticalAlignment="Top" Margin="0,0,10,0" Width="35" Height="35" Style="{StaticResource MaterialDesignFloatingActionAccentButton}"> <materialDesign:PackIcon Kind="Add"/> </Button> <ListBox /> </DockPanel> </DockPanel> <Border Margin="10" CornerRadius="4" Opacity="0.1" Grid.Column="1" Background="#BEBEBE" /> <DockPanel Margin="15" Grid.Column="1" > <DockPanel LastChildFill="False" > <TextBlock Text="代办事项" FontSize='30' FontWeight="Bold" /> <Button DockPanel.Dock="Right" VerticalAlignment="Top" Margin="0,0,10,0" Width="35" Height="35" Style="{StaticResource MaterialDesignFloatingActionAccentButton}"> <materialDesign:PackIcon Kind="Add" /> </Button> <ListBox /> </DockPanel> </DockPanel> </Grid>
实际效果:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。