全て
| カテゴリ未分類
| 通常
| 数学
| 投資
| 副業
| 転職・就職
| ブログ
| 株Stock
| VBA
| SharePoint
| REST API
| RenderListDataAsStream
| PowerAutomateDesktop
| Graph API
| PowerApps
| PowerAutomate
| PowerFx
| OfficeScriptオフィススクリプト
| PowerAutomate日時Excel日時相互関係
| PowerShell
| Dataverse
| UiPath
| OneDrive
| LINQ
| VB.NET
| C#.NET
| WPFC#.NET
| Git
| ネットワーク
| Azure
| AWS
| JP1
| セキュリティ
| JavaScript
| Google Cloud
カテゴリ:C#.NET
以下は、C# WPFでガントチャートを作成するサンプルコードです。このサンプルでは、`GanttChartItem`というクラスを作成して、ガントチャートのアイテムを表現します。そして、`GanttChart`というユーザーコントロールを作成し、ガントチャート全体を表現します。 まず、`GanttChartItem`クラスを作成します。 ```csharp using System; namespace GanttChartSample { public class GanttChartItem { public string Name { get; set; } public DateTime StartDate { get; set; } public DateTime EndDate { get; set; } } } ``` 次に、`GanttChart`ユーザーコントロールを作成します。 ```csharp using System; using System.Collections.Generic; using System.Windows; using System.Windows.Controls; using System.Windows.Media; namespace GanttChartSample { public partial class GanttChart : UserControl { public List<GanttChartItem> Items { get { return (List<GanttChartItem>)GetValue(ItemsProperty); } set { SetValue(ItemsProperty, value); } } public static readonly DependencyProperty ItemsProperty = DependencyProperty.Register("Items", typeof(List<GanttChartItem>), typeof(GanttChart), new PropertyMetadata(new List<GanttChartItem>(), OnItemsPropertyChanged)); private static void OnItemsPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var chart = d as GanttChart; if (chart != null) { chart.DrawChart(); } } public GanttChart() { InitializeComponent(); } private void DrawChart() { ChartCanvas.Children.Clear(); if (Items == null || Items.Count == 0) return; double totalDays = 0; foreach (var item in Items) { totalDays = Math.Max(totalDays, (item.EndDate - item.StartDate).TotalDays); } double itemHeight = 30; double itemWidth = ChartCanvas.ActualWidth / totalDays; for (int i = 0; i < Items.Count; i++) { var item = Items[i]; double x = (item.StartDate - Items[0].StartDate).TotalDays * itemWidth; double width = (item.EndDate - item.StartDate).TotalDays * itemWidth; var rect = new Rectangle { Width = width, Height = itemHeight, Fill = Brushes.Blue, Stroke = Brushes.Black, StrokeThickness = 1 }; Canvas.SetLeft(rect, x); Canvas.SetTop(rect, i * itemHeight); ChartCanvas.Children.Add(rect); var label = new Label { Content = item.Name, Foreground = Brushes.White, HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center, FontWeight = FontWeights.Bold }; Canvas.SetLeft(label, x); Canvas.SetTop(label, i * itemHeight); ChartCanvas.Children.Add(label); } } } } ``` そして、XAMLファイルで`GanttChart`ユーザーコントロールを使用します。 ```xml <UserControl x:Class="GanttChartSample.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:GanttChartSample" mc:Ignorable="d" d:DesignHeight="450" d:DesignWidth="800"> <Grid> <local:GanttChart Items="{Binding GanttChartItems}" /> </Grid> </UserControl> ``` これで、`GanttChart`を含むWPFアプリケーションを作成し、`GanttChartItem`をバインドすることでガントチャートを表示できます。 お気に入りの記事を「いいね!」で応援しよう
Last updated
2024.03.17 09:56:56
[C#.NET] カテゴリの最新記事
|