首页设计二,创建ListBox列表数据
1.效果图:
一.首页鼠标悬停效果
- 使用触发器来实现
首先,上面的图标文本框是使用 Border 来实现的,那么就要在 Border 中重写该样式。
* 所有的控件,触发器固定写法应该都是这样,只需要通过TargetType 来指定目标控件类型即可
<Border.Style><Style TargetType="Border"><Style.Triggers></Style.Triggers></Style></Border.Style>
- 接着为触发器添加动作属性,例如,当前要实现的是如果鼠标悬停时,给Border 设置一个阴影效果,鼠标悬停触发器属性设置如下
<Trigger Property="IsMouseOver" Value="True"></Trigger>
- 添加阴影 Effect 属性 ,通过设置阴影的阴影效果属性 DropShadowEffect 来实现,阴影效果主要的几个属性
- Color 颜色
- ShadowDepth 深度
- BlurRadius 阴影模糊程度
<Border.Style><Style TargetType="Border"><Style.Triggers><Trigger Property="IsMouseOver" Value="True"><Setter Property="Effect"><Setter.Value><DropShadowEffect Color="#DDDDDD" ShadowDepth="1" BlurRadius="10" /></Setter.Value></Setter></Trigger></Style.Triggers></Style></Border.Style>
- 以下都是写触发器设置一些效果的固定的写法
--Property="xx" 设置鼠标悬停属性,或者是鼠标其他属性
<Trigger Property="xx" Value="True">
-- Property="xxx" 需要设置的属性,例如当前是阴影属性<Setter Property="xxx">
-- 那么就再加一层这个 Setter.Value<Setter.Value>
--- 然后这里就具体设置阴影的一些属性<xxx /></Setter.Value></Setter></Trigger>
反正我是这样理解 的,错了就错了,无关要紧。博客不做任何教程提供,只做为学习笔记!
二.ListBox 创建数据模板并添加数据
1.首先,给待办事项列表和备忘录列表添加实体类,并且把一些共用属性抽离出来,做为父类。
- 父类属性
public class BaseDto
{private int id;public int Id{get { return id; }set { id = value; }}private DateTime createDate;public DateTime CreateDate{get { return createDate; }set { createDate = value; }}private DateTime updateDate;public DateTime UpdateDate{get { return updateDate; }set { updateDate = value; }}}
- 待办事项属性,并且继承了父类的共用属性
public class ToDoDto: BaseDto
{private string title;/// <summary>/// 标题/// </summary>public string Title{get { return title; }set { title = value; }}private string content;/// <summary>/// 内容/// </summary>public string Content{get { return content; }set { content = value; }}private int status;/// <summary>/// 状态/// </summary>public int Status{get { return status; }set { status = value; }}
}
- 备忘录属性,同样继承自父类
/// <summary>/// 备忘录属性/// </summary>public class MemoDto:BaseDto{private string title;/// <summary>/// 标题/// </summary>public string Title{get { return title; }set { title = value; }}private string content;/// <summary>/// 内容/// </summary>public string Content{get { return content; }set { content = value; }}private int status;/// <summary>/// 状态/// </summary>public int Status{get { return status; }set { status = value; }}}
2. 实体类创建完成,创建数据,并渲染在ListBox中
- 在 IndexViewModel.cs 中,创建两个集合的静态数据
public class IndexViewModel:BindableBase{public IndexViewModel(){TaskBars=new ObservableCollection<TaskBar>();CreateTaskBars();CreateTestDate();}private ObservableCollection<TaskBar> taskBars;public ObservableCollection<TaskBar> TaskBars{get { return taskBars; }set { taskBars = value; RaisePropertyChanged(); }}private ObservableCollection<ToDoDto> toDoDtos;public ObservableCollection<ToDoDto> ToDoDtos{get { return toDoDtos; }set { toDoDtos = value; RaisePropertyChanged(); }}private ObservableCollection<MemoDto> memoDtos;public ObservableCollection<MemoDto> MemoDtos{get { return memoDtos; }set { memoDtos = 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 = "" });}void CreateTestDate(){ToDoDtos = new ObservableCollection<ToDoDto>();MemoDtos = new ObservableCollection<MemoDto>();for (int i = 0; i < 10; i++){ToDoDtos.Add(new ToDoDto { Title="待办"+i,Content="正在处理中.."});MemoDtos.Add(new MemoDto { Title = "备忘" + i, Content = "我的密码" });}}}
3. 把渲染数据在ListBox中
- 在 ListBox 绑定数据
<ListBox ItemsSource="{Binding ToDoDtos}" />
- 修改ListBox 控件模板,这个写法是固定的
<ListBox ItemsSource="{Binding ToDoDtos}" ><ListBox.ItemTemplate><DataTemplate></DataTemplate></ListBox.ItemTemplate>
</ListBox>
- 待办事项
<ListBox ItemsSource="{Binding ToDoDtos}" ScrollViewer.VerticalScrollBarVisibility="Hidden" HorizontalContentAlignment="Stretch" ><ListBox.ItemTemplate><DataTemplate><DockPanel MaxHeight="80" LastChildFill="False"><ToggleButton DockPanel.Dock="Right"/><StackPanel><TextBlock Text="{Binding Title}" FontSize="16" FontWeight="Bold"/><TextBlock Text="{Binding Content}" Margin="0,5" Opacity="0.5" /></StackPanel></DockPanel></DataTemplate></ListBox.ItemTemplate>
</ListBox>
- 备忘录
<ListBox ItemsSource="{Binding MemoDtos}" ScrollViewer.VerticalScrollBarVisibility="Hidden" ><ListBox.ItemTemplate><DataTemplate><StackPanel MaxHeight="80"><TextBlock Text="{Binding Title}" FontSize="16" FontWeight="Bold"/><TextBlock Text="{Binding Content}" Margin="0,5" Opacity="0.5" /></StackPanel></DataTemplate></ListBox.ItemTemplate>
</ListBox>
- ListBox 隐藏滚动条属性:ScrollViewer.VerticalScrollBarVisibility 设置成 Hidden
ScrollViewer.VerticalScrollBarVisibility="Hidden"
2. 待办事项里面,有一个ToggleButton 按钮,并且内容是分左右2个部分,所以外层可以使用DockPanel 包着,并且使用 DockPanel.Dock="Right" 属性来控制 按钮向右移动。
3. 还需要设置 List Box 一个HorizontalContentAlignment 属性,为Stretch。这样ToggleButton 才会往右移动。
三.源码
- IndexView.xaml
<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"><Border.Style><Style TargetType="Border"><Style.Triggers><Trigger Property="IsMouseOver" Value="True"><Setter Property="Effect"><Setter.Value><DropShadowEffect Color="#DDDDDD" ShadowDepth="1" BlurRadius="10" /></Setter.Value></Setter></Trigger></Style.Triggers></Style></Border.Style><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 ItemsSource="{Binding ToDoDtos}" ScrollViewer.VerticalScrollBarVisibility="Hidden" HorizontalContentAlignment="Stretch" ><ListBox.ItemTemplate><DataTemplate><DockPanel MaxHeight="80" LastChildFill="False"><ToggleButton DockPanel.Dock="Right"/><StackPanel><TextBlock Text="{Binding Title}" FontSize="16" FontWeight="Bold"/><TextBlock Text="{Binding Content}" Margin="0,5" Opacity="0.5" /></StackPanel></DockPanel></DataTemplate></ListBox.ItemTemplate></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 ItemsSource="{Binding MemoDtos}" ScrollViewer.VerticalScrollBarVisibility="Hidden" ><ListBox.ItemTemplate><DataTemplate><StackPanel MaxHeight="80"><TextBlock Text="{Binding Title}" FontSize="16" FontWeight="Bold"/><TextBlock Text="{Binding Content}" Margin="0,5" Opacity="0.5" /></StackPanel></DataTemplate></ListBox.ItemTemplate></ListBox></DockPanel></Grid></Grid>
</UserControl>