ガントチャート作成日記

久しぶりに更新。スマホのゲームにはまってしまって、なかなかこっちに時間が割けなかった。
とりあえず、DataGridではなく、独自で一覧を作成する方向で作り続けてみようと思う。

チャートの部分と同様にGridコントロールに独自クラスをバインドする形式にして、DataTemplateSelecterでDataTemplateを切り替えることでテキストボックスや行部分を表示している。
Thumbコントロールを使ってリサイズも可能なようにしておいて、セルのマージができないのを補ってみた。

工夫したところとしては処理速度を補うために、バインド用のリストとテキストボックス用のリストと行を表現するためのリストを分けた。

<ソース抜粋(GanntGridViewModel.cs)>
   // 左部のタスク一覧を表現するためのセルを格納する各リスト
   this.GanttGridTaskViewPartsRowCells = new ObservableCollection<GanttCell>();
   this.GanttGridTaskViewPartsTaskCells = new ObservableCollection<GanttCell>();
   this.GanttGridTaskViewCells = new ObservableCollection<GanttCell>();

GanttGridTaskViewPartsRowCellsとGanttGridTaskViewPartsTaskCellsにGanntCellクラスをインスタンス化してそれぞれに追加したのちに、this.GanttGridTaskViewCellsに両方のリストの内容をAddしてバインディングに利用する。
そうすることでタスクだけを変更したい時にはGanttGridTaskViewCellsをループするのではなく、GanttGridTaskViewPartsTaskCellsをループすることで対応できる。

問題としては、タスクを追加したときにリストへの追加ミスがないようにしないといけないところと、その際に他の問題が起きないか、というところ・・・。

これは試してみないとわからないので、次回やってみようと思う。

<DownLoad>

ガントチャート作成日記

行や列を見やすくするために別のDataTemplateを表現するようにした。



バインドする全体のGanttCellを格納するGanttGridCellsと列や行を分けて格納しておく
GanttGridRowCells やGanttGridColumnCells を新たに追加した。

public GanttGridViewModel()
{
    this.GanttGridCells = new ObservableCollection<GanttCell>();
    this.GanttGridRowCells = new ObservableCollection<GanttCell>();
    this.GanttGridColumnCells = new ObservableCollection<GanttCell>();
    this.GanttGridTaskCells = new ObservableCollection<GanttCell>();

    GanttHeader h = new GanttHeader();
 
    h.ColumnCount = this.ColumnCount;
    h.ColumnWidth = this.ColumnWidth;

    ObservableCollection<GanttHeaderCell> topCells = new ObservableCollection<GanttHeaderCell>();
    ObservableCollection<GanttHeaderCell> bottomCells = new ObservableCollection<GanttHeaderCell>();

    int colSpanCnt = 0;
    for (int i = 0; i <= this.ColumnCount; i++)
    {
        DateTime d = DateTime.Now.AddDays(i);
        if (d.ToString("dd") == "01" || i == 0)
        {
            int iDaysInMonth = DateTime.DaysInMonth(d.Year, d.Month);
            colSpanCnt = iDaysInMonth - int.Parse(d.ToString("dd")) + 1;
            topCells.Add(new GanttHeaderCell() { ColumnIndex = i, RowIndex = 0, ColumnSpan = colSpanCnt, Date = d });
        }
        bottomCells.Add(new GanttHeaderCell() { ColumnIndex = i, RowIndex = 0, Date = d });
    }

    h.BottomHeaderCells = bottomCells;
    h.TopHeaderCells = topCells;

    TimeSpan ts = DateTime.Now.AddYears(1) - DateTime.Now;
    h.ColumnCount = ts.Days;

    this.Header = h;


    // タスクを表現するセルを追加
    for (int i = 0; i < 500; i++)
    {
        this.GanttGridTaskCells.Add(new GanttCell() { RowIndex = i, RowSpan = 1, ColumnIndex = i, ColumnSpan = 2, DataTemplateType = GanttCell.TemplateType.Task });
    }

    // タスクの数だけ行を表現するセルを追加
    for (int i = 0; i < this.GanttGridTaskCells.Count - 1; i++)
    {
        if (i % 2 == 0)
        {
            this.GanttGridRowCells.Add(new GanttCell() { RowIndex = i, RowSpan = 1, ColumnIndex = 0, ColumnSpan = 365, DataTemplateType = GanttCell.TemplateType.RowLine });
        }
        else
        {
            this.GanttGridRowCells.Add(new GanttCell() { RowIndex = i, RowSpan = 1, ColumnIndex = 0, ColumnSpan = 365, DataTemplateType = GanttCell.TemplateType.AlternateRowLine });
        }
    }

    // 列の数だけ列を表現するセルを追加
    foreach (var item in this.Header.BottomHeaderCells)
    {
        string week = item.Date.ToString("ddd");
        GanttCell.TemplateType type = GanttCell.TemplateType.ColumnLine;
        if (week == "日")
        {
            type = GanttCell.TemplateType.ColumnLineOrange;
        }
        else if (week == "土")
        {
            type = GanttCell.TemplateType.ColumnLineBlue;
        }
        else if (Common.Holiday(item.Date).holiday != Common.HolidayInfo.HOLIDAY.WEEKDAY)
        {
            // 平日以外(祝日・振替休日)の場合は日と同じ色を設定
            type = GanttCell.TemplateType.ColumnLineOrange;
        }

        this.GanttGridColumnCells.Add(new GanttCell() { RowIndex = 0, ColumnIndex = item.ColumnIndex, ColumnSpan = 1, RowSpan = this.GanttGridTaskCells.Count, DataTemplateType = type });
    }

    //バインディングするためのセルを作成する
    foreach (var item in this.GanttGridColumnCells) { this.GanttGridCells.Add(item); }
    foreach (var item in this.GanttGridRowCells) { this.GanttGridCells.Add(item); }
    foreach (var item in this.GanttGridTaskCells) { this.GanttGridCells.Add(item); }

}

バインドされているのはGanttGridCellsプロパティだが見た目を変更するためにDataTemplateSelectorを継承したGanttGridTemplateSelecterを作成している。

GanttGridTemplateSelecterはGanttGrid.xamlで利用している
ItemsControlのItemTemplateSelectorに設定している。
各DataTemplateはUserControlのResourcesに定義しておいて、GanttGridCellsがバインドされたらバインドされているGanttCellのDataTemplateTypeプロパティに応じて利用するDateTemplateを切り替えている。

詳細はソースを参照。
前回アップしたものとWinMergeなどで比較するとわかりやすい。

ついでにDataGridにGanttGridTaskCellsをバインドすることで、ガントチャートっぽくなった。DataGridをスクロールしたときに同期するようにしてみた。
ガントチャート側をスクロールしたときにもスクロールするようにしたりする必要はある。

横に出すのをDataGridのような一覧にするか、移動、リサイズ可能なTextBoxを作成して、それをバインドするか悩むところ。
せっかくなんでいろんなソフトを作ってみようかと思う。

DataGridで階層を表現できると一番いいけど・・・。
セルのマージは難しそうだし、悩ましい。

<DownLoad>

ガントチャート作成日記

タスクを表現してみた。
タスクの移動とサイズをドラッグで変更できるようにプログラムを修正してみた。

リサイズと移動のためにThumbコントロールを利用している。
Gridの幅を細かくしたり、Canvasコントロールに配置すれば、ユーザーが自由にコントロールを配置できる。

<GanttGrid.xaml>に修正を行う。
明細にタスクを表現するためにItemsControlのスタイルを追加する。

                <!-- タスクを表現 -->
                <ItemsControl.ItemContainerStyle>
                    <Style>
                        <Setter Property="Grid.Column" Value="{Binding Path=ColumnIndex}"/>
                        <Setter Property="Grid.Row" Value="{Binding Path=RowIndex}"/>
                        <Setter Property="Grid.ColumnSpan" Value="{Binding Path=ColumnSpan}"/>
                    </Style>
                </ItemsControl.ItemContainerStyle>
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <!--ドラッグ対象のオブジェクトを定義する-->
                        <Border BorderBrush="Black" BorderThickness="0.25"  Height="18">
                            <Grid Height="18">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="3"/>
                                    <ColumnDefinition Width="*"/>
                                    <ColumnDefinition Width="3"/>
                                </Grid.ColumnDefinitions>
                                <Thumb Name="LeftThumb" DragDelta="LeftThumb_DragDelta" QueryCursor="LeftThumb_QueryCursor" Grid.Column="0" >
                                    <Thumb.Template>
                                        <ControlTemplate>
                                            <Grid>
                                                <TextBlock Background="#FFF9CC95" Text="" />
                                            </Grid>
                                        </ControlTemplate>
                                    </Thumb.Template>
                                </Thumb>
                                <Thumb Name="CenterThumb" Grid.Column="1" DragDelta="Thumb_DragDelta" QueryCursor="CenterThumb_QueryCursor">
                                    <Thumb.Template>
                                        <ControlTemplate>
                                            <Grid>
                                                <TextBlock Background="#FFF9CC95" Text="サンプル"  FontFamily="MS Gothic"/>
                                            </Grid>
                                        </ControlTemplate>
                                    </Thumb.Template>
                                </Thumb>
                                <Thumb Name="RightThumb" Grid.Column="2" Width="5" Background="red" DragDelta="RightThumb_DragDelta" QueryCursor="RightThumb_QueryCursor">
                                    <Thumb.Template>
                                        <ControlTemplate>
                                            <Grid>
                                                <TextBlock Background="#FFF9CC95" Text="" />
                                            </Grid>
                                        </ControlTemplate>
                                    </Thumb.Template>
                                </Thumb>

                            </Grid>
                        </Border>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
                
<GanntGrid.cs>
下記のソースコードを追加
26で割っているのはColumnWidthの長さ。
移動量をColumnWidthで割ってColumnIndexを変更量を計算している

        //Thumbコントロールのドラッグイベント処理
        private void Thumb_DragDelta(object sender, DragDeltaEventArgs e)
        {
            var thumb = sender as Thumb;
            if (thumb == null) return;

            var cell = thumb.DataContext as GanttCell;

            int move = (int)e.HorizontalChange / 26;
            Console.WriteLine((e.HorizontalChange / 26).ToString() + ";" + move.ToString());
            cell.ColumnIndex = cell.ColumnIndex + (int)move;

        }

        private void LeftThumb_DragDelta(object sender, DragDeltaEventArgs e)
        {
            Console.WriteLine((e.HorizontalChange / 26).ToString() + ";" + "LeftThumb_DragDelta");

            var thumb = sender as Thumb;
            if (thumb == null) return;

            var cell = thumb.DataContext as GanttCell;

            int move = (int)e.HorizontalChange / 26;
            cell.ColumnIndex = cell.ColumnIndex + (int)move;
            cell.ColumnSpan = cell.ColumnSpan + (int)move * -1;

            Console.WriteLine((move).ToString());

        }

        private void RightThumb_DragDelta(object sender, DragDeltaEventArgs e)
        {
            Console.WriteLine((e.HorizontalChange / 26).ToString() + ";" + "RightThumb_DragDelta");

            var thumb = sender as Thumb;
            if (thumb == null) return;

            var cell = thumb.DataContext as GanttCell;

            int move = (int)e.HorizontalChange / 26;
            cell.ColumnSpan = cell.ColumnSpan + (int)move;
        }

        private void LeftThumb_QueryCursor(object sender, QueryCursorEventArgs e)
        {
            e.Cursor = Cursors.SizeWE;
            e.Handled = true;
        }

        private void CenterThumb_QueryCursor(object sender, QueryCursorEventArgs e)
        {
            e.Cursor = Cursors.Cross;
            e.Handled = true;
        }

        private void RightThumb_QueryCursor(object sender, QueryCursorEventArgs e)
        {
            e.Cursor = Cursors.SizeWE;
            e.Handled = true;
        }





<DownLoad>

ガントチャート作成日記

ヘッダー部を作成するために大きく下記の内容を対応していく。

 ・大きく分けて年月部分と日付部分をセルを表現する
 ・ヘッダーを表示するためのGanttHeaderクラスを作成し、ViewModelにHeaderプロパティを追加
 ・年月部分を表現するためにユーザーコントロールを作成する
 ・日付部分を表現するためにDataTemplateを利用する
  ※ユーザーコントロールを大量に作成すると遅くなるらしいので。

①の部分はユーザーコントロールで作成した。
②の部分はDataTemplateを利用して作成した。




ちょっと困ったのが日曜日と土曜日の背景色について、ValueConverterでバインディングしている日付を渡して色を変えようと思ったが色を事前にXamlに定義したものを利用しようと思うとFindResouceが利用できないので、MultiValueConverterを利用して、自分自身を渡すことで対応した。(DayOfTheWeekBackgroundConverter.cs)

細かいのはサンプルソースを参照。

ーーーーー DayOfTheWeekBackgroundConverter.cs
       public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            DateTime b = (DateTime)values[0];
            FrameworkElement callingElement = (FrameworkElement)values[1];
            LinearGradientBrush brush = null;
            string week = b.ToString("ddd");
            if (week == "日")
            {
                brush = (LinearGradientBrush)callingElement.FindResource("OrangeGradientBrush");
            }
            else if (week == "土")
            {
                brush = (LinearGradientBrush)callingElement.FindResource("BlueGradientBrush");
            }
            else if (Common.Holiday(b).holiday != Common.HolidayInfo.HOLIDAY.WEEKDAY)
            {
                // 平日以外(祝日・振替休日)の場合は日と同じ色を設定
                brush = (LinearGradientBrush)callingElement.FindResource("OrangeGradientBrush");
            }
            else
            {
                brush = (LinearGradientBrush)callingElement.FindResource("NormalGradientBrush");
            }
            return brush;
        }
ーーーーー 

ーーーーー GanttGrid.xaml 
                            <Border x:Name="DayLabelRowBorder" BorderBrush="Silver"  BorderThickness="0.25">
                                <Border.Background>
                                    <MultiBinding Converter="{StaticResource ConverterDayOfTheWeekBackground}">
                                        <Binding Path="Date"/>
                                        <Binding RelativeSource="{RelativeSource Self}"/>
                                    </MultiBinding>
                                </Border.Background>
                                
                                <Grid>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="*"/>
                                        <RowDefinition Height="*"/>
                                    </Grid.RowDefinitions>
                                    <TextBlock x:Name="Day" Text="{Binding Date,Mode=TwoWay,StringFormat=dd}" TextAlignment="Center"  VerticalAlignment="Center" FontFamily="MS Gothic" Grid.Row="0" Padding="0" TextOptions.TextFormattingMode="Display" TextOptions.TextRenderingMode="ClearType" />
                                    <TextBlock x:Name="DayOfWeek" Text="{Binding Date,Converter={StaticResource ConverterDayOfTheWeek}}" TextAlignment="Center"  VerticalAlignment="Center" FontFamily="MS Gothic" Grid.Row="1" Padding="0" TextOptions.TextFormattingMode="Display" TextOptions.TextRenderingMode="ClearType" />
                                </Grid>

                            </Border>
ーーーーー 


<DownLoad>
https://sites.google.com/site/mmbloguserfile/wai-bufairu/20181204_GanntChart.zip

次は、タスクを表現する部分をやろうと思う。
タスクを大量に表示してもそんなに遅くならないようにするようにしたいと思うがどこまでできるか・・・。

ガントチャート作成日記

久しぶりにガントチャート関連の記事を作成。
以前の日記でも書いたが、簡単にガントチャートを作る方法として、
Canvasを使わずにGridを利用する方法を試している。

データ件数が多くなると、遅くなると思われるが500件程度であれば何とか使えるレベルのものを作れると信じて色々やってみているが、前回まで作ったものは件数が多いとちょっと遅くなるので復習と処理速度の改善ができないかをゆっくり考えながら一から作ってみようと思う。

イメージとしてはGridコントロールに独自で作ったGanntCellインスタンスをバインドしてデータを表示する。
GanntCellはGridコントロールにバインドするためのクラス。
行、列、タスクを表現するためには後でDataTemplateを切り替えるようにする。

まずは、明細部分の表示として、ユーザーコントロール(GanttGrid.xml)を作成する。
今回はとりあえず、ベースのコントロールを作るところまで。
次回があれば、データをバインドするところまでやりたい。



---- <GanttGrid.xaml> ----
<UserControl x:Class="GanntChart.GanntGrid.GanttGrid"
             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:GanntChart.GanntGrid"
             xmlns:helper="clr-namespace:GanntChart.GanntGrid.helper"
             xmlns:data="clr-namespace:GanntChart.GanntGrid.data"
             mc:Ignorable="d"
             d:DesignHeight="300" d:DesignWidth="300">
 
    <UserControl.Resources>
        <data:GanntGridViewModel x:Key="GanntGridVM" />
    </UserControl.Resources>


    <UserControl.DataContext>
        <Binding Mode="OneWay" Source="{StaticResource GanntGridVM}"/>
    </UserControl.DataContext>


    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="55"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>
     
        <!-- 明細 -->
        <ScrollViewer Name="scrollDetail" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" Grid.Row="1" >
            <ItemsControl Name="ic" ItemsSource="{Binding GanttGridCells}" VirtualizingPanel.IsVirtualizing="True" 
                              VirtualizingPanel.VirtualizationMode="Recycling" >
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <Grid Name="gridCalender" helper:GridHelpers.RowCount="{Binding RowCount}"
                              helper:GridHelpers.ColumnCount="{Binding ColumnCount}"
                              helper:GridHelpers.ColumnWidth="15"
                              helper:GridHelpers.RowHeight="25"
                              Margin="0,0,0,25" ShowGridLines="True">
                        </Grid>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
            </ItemsControl>
        </ScrollViewer>
    </Grid>

</UserControl>

-------------------------

---- <GanttGrid.cs> ----

namespace GanntChart.GanntGrid
{
    /// <summary>
    /// GanttGrid.xaml の相互作用ロジック
    /// </summary>
    public partial class GanttGrid : UserControl
    {
        public GanttGrid()
        {
            InitializeComponent();
        }

    }
}
-------------------------

次にGanntGridコントロールにバインドするためのViewModelを作成する

---- <GanntGridViewModel.cs> ----

namespace GanntChart.GanntGrid.data
{
    public class GanntGridViewModel : INotifyPropertyChanged
    {

        public GanntGridViewModel()
        {
            this.GanttGridCells = new ObservableCollection<GanttCell>();

            for (int i = 0; i < 500; i++)
            {
                this.GanttGridCells.Add(new GanttCell() );
            }

        }

        /// <summary>
        /// GridHelpersクラスを利用してGantGridのGridコントロールにバインドし、
        /// 行数に対してバインディングを設定する
        /// </summary>
        public int RowCount
        {
            get { return this.GanttGridCells.Count(); }
        }

        public int ColumnCount
        {
            get { return 365; }
        }
        public int RowHeight
        {
            get { return 25; }
        }

        public int ColumnWidth
        {
            get { return 25; }
        }

        private ObservableCollection<GanttCell> _ganttGridCells;
        /// <summary>
        /// GanttGridに表示するResizeRectangleや各セルを設定するためのクラス
        /// 行や列の罫線に対するデータも配列内に設定するので、配列内の要素数とタスク数は一致しない
        /// </summary>
        public ObservableCollection<GanttCell> GanttGridCells
        {
            get { return _ganttGridCells; }
            set
            {
                _ganttGridCells = value;
                NotifyPropertyChanged();
            }
        }


        #region インターフェイス実装イベント
        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion
    }
}
---------------------

GanntGridに表示するCellクラスを作成する。
※見た目についてはあとでDataTemplateを作成する。
---- <GanntCell.cs> ----
namespace GanntChart.GanntGrid.data
{
    /// <summary>
    /// GanttGrid上にデータを表示するためのクラス
    /// Taskを表すResizeRectangleだけではなく、ThunderLineや交互色を表すためにも利用する
    /// </summary>
    public class GanttCell : INotifyPropertyChanged
    {

        #region インターフェイス実装イベント
        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion

    }
}
---------------------

GanntGridでのタスクを管理するためのクラスを作成する。

---- <GanttGridTask.cs> ----
namespace GanntChart.GanntGrid.data
{
    /// <summary>
    /// GanntGridで利用するタスククラス
    /// バインドで利用するために、INotifyPropertyChangedインターフェイスとエラー通知のためのINotifyDataErrorInfoインターフェイスを継承している
    /// GantGrid上では、ResizeRectangleユーザーコントロールにバインドすることでタスクを表現しているが、
    /// その際にはタスクに実装しているCellプロパティで表現している
    /// タスクには子タスクを複数保持することができる
    /// </summary>
    public class GanttGridTask : INotifyPropertyChanged
    {
        /// <summary>
        /// タスクの所属するViewMode
        /// </summary>
        public GanntGridViewModel ViewModel;

        public GanttGridTask()
        {
        }

        #region インターフェイス実装イベント
        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion

        #region プロパティ

        /// <summary>
        /// Gannt表示用
        /// </summary>
        public GanttCell Cell { get; set; }

        #endregion

    }

}
---------------------

共通関数クラス
---- <Common.cs> ----
namespace GanntChart
{
    public static class Common
    {

        public static string CommandLine_Path = "";

        /// <summary>
        /// 文字列の指定した位置から指定した長さを取得する
        /// </summary>
        /// <param name="str">文字列</param>
        /// <param name="start">開始位置</param>
        /// <param name="len">長さ</param>
        /// <returns>取得した文字列</returns>
        public static string Mid(string str, int start, int len)
        {
            if (start <= 0)
            {
                throw new ArgumentException("引数'start'は1以上でなければなりません。");
            }
            if (len < 0)
            {
                throw new ArgumentException("引数'len'は0以上でなければなりません。");
            }
            if (str == null || str.Length < start)
            {
                return "";
            }
            if (str.Length < (start + len))
            {
                return str.Substring(start - 1);
            }
            return str.Substring(start - 1, len);
        }

        /// <summary>
        /// 文字列の指定した位置から末尾までを取得する
        /// </summary>
        /// <param name="str">文字列</param>
        /// <param name="start">開始位置</param>
        /// <returns>取得した文字列</returns>
        public static string Mid(string str, int start)
        {
            return Mid(str, start, str.Length);
        }

        /// <summary>
        /// 文字列の先頭から指定した長さの文字列を取得する
        /// </summary>
        /// <param name="str">文字列</param>
        /// <param name="len">長さ</param>
        /// <returns>取得した文字列</returns>
        public static string Left(string str, int len)
        {
            if (len < 0)
            {
                throw new ArgumentException("引数'len'は0以上でなければなりません。");
            }
            if (str == null)
            {
                return "";
            }
            if (str.Length <= len)
            {
                return str;
            }
            return str.Substring(0, len);
        }

        /// <summary>
        /// 文字列の末尾から指定した長さの文字列を取得する
        /// </summary>
        /// <param name="str">文字列</param>
        /// <param name="len">長さ</param>
        /// <returns>取得した文字列</returns>
        public static string Right(string str, int len)
        {
            if (len < 0)
            {
                throw new ArgumentException("引数'len'は0以上でなければなりません。");
            }
            if (str == null)
            {
                return "";
            }
            if (str.Length <= len)
            {
                return str;
            }
            return str.Substring(str.Length - len, len);
        }

        #region エラー処理

        /// <summary>
        /// エラーメッセージ表示
        /// </summary>
        /// <param name="ex"></param>
        public static void ShowErrMsg(Exception ex)
        {
            System.Windows.MessageBox.Show("エラーが発生しました。" + "\n" + ex.Message + "\n" + ex.StackTrace);
        }

        #endregion

        #region 祝日判定

        // 春分の日を返すメソッド
        public static int Syunbun(int yy)
        {
            int dd;
            if (yy <= 1947)
            {
                dd = 99;
            }
            else if (yy <= 1979)
            {
                dd = (int)(20.8357 + (0.242194 * (yy - 1980)) - (int)((yy - 1983) / 4));
            }
            else if (yy <= 2099)
            {
                dd = (int)(20.8431 + (0.242194 * (yy - 1980)) - (int)((yy - 1980) / 4));
            }
            else if (yy <= 2150)
            {
                dd = (int)(21.851 + (0.242194 * (yy - 1980)) - (int)((yy - 1980) / 4));
            }
            else
            {
                dd = 99;
            }
            return dd;
        }

        // 秋分の日を返すメソッド
        public static int Syubun(int yy)
        {
            int dd;
            if (yy <= 1947)
            {
                dd = 99;
            }
            else if (yy <= 1979)
            {
                dd = (int)(23.2588 + (0.242194 * (yy - 1980)) - (int)((yy - 1983) / 4));
            }
            else if (yy <= 2099)
            {
                dd = (int)(23.2488 + (0.242194 * (yy - 1980)) - (int)((yy - 1980) / 4));
            }
            else if (yy <= 2150)
            {
                dd = (int)(24.2488 + (0.242194 * (yy - 1980)) - (int)((yy - 1980) / 4));
            }
            else
            {
                dd = 99;
            }
            return dd;
        }

        // 祝日情報(戻り値)
        public struct HolidayInfo
        {
            public enum HOLIDAY
            {
                /// <summary>
                /// 平日
                /// </summary>
                WEEKDAY = 0, // 平日
                /// <summary>
                /// 休日
                /// </summary>
                HOLIDAY = 1, // 休日
                /// <summary>
                /// 振替休日
                /// </summary>
                C_HOLIDAY = 2, // 振休
                /// <summary>
                /// 祝日
                /// </summary>
                SYUKUJITSU = 3, // 祝日
            };
            public HOLIDAY holiday; // その日の種類
            public DayOfWeek week; // その日の曜日
            public String name; // その日に名前が付いている場合はその名前。
        };
        private static readonly DateTime SYUKUJITSU = new DateTime(1948, 7, 20); // 祝日法施行日
        private static readonly DateTime FURIKAE = new DateTime(1973, 07, 12); // 振替休日制度の開始日

        // その日が何かを調べるメソッド
        public static HolidayInfo Holiday(DateTime t)
        {
            int yy = t.Year;
            int mm = t.Month;
            int dd = t.Day;
            DayOfWeek ww = t.DayOfWeek;

            HolidayInfo result = new HolidayInfo();
            result.week = ww;
            result.holiday = HolidayInfo.HOLIDAY.WEEKDAY;

            // 祝日法施行以前
            if (t < SYUKUJITSU) return result;

            switch (mm)
            {
                // 1月 //
                case 1:
                    if (dd == 1)
                    {
                        result.holiday = HolidayInfo.HOLIDAY.SYUKUJITSU;
                        result.name = "元日";
                    }
                    else
                    {
                        if (yy >= 2000)
                        {
                            if (((int)((dd - 1) / 7) == 1) && (ww == DayOfWeek.Monday))
                            {
                                result.holiday = HolidayInfo.HOLIDAY.SYUKUJITSU;
                                result.name = "成人の日";
                            }
                        }
                        else
                        {
                            if (dd == 15)
                            {
                                result.holiday = HolidayInfo.HOLIDAY.SYUKUJITSU;
                                result.name = "成人の日";
                            }
                        }
                    }
                    break;
                // 2月 //
                case 2:
                    if (dd == 11)
                    {
                        if (yy >= 1967)
                        {
                            result.holiday = HolidayInfo.HOLIDAY.SYUKUJITSU;
                            result.name = "建国記念の日";
                        }
                    }
                    else if ((yy == 1989) && (dd == 24))
                    {
                        result.holiday = HolidayInfo.HOLIDAY.SYUKUJITSU;
                        result.name = "昭和天皇の大喪の礼";
                    }
                    break;
                // 3月 //
                case 3:
                    if (dd == Syunbun(yy))
                    {
                        result.holiday = HolidayInfo.HOLIDAY.SYUKUJITSU;
                        result.name = "春分の日";
                    }
                    break;
                // 4月 //
                case 4:
                    if (dd == 29)
                    {
                        if (yy >= 2007)
                        {
                            result.holiday = HolidayInfo.HOLIDAY.SYUKUJITSU;
                            result.name = "昭和の日";
                        }
                        else if (yy >= 1989)
                        {
                            result.holiday = HolidayInfo.HOLIDAY.SYUKUJITSU;
                            result.name = "みどりの日";
                        }
                        else
                        {
                            result.holiday = HolidayInfo.HOLIDAY.SYUKUJITSU;
                            result.name = "天皇誕生日";
                        }
                    }
                    else if ((yy == 1959) && (dd == 10))
                    {
                        result.holiday = HolidayInfo.HOLIDAY.SYUKUJITSU;
                        result.name = "皇太子明仁親王の結婚の儀";
                    }
                    break;
                // 5月 //
                case 5:
                    if (dd == 3)
                    {
                        result.holiday = HolidayInfo.HOLIDAY.SYUKUJITSU;
                        result.name = "憲法記念日";
                    }
                    else if (dd == 4)
                    {
                        if (yy >= 2007)
                        {
                            result.holiday = HolidayInfo.HOLIDAY.SYUKUJITSU;
                            result.name = "みどりの日";
                        }
                        else if (yy >= 1986)
                        {
                            /* 5/4が日曜日は『只の日曜』、月曜日は『憲法記念日の振替休日』(~2006年)*/
                            if (ww > DayOfWeek.Monday)
                            {
                                result.holiday = HolidayInfo.HOLIDAY.HOLIDAY;
                                result.name = "国民の休日";
                            }
                        }
                    }
                    else if (dd == 5)
                    {
                        result.holiday = HolidayInfo.HOLIDAY.SYUKUJITSU;
                        result.name = "こどもの日";
                    }
                    else if (dd == 6)
                    {
                        /* [5/3,5/4が日曜]ケースのみ、ここで判定 */
                        if ((yy >= 2007) && ((ww == DayOfWeek.Tuesday) || (ww == DayOfWeek.Wednesday)))
                        {
                            result.holiday = HolidayInfo.HOLIDAY.C_HOLIDAY;
                            result.name = "振替休日";
                        }
                    }
                    break;
                // 6月 //
                case 6:
                    if ((yy == 1993) && (dd == 9))
                    {
                        result.holiday = HolidayInfo.HOLIDAY.SYUKUJITSU;
                        result.name = "皇太子徳仁親王の結婚の儀";
                    }
                    break;
                // 7月 //
                case 7:
                    if (yy >= 2003)
                    {
                        if (((int)((dd - 1) / 7) == 2) && (ww == DayOfWeek.Monday))
                        {
                            result.holiday = HolidayInfo.HOLIDAY.SYUKUJITSU;
                            result.name = "海の日";
                        }
                    }
                    else if (yy >= 1996)
                    {
                        if (dd == 20)
                        {
                            result.holiday = HolidayInfo.HOLIDAY.SYUKUJITSU;
                            result.name = "海の日";
                        }
                    }
                    break;
                // 8月 //
                case 8:
                    if (dd == 11)
                    {
                        if (yy >= 2016)
                        {
                            result.holiday = HolidayInfo.HOLIDAY.SYUKUJITSU;
                            result.name = "山の日";
                        }
                    }
                    break;
                // 9月 //
                case 9:
                    if (dd == Syubun(yy))
                    {
                        result.holiday = HolidayInfo.HOLIDAY.SYUKUJITSU;
                        result.name = "秋分の日";
                    }
                    else
                    {
                        if (yy >= 2003)
                        {
                            if (((int)((dd - 1) / 7) == 2) && (ww == DayOfWeek.Monday))
                            {
                                result.holiday = HolidayInfo.HOLIDAY.SYUKUJITSU;
                                result.name = "敬老の日";
                            }
                            else if (ww == DayOfWeek.Tuesday)
                            {
                                if (dd == Syubun(yy) - 1)
                                {
                                    result.holiday = HolidayInfo.HOLIDAY.HOLIDAY;
                                    result.name = "国民の休日";
                                }
                            }
                        }
                        else if (yy >= 1966)
                        {
                            if (dd == 15)
                            {
                                result.holiday = HolidayInfo.HOLIDAY.SYUKUJITSU;
                                result.name = "敬老の日";
                            }
                        }
                    }
                    break;
                // 10月 //
                case 10:
                    if (yy >= 2000)
                    {
                        if (((int)((dd - 1) / 7) == 1) && (ww == DayOfWeek.Monday))
                        {
                            result.holiday = HolidayInfo.HOLIDAY.SYUKUJITSU;
                            result.name = "体育の日";
                        }
                    }
                    else if (yy >= 1966)
                    {
                        if (dd == 10)
                        {
                            result.holiday = HolidayInfo.HOLIDAY.SYUKUJITSU;
                            result.name = "体育の日";
                        }
                    }
                    break;
                // 11月 //
                case 11:
                    if (dd == 3)
                    {
                        result.holiday = HolidayInfo.HOLIDAY.SYUKUJITSU;
                        result.name = "文化の日";
                    }
                    else if (dd == 23)
                    {
                        result.holiday = HolidayInfo.HOLIDAY.SYUKUJITSU;
                        result.name = "勤労感謝の日";
                    }
                    else if ((yy == 1990) && (dd == 12))
                    {
                        result.holiday = HolidayInfo.HOLIDAY.SYUKUJITSU;
                        result.name = "即位礼正殿の儀";
                    }
                    break;
                // 12月 //
                case 12:
                    if (dd == 23)
                    {
                        if (yy >= 1989)
                        {
                            result.holiday = HolidayInfo.HOLIDAY.SYUKUJITSU;
                            result.name = "天皇誕生日";
                        }
                    }
                    break;
                default:
                    break;
            }

            if ((result.holiday == HolidayInfo.HOLIDAY.WEEKDAY
                 || result.holiday == HolidayInfo.HOLIDAY.HOLIDAY) &&
                (ww == DayOfWeek.Monday))
            {
                /*月曜以外は振替休日判定不要
                  5/6(火,水)の判定は上記ステップで処理済
                  5/6(月)はここで判定する  */
                if (t >= FURIKAE)
                {
                    if (Holiday(t.AddDays(-1)).holiday == HolidayInfo.HOLIDAY.SYUKUJITSU)
                    {    /* 再帰呼出 */
                        result.holiday = HolidayInfo.HOLIDAY.C_HOLIDAY;
                        result.name = "振替休日";
                    }
                }
            }
            return result;
        }
        #endregion

    }
}
---------------------

GanntGridではGridコントロールに対してデータをバインドして利用するためにヘルパークラスを作成する。
※https://rachel53461.wordpress.com/2011/09/17/wpf-grids-rowcolumn-count-properties/のソースを利用
 一部修正
---- <GridHelpers.cs> ----
namespace GanntChart.GanntGrid.helper
{
    // https://rachel53461.wordpress.com/2011/09/17/wpf-grids-rowcolumn-count-properties/
    public class GridHelpers
    {
        #region RowCount Property

        /// <summary>
        /// Adds the specified number of Rows to RowDefinitions.
        /// Default Height is Auto
        /// </summary>
        public static readonly DependencyProperty RowCountProperty =
            DependencyProperty.RegisterAttached(
                "RowCount", typeof(int), typeof(GridHelpers),
                new PropertyMetadata(-1, RowCountChanged));

        // Get
        public static int GetRowCount(DependencyObject obj)
        {
            return (int)obj.GetValue(RowCountProperty);
        }

        // Set
        public static void SetRowCount(DependencyObject obj, int value)
        {
            obj.SetValue(RowCountProperty, value);
        }

        // Change Event - Adds the Rows
        public static void RowCountChanged(
            DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            if (!(obj is Grid) || (int)e.NewValue < 0)
                return;

            Grid grid = (Grid)obj;
            grid.RowDefinitions.Clear();

            for (int i = 0; i < (int)e.NewValue; i++)
                grid.RowDefinitions.Add(
                    new RowDefinition() { Height = GridLength.Auto });

            SetStarRows(grid);

            // 行幅再設定
            SetRowHeight(grid);

        }

        #endregion

        #region ColumnCount Property

        /// <summary>
        /// Adds the specified number of Columns to ColumnDefinitions.
        /// Default Width is Auto
        /// </summary>
        public static readonly DependencyProperty ColumnCountProperty =
            DependencyProperty.RegisterAttached(
                "ColumnCount", typeof(int), typeof(GridHelpers),
                new PropertyMetadata(-1, ColumnCountChanged));

        // Get
        public static int GetColumnCount(DependencyObject obj)
        {
            return (int)obj.GetValue(ColumnCountProperty);
        }

        // Set
        public static void SetColumnCount(DependencyObject obj, int value)
        {
            obj.SetValue(ColumnCountProperty, value);
        }

        // Change Event - Add the Columns
        public static void ColumnCountChanged(
            DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            if (!(obj is Grid) || (int)e.NewValue < 0)
                return;

            Grid grid = (Grid)obj;
            grid.ColumnDefinitions.Clear();

            for (int i = 0; i < (int)e.NewValue; i++)
                grid.ColumnDefinitions.Add(
                    new ColumnDefinition() { Width = GridLength.Auto });

            SetStarColumns(grid);

            // カラム幅再設定
            SetColumnWidth(grid);

        }

        #endregion

        #region StarRows Property

        /// <summary>
        /// Makes the specified Row's Height equal to Star.
        /// Can set on multiple Rows
        /// </summary>
        public static readonly DependencyProperty StarRowsProperty =
            DependencyProperty.RegisterAttached(
                "StarRows", typeof(string), typeof(GridHelpers),
                new PropertyMetadata(string.Empty, StarRowsChanged));

        // Get
        public static string GetStarRows(DependencyObject obj)
        {
            return (string)obj.GetValue(StarRowsProperty);
        }

        // Set
        public static void SetStarRows(DependencyObject obj, string value)
        {
            obj.SetValue(StarRowsProperty, value);
        }

        // Change Event - Makes specified Row's Height equal to Star
        public static void StarRowsChanged(
            DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            if (!(obj is Grid) || string.IsNullOrEmpty(e.NewValue.ToString()))
                return;

            SetStarRows((Grid)obj);
        }

        #endregion

        #region StarColumns Property

        /// <summary>
        /// Makes the specified Column's Width equal to Star.
        /// Can set on multiple Columns
        /// </summary>
        public static readonly DependencyProperty StarColumnsProperty =
            DependencyProperty.RegisterAttached(
                "StarColumns", typeof(string), typeof(GridHelpers),
                new PropertyMetadata(string.Empty, StarColumnsChanged));

        // Get
        public static string GetStarColumns(DependencyObject obj)
        {
            return (string)obj.GetValue(StarColumnsProperty);
        }

        // Set
        public static void SetStarColumns(DependencyObject obj, string value)
        {
            obj.SetValue(StarColumnsProperty, value);
        }

        // Change Event - Makes specified Column's Width equal to Star
        public static void StarColumnsChanged(
            DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            if (!(obj is Grid) || string.IsNullOrEmpty(e.NewValue.ToString()))
                return;

            SetStarColumns((Grid)obj);
        }

        #endregion

        #region ColumnWidth Property

        /// <summary>
        /// カラム幅設定用独自プロパティ
        /// </summary>
        public static readonly DependencyProperty ColumnWidthProperty =
            DependencyProperty.RegisterAttached(
                "ColumnWidth", typeof(int), typeof(GridHelpers),
                new PropertyMetadata(0, ColumnWidthChanged));

        // 取得処理
        public static int GetColumnWidth(DependencyObject obj)
        {
            return (int)obj.GetValue(ColumnWidthProperty);
        }

        // 設定処理
        public static void SetColumnWidth(DependencyObject obj, string value)
        {
            obj.SetValue(ColumnWidthProperty, value);
        }

        // 値変更時処理
        public static void ColumnWidthChanged(
            DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            if (!(obj is Grid) || string.IsNullOrEmpty(e.NewValue.ToString()))
                return;

            SetColumnWidth((Grid)obj);
        }

        #endregion

        #region RowHeight Property
        /// <summary>
        /// カラム幅設定用独自プロパティ
        /// </summary>
        public static readonly DependencyProperty RowHeightProperty =
            DependencyProperty.RegisterAttached(
                "RowHeight", typeof(int), typeof(GridHelpers),
                new PropertyMetadata(0, RowHeightChanged));

        // 取得処理
        public static int GetRowHeight(DependencyObject obj)
        {
            return (int)obj.GetValue(RowHeightProperty);
        }

        // 設定処理
        public static void SetRowHeight(DependencyObject obj, string value)
        {
            obj.SetValue(RowHeightProperty, value);
        }

        // 値変更時処理
        public static void RowHeightChanged(
            DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            if (!(obj is Grid) || string.IsNullOrEmpty(e.NewValue.ToString()))
                return;
            SetRowHeight((Grid)obj);
        }

        #endregion


        private static void SetStarColumns(Grid grid)
        {
            string[] starColumns =
                GetStarColumns(grid).Split(',');

            for (int i = 0; i < grid.ColumnDefinitions.Count; i++)
            {
                if (starColumns.Contains(i.ToString()))
                    grid.ColumnDefinitions[i].Width =
                        new GridLength(1, GridUnitType.Star);
            }
        }

        private static void SetStarRows(Grid grid)
        {
            string[] starRows =
                GetStarRows(grid).Split(',');

            for (int i = 0; i < grid.RowDefinitions.Count; i++)
            {
                if (starRows.Contains(i.ToString()))
                    grid.RowDefinitions[i].Height =
                        new GridLength(1, GridUnitType.Star);
            }
        }

        // カラム幅設定の独自プロパティ設定用
        private static void SetColumnWidth(Grid grid)
        {
            for (int i = 0; i < grid.ColumnDefinitions.Count; i++)
            {
                grid.ColumnDefinitions[i].Width = new GridLength(GetColumnWidth(grid));
            }
        }

        // 行幅設定の独自プロパティ設定用
        private static void SetRowHeight(Grid grid)
        {
            for (int i = 0; i < grid.RowDefinitions.Count; i++)
            {
                grid.RowDefinitions[i].Height = new GridLength(GetRowHeight(grid));
            }
        }
    }
}
---------------------


メイン画面
とりあえず配置するだけ。

---- <MainWindow.xaml> ----

<Window
        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:GanntChart"
        xmlns:GanntGrid="clr-namespace:GanntChart.GanntGrid" x:Class="GanntChart.MainWindow"
        mc:Ignorable="d"
        Title="MainWindow" Height="720" Width="1024">
    <Grid>

        <GanntGrid:GanttGrid x:Name="grid" HorizontalAlignment="Left" Height="571" Margin="47,75,0,0" VerticalAlignment="Top" Width="933"/>

    </Grid>
</Window>
-------------------------


---- <MainWindow.cs> ----
namespace GanntChart
{
    /// <summary>
    /// MainWindow.xaml の相互作用ロジック
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            SetEvents();
        }

        private void SetEvents()
        {
            this.Loaded += MainWindow_Loaded;
        }

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            GanntChart.GanntGrid.data.GanntGridViewModel ViewModel = new GanntGrid.data.GanntGridViewModel();
            grid.DataContext = ViewModel;

        }
    }
}
-------------------------

PowerShellでEdgeを自動化(インストール不要。参考:郵便追跡サービス自動操作)

1.経緯について  RPAのソフトをインストールできないので、これまでVBSでCreateObjectでブラウザの自動操作をすることがたまにあった。 ※いざというときの手札として持っているだけで安心感が段違い  見た目上IEがインストールされていなくても、CreateObject...