WPF实现时间轴(仿Gitee)
生活随笔
收集整理的這篇文章主要介紹了
WPF实现时间轴(仿Gitee)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?WPF開發者QQ群:?340500857? | 微信群 -> 進入公眾號主頁?加入組織
“?前言,接著上一篇圓形菜單。”
歡迎轉發、分享、點贊、在看,謝謝~。??
01
—
效果預覽
效果預覽(更多效果請下載源碼體驗):
02
—
代碼如下
一、TimeLineItemControl.cs 代碼如下
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Media;namespace WpfTimeLineControl {public class TimeLineItemControl : Control{public bool IsBottom{get { return (bool)GetValue(IsBottomProperty); }set { SetValue(IsBottomProperty, value); }}public static readonly DependencyProperty IsBottomProperty =DependencyProperty.Register("IsBottom", typeof(bool), typeof(TimeLineItemControl), new PropertyMetadata(false));public ItemTypeEnum ItemType{get { return (ItemTypeEnum)GetValue(ItemTypeProperty); }set { SetValue(ItemTypeProperty, value); }}public static readonly DependencyProperty ItemTypeProperty =DependencyProperty.Register("ItemType", typeof(ItemTypeEnum), typeof(TimeLineItemControl));public string Text{get { return (string)GetValue(TextProperty); }set { SetValue(TextProperty, value); }}public static readonly DependencyProperty TextProperty =DependencyProperty.Register("Text", typeof(string), typeof(TimeLineItemControl), new PropertyMetadata(string.Empty));public string Head{get { return (string)GetValue(HeadProperty); }set { SetValue(HeadProperty, value); }}public static readonly DependencyProperty HeadProperty =DependencyProperty.Register("Head", typeof(string), typeof(TimeLineItemControl), new PropertyMetadata(string.Empty));public DataTemplate CommonTemplate{get { return (DataTemplate)GetValue(CommonTemplateProperty); }set { SetValue(CommonTemplateProperty, value); }}public static readonly DependencyProperty CommonTemplateProperty =DependencyProperty.Register("CommonTemplate", typeof(DataTemplate), typeof(TimeLineItemControl));public DataTemplate TextTemplate{get { return (DataTemplate)GetValue(TextTemplateProperty); }set { SetValue(TextTemplateProperty, value); }}public static readonly DependencyProperty TextTemplateProperty =DependencyProperty.Register("TextTemplate", typeof(DataTemplate), typeof(TimeLineItemControl));public Brush BackgroundColor{get { return (Brush)GetValue(BackgroundColorProperty); }set { SetValue(BackgroundColorProperty, value); }}public static readonly DependencyProperty BackgroundColorProperty =DependencyProperty.Register("BackgroundColor", typeof(Brush), typeof(TimeLineItemControl), new PropertyMetadata(null));static TimeLineItemControl(){DefaultStyleKeyProperty.OverrideMetadata(typeof(TimeLineItemControl), new FrameworkPropertyMetadata(typeof(TimeLineItemControl)));}}public enum ItemTypeEnum{Time,Name,Fork,Star} }二、App.xaml代碼如下
三、MainWindow.xaml.cs代碼如下
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes;namespace WpfTimeLineControl {/// <summary>/// MainWindow.xaml 的交互邏輯/// </summary>public partial class MainWindow : Window{public MainWindow(){InitializeComponent();}int num = 0;private void AddButton_Click(object sender, RoutedEventArgs e){num++;switch (num){case 1:this.TimeLineControl.Items.Add(new TimeLineItemControl() { Text = DateTime.Now.ToString("yyyy-MM-dd"), ItemType = ItemTypeEnum.Time });this.TimeLineControl.Items.Add(new TimeLineItemControl() { Text = "我是騙人布010", Head = "我", ItemType = ItemTypeEnum.Name, BackgroundColor = new SolidColorBrush(GetRandomColor()) });this.TimeLineControl.Items.Add(new TimeLineItemControl() { Text = "閆驚鏵/WPFDevelopers", ItemType = ItemTypeEnum.Star });break;case 2:this.TimeLineControl.Items.Add(new TimeLineItemControl() { Text = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd"), ItemType = ItemTypeEnum.Time });this.TimeLineControl.Items.Add(new TimeLineItemControl() { Text = "風云大陸", Head = "風", ItemType = ItemTypeEnum.Name, BackgroundColor = new SolidColorBrush(GetRandomColor()) });break;case 3:this.TimeLineControl.Items.Add(new TimeLineItemControl() { Text = DateTime.Now.AddDays(-2).ToString("yyyy-MM-dd"), ItemType = ItemTypeEnum.Time });this.TimeLineControl.Items.Add(new TimeLineItemControl() { Text = "王羲之", Head = "王", ItemType = ItemTypeEnum.Name, BackgroundColor = new SolidColorBrush(GetRandomColor())});this.TimeLineControl.Items.Add(new TimeLineItemControl() { Text = "閆驚鏵/WPFDevelopers", ItemType = ItemTypeEnum.Star });break;case 4:this.TimeLineControl.Items.Add(new TimeLineItemControl() { Text = DateTime.Now.AddDays(-3).ToString("yyyy-MM-dd"), ItemType = ItemTypeEnum.Time });this.TimeLineControl.Items.Add(new TimeLineItemControl() { Text = "花雨", Head = "花", ItemType = ItemTypeEnum.Name, BackgroundColor = new SolidColorBrush(GetRandomColor()) });this.TimeLineControl.Items.Add(new TimeLineItemControl() { Text = "閆驚鏵/WPFDevelopers", ItemType = ItemTypeEnum.Star });break;case 5:this.TimeLineControl.Items.Add(new TimeLineItemControl() { Text = DateTime.Now.AddDays(-6).ToString("yyyy-MM-dd"), ItemType = ItemTypeEnum.Time });this.TimeLineControl.Items.Add(new TimeLineItemControl() { Text = "紀春慶", Head = "紀", ItemType = ItemTypeEnum.Name, BackgroundColor = new SolidColorBrush(GetRandomColor()) });this.TimeLineControl.Items.Add(new TimeLineItemControl() { Text = "閆驚鏵/WPFDevelopers", ItemType = ItemTypeEnum.Star });break;default:break;}}Color GetRandomColor(){var random = new Random();return Color.FromRgb((byte)random.Next(0, 255), (byte)random.Next(0, 255), (byte)random.Next(0, 255));}} }源碼地址
github:https://github.com/yanjinhuagood/WPFDevelopers.git
gitee:https://gitee.com/yanjinhua/WPFDevelopers.git
WPF開發者QQ群:?340500857?
blogs:?https://www.cnblogs.com/yanjinhua
Github:https://github.com/yanjinhuagood
出處:https://www.cnblogs.com/yanjinhua
版權:本作品采用「署名-非商業性使用-相同方式共享 4.0 國際」許可協議進行許可。
轉載請著名作者 出處 https://github.com/yanjinhuagood
總結
以上是生活随笔為你收集整理的WPF实现时间轴(仿Gitee)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2021年,推荐你使用.NET 5的7大
- 下一篇: 在ASP.NET Core微服务架构下使