SHOEISHA iD

※旧SEメンバーシップ会員の方は、同じ登録情報(メールアドレス&パスワード)でログインいただけます

CodeZine編集部では、現場で活躍するデベロッパーをスターにするためのカンファレンス「Developers Summit」や、エンジニアの生きざまをブーストするためのイベント「Developers Boost」など、さまざまなカンファレンスを企画・運営しています。

Infragistics NetAdvantageチュートリアル(AD)

Silverlight/WPFでデータバインディングを利用しOutlookライクなスケジュールを構築する その1
Silverlight編

Silverlight/WPFでOutlookライクなアプリ構築(1)

  • このエントリーをはてなブックマークに追加

画面に表示させるデータをコードで生成する

 今回はコード ビハインドを用いて画面に表示するデータを生成します。まず、先ほど作成した ScheduleDataViewModel プロジェクトを xamSchedule_SL プロジェクトに追加します。追加する場合は、プロジェクトの [参照設定] のコンテキスト メニューから [参照の追加] を選択します。その後表示される追加ダイアログで [プロジェクト] タブを選択し、ScheduleDataViewModel を追加します。

図11 プロジェクト参照の追加
図11 プロジェクト参照の追加

 次に xamSchedule_SL プロジェクトに新しいフォルダーを "ViewModels" と名付け作成し、

 そのフォルダー内に新しいクラスを "MainPageViewModel.cs" と名付け、作成します。

図12 フォルダー クラスを追加
図12 フォルダー クラスを追加

 MainPageViewModel.cs クラスでは、先ほど参照を追加した ScheduleDataViewModel を用いて、Resource, ResourceCalendar, Appointment を格納するプロパティを作成します。また、現在のユーザーを示す、CurrentUserId プロパティを実装します。

コード8 MainPageViewModel.cs プロパティ部分
using System;
using ScheduleDataViewModel;
using System.Collections.ObjectModel;

namespace xamSchedule_SL.ViewModels
{
    public class MainPageViewModel : ViewModelBase
    {
        #region プロパティ

        private ObservableCollection<ResourceInfo> _resources;
        public ObservableCollection<ResourceInfo> Resources
        {
            get { return _resources; }
            set
            {
                _resources = value;
                OnPropertyChanged("Resources");
            }
        }

        private ObservableCollection<ResourceCalendarInfo> _resourceCalendars;
        public ObservableCollection<ResourceCalendarInfo> ResourceCalendars
        {
            get { return _resourceCalendars; }
            set
            {
                _resourceCalendars = value;
                OnPropertyChanged("ResourceCalendars");
            }
        }

        private ObservableCollection<AppointmentInfo> _appointments;
        public ObservableCollection<AppointmentInfo> Appointments
        {
            get { return _appointments; }
            set
            {
                _appointments = value;
                OnPropertyChanged("Appointments");
            }
        }

        private string _currentUserId;
        public string CurrentUserId
        {
            get { return _currentUserId; }
            set
            {
                _currentUserId = value;
                OnPropertyChanged("CurrentUserId");
            }
        }
        
        #endregion
        // その他の実装
    }
}

 次にコンストラクタを実装します。今回の記事ではコードでデータを作成します。ここでの注意点としては、予定の開始時刻、終了時刻は世界標準時であるということです。

コード9 MainPageViewModel.csコンストラクタでデータを作成
using System;
using ScheduleDataViewModel;
using System.Collections.ObjectModel;

namespace xamSchedule_SL.ViewModels
{
    public class MainPageViewModel : ViewModelBase
    {
        #region プロパティ
            // 省略
        #endregion

        #region コンストラクタ
        public MainPageViewModel()
        {
            // リソースを作成
            Resources = new ObservableCollection<ResourceInfo>();
            Resources.Add(new ResourceInfo()
            {
                Id = "R0001",
                PrimaryResourceCalendarId = "C0001"
            });

            // リソースカレンダーを作成
            ResourceCalendars = new ObservableCollection<ResourceCalendarInfo>();
            ResourceCalendars.Add(new ResourceCalendarInfo()
            {
                 Id = "C0001",
                 OwningResourceId = "R0001"
            });

            // 予定を追加
            Appointments = new ObservableCollection<AppointmentInfo>();
            Appointments.Add(new AppointmentInfo()
            {
                 Id = "A0001",
                 OwningCalendarId = "C0001",
                 OwningResourceId = "R0001",
                 Start = DateTime.Now.ToUniversalTime(),
                 End = DateTime.Now.AddHours(2).ToUniversalTime(),
            });

            // 現在のユーザー ID を指定
            this.CurrentUserId = this.Resources[0].Id;
        }        
        #endregion
    }
}

 この MainPageViewModel は実行時に MainPage の DataContext に格納します。そのため、MainPage.xaml.cs を開き、コンストラクタで MainPageViewModel のインスタンスを作成します。

コード10 MainPage.xaml.cs
using System.Windows.Controls;
using xamSchedule_SL.ViewModels;

namespace xamSchedule_SL
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            MainPageViewModel vm = new MainPageViewModel();
            this.DataContext = vm;
        }
    }
}

 これでスケジュールに表示させるためのデータの作成が完了しました。

次のページ
データのマッピングを行い、予定を画面に表示させる

この記事は参考になりましたか?

  • このエントリーをはてなブックマークに追加
Infragistics NetAdvantageチュートリアル連載記事一覧

もっと読む

この記事の著者

インフラジスティックス・ジャパン株式会社 池原 大然(イケハラ ダイゼン)

国内ベンチャー企業にて.NETエンジニアとして開発に従事、2007年インフラジスティックス・ジャパンに入社。現在デベロッパー エバンジェリストとして、.NETやWPF/Silverlight製品や技術の啓蒙活動を行う。Microsoft MVP for Client App Dev 2010/04 ...

※プロフィールは、執筆時点、または直近の記事の寄稿時点での内容です

【AD】本記事の内容は記事掲載開始時点のものです 企画・制作 株式会社翔泳社

この記事は参考になりましたか?

この記事をシェア

  • このエントリーをはてなブックマークに追加
CodeZine(コードジン)
https://codezine.jp/article/detail/5669 2010/12/27 17:28

おすすめ

アクセスランキング

アクセスランキング

イベント

CodeZine編集部では、現場で活躍するデベロッパーをスターにするためのカンファレンス「Developers Summit」や、エンジニアの生きざまをブーストするためのイベント「Developers Boost」など、さまざまなカンファレンスを企画・運営しています。

新規会員登録無料のご案内

  • ・全ての過去記事が閲覧できます
  • ・会員限定メルマガを受信できます

メールバックナンバー

アクセスランキング

アクセスランキング