「東雲 忠太郎」の平凡な日常のできごと

「東雲 忠太郎」の平凡な日常のできごと

2024.03.18
XML
カテゴリ: C#.NET


以下は、C# WPFアプリケーションでシフト管理表を作成するサンプルコードです。このサンプルでは、日付ごとの従業員のシフト情報を表示し、編集することができます。


まず、Shiftクラスを定義します。このクラスは、日付と従業員の名前を保持します。


```csharp

public class Shift

{

    public DateTime Date { get; set; }

    public string EmployeeName { get; set; }

}

```


次に、MainViewModelクラスを作成します。このクラスでは、シフトの一覧を管理します。


```csharp

using System;

using System.Collections.ObjectModel;

using System.ComponentModel;

using System.Runtime.CompilerServices;


public class MainViewModel : INotifyPropertyChanged

{

    private ObservableCollection<Shift> _shifts;

    public ObservableCollection<Shift> Shifts

    {

        get { return _shifts; }

        set

        {

            _shifts = value;

            OnPropertyChanged();

        }

    }


    public MainViewModel()

    {

        // サンプルデータの生成

        Shifts = new ObservableCollection<Shift>

        {

            new Shift { Date = DateTime.Today, EmployeeName = "John Doe" },

            new Shift { Date = DateTime.Today.AddDays(1), EmployeeName = "Jane Smith" },

            // 必要に応じて他のシフト情報を追加

        };

    }


    public event PropertyChangedEventHandler PropertyChanged;


    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)

    {

        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));

    }

}

```


次に、MainWindow.xamlファイルにデータグリッドを追加し、バインディングを設定します。


```xml

<Window x:Class="ShiftManagementApp.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        xmlns:local="clr-namespace:ShiftManagementApp"

        Title="Shift Management" Height="450" Width="800">

    <Grid>

        <DataGrid ItemsSource="{Binding Shifts}" AutoGenerateColumns="False">

            <DataGrid.Columns>

                <DataGridTextColumn Header="Date" Binding="{Binding Date}"/>

                <DataGridTextColumn Header="Employee Name" Binding="{Binding EmployeeName}"/>

            </DataGrid.Columns>

        </DataGrid>

    </Grid>

</Window>

```


最後に、MainWindow.xaml.csファイルでViewModelを作成し、DataContextとして設定します。


```csharp

public partial class MainWindow : Window

{

    public MainWindow()

    {

        InitializeComponent();

        DataContext = new MainViewModel();

    }

}

```


これで、C# WPFアプリケーションでシフト管理表を作成し、表示することができます。必要に応じて、データの追加、削除、編集などの機能を追加することができます。






お気に入りの記事を「いいね!」で応援しよう

Last updated  2024.03.18 07:55:44


【毎日開催】
15記事にいいね!で1ポイント
10秒滞在
いいね! -- / --
おめでとうございます!
ミッションを達成しました。
※「ポイントを獲得する」ボタンを押すと広告が表示されます。
x

© Rakuten Group, Inc.
Create a Mobile Website
スマートフォン版を閲覧 | PC版を閲覧
Share by: